示例#1
0
  /**
   * Tests method setRequestContent of HttpClient.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void writeMultipartMessage() throws IOException {
    final HttpRequest req = new HttpRequest();
    req.isMultipart = true;
    req.payloadAttrs.put("media-type", "multipart/alternative");
    req.payloadAttrs.put("boundary", "boundary42");
    final Part p1 = new Part();
    p1.headers.put("Content-Type", "text/plain; charset=us-ascii");
    p1.bodyAttrs.put("media-type", "text/plain");
    final String plain = "...plain text....";
    p1.bodyContent.add(Str.get(plain + '\n'));

    final Part p2 = new Part();
    p2.headers.put("Content-Type", "text/richtext");
    p2.bodyAttrs.put("media-type", "text/richtext");
    final String rich = ".... richtext version...";
    p2.bodyContent.add(Str.get(rich));

    final Part p3 = new Part();
    p3.headers.put("Content-Type", "text/x-whatever");
    p3.bodyAttrs.put("media-type", "text/x-whatever");
    final String fancy = ".... fanciest formatted version...";
    p3.bodyContent.add(Str.get(fancy));

    req.parts.add(p1);
    req.parts.add(p2);
    req.parts.add(p3);

    final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com"));
    HttpClient.setRequestContent(fakeConn.getOutputStream(), req);
    final String expResult =
        "--boundary42"
            + CRLF
            + "Content-Type: text/plain; charset=us-ascii"
            + CRLF
            + CRLF
            + plain
            + Prop.NL
            + CRLF
            + "--boundary42"
            + CRLF
            + "Content-Type: text/richtext"
            + CRLF
            + CRLF
            + rich
            + CRLF
            + "--boundary42"
            + CRLF
            + "Content-Type: text/x-whatever"
            + CRLF
            + CRLF
            + fancy
            + CRLF
            + "--boundary42--"
            + CRLF;

    // Compare results
    assertEquals(expResult, fakeConn.getOutputStream().toString());
  }
示例#2
0
  /**
   * Tests parsing of multipart request when the contents for each part are set from the $bodies
   * parameter.
   *
   * @throws IOException I/O Exception
   * @throws QueryException query exception
   */
  @Test
  public void parseMultipartReqBodies() throws IOException, QueryException {
    final String multiReq =
        "<http:request "
            + "xmlns:http='http://expath.org/ns/http-client' "
            + "method='POST' href='"
            + REST_ROOT
            + "'>"
            + "<http:header name='hdr1' value='hdr1val'/>"
            + "<http:header name='hdr2' value='hdr2val'/>"
            + "<http:multipart media-type='multipart/mixed' boundary='xxxx'>"
            + "<http:header name='p1hdr1' value='p1hdr1val'/>"
            + "<http:header name='p1hdr2' value='p1hdr2val'/>"
            + "<http:body media-type='text/plain'/>"
            + "<http:header name='p2hdr1' value='p2hdr1val'/>"
            + "<http:body media-type='text/plain'/>"
            + "<http:body media-type='text/plain'/>"
            + "</http:multipart>"
            + "</http:request>";

    final DBNode dbNode1 = new DBNode(new IOContent(multiReq));
    final ItemList bodies = new ItemList();
    bodies.add(Str.get("Part1"));
    bodies.add(Str.get("Part2"));
    bodies.add(Str.get("Part3"));

    final HttpRequestParser rp = new HttpRequestParser(null);
    final HttpRequest r = rp.parse(dbNode1.children().next(), bodies.iter());

    assertEquals(2, r.attributes.size());
    assertEquals(2, r.headers.size());
    assertTrue(r.isMultipart);
    assertEquals(3, r.parts.size());

    // check parts
    final Iterator<Part> i = r.parts.iterator();
    Part part = i.next();
    assertEquals(2, part.headers.size());
    assertEquals(1, part.bodyContent.size());
    assertEquals(1, part.bodyAttrs.size());

    part = i.next();
    assertEquals(1, part.headers.size());
    assertEquals(1, part.bodyContent.size());
    assertEquals(1, part.bodyAttrs.size());

    part = i.next();
    assertEquals(0, part.headers.size());
    assertEquals(1, part.bodyContent.size());
    assertEquals(1, part.bodyAttrs.size());
  }
示例#3
0
  /**
   * Tests writing of request content with different combinations of the body attributes media-type
   * and method.
   *
   * @throws IOException IO exception
   */
  @Test
  public void writeMessage() throws IOException {
    // Case 1: No method, media-type='text/xml'
    final HttpRequest req1 = new HttpRequest();
    final FakeHttpConnection fakeConn1 = new FakeHttpConnection(new URL("http://www.test.com"));
    req1.payloadAttrs.put(SerializerOptions.MEDIA_TYPE.name(), "text/xml");
    // Node child
    final FElem e1 = new FElem("a").add("a");
    req1.bodyContent.add(e1);
    // String item child
    req1.bodyContent.add(Str.get("<b>b</b>"));
    HttpClient.setRequestContent(fakeConn1.getOutputStream(), req1);
    assertEquals("<a>a</a>&lt;b&gt;b&lt;/b&gt;", fakeConn1.out.toString(Strings.UTF8));

    // Case 2: No method, media-type='text/plain'
    final HttpRequest req2 = new HttpRequest();
    final FakeHttpConnection fakeConn2 = new FakeHttpConnection(new URL("http://www.test.com"));
    req2.payloadAttrs.put(SerializerOptions.MEDIA_TYPE.name(), "text/plain");
    // Node child
    final FElem e2 = new FElem("a").add("a");
    req2.bodyContent.add(e2);
    // String item child
    req2.bodyContent.add(Str.get("<b>b</b>"));
    HttpClient.setRequestContent(fakeConn2.getOutputStream(), req2);
    assertEquals("a<b>b</b>", fakeConn2.out.toString());

    // Case 3: method='text', media-type='text/xml'
    final HttpRequest req3 = new HttpRequest();
    final FakeHttpConnection fakeConn3 = new FakeHttpConnection(new URL("http://www.test.com"));
    req3.payloadAttrs.put(SerializerOptions.MEDIA_TYPE.name(), "text/xml");
    req3.payloadAttrs.put("method", "text");
    // Node child
    final FElem e3 = new FElem("a").add("a");
    req3.bodyContent.add(e3);
    // String item child
    req3.bodyContent.add(Str.get("<b>b</b>"));
    HttpClient.setRequestContent(fakeConn3.getOutputStream(), req3);
    assertEquals("a<b>b</b>", fakeConn3.out.toString());
  }
示例#4
0
  /**
   * Tests ResponseHandler.getResponse() with multipart response having preamble and epilogue.
   *
   * @throws IOException I/O Exception
   * @throws Exception exception
   */
  @Test
  public void multipartRespPreamble() throws Exception {
    // Create fake HTTP connection
    final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com"));
    final Map<String, List<String>> hdrs = new HashMap<>();
    final List<String> fromVal = new ArrayList<>();
    fromVal.add("Nathaniel Borenstein <*****@*****.**>");
    // From: Nathaniel Borenstein <*****@*****.**>
    hdrs.put("From", fromVal);
    final List<String> mimeVal = new ArrayList<>();
    mimeVal.add("1.0");
    final List<String> toVal = new ArrayList<>();
    toVal.add("Ned Freed <*****@*****.**>");
    // To: Ned Freed <*****@*****.**>
    hdrs.put("To", toVal);
    // MIME-Version: 1.0
    hdrs.put("MIME-version", mimeVal);
    final List<String> subjVal = new ArrayList<>();
    subjVal.add("Formatted text mail");
    // Subject: Formatted text mail
    hdrs.put("Subject", subjVal);
    final List<String> contTypeVal = new ArrayList<>();
    contTypeVal.add("multipart/mixed");
    contTypeVal.add("boundary=\"simple boundary\"");
    // Content-Type: multipart/alternative; boundary=boundary42
    hdrs.put("Content-Type", contTypeVal);
    conn.headers = hdrs;
    conn.contentType = "multipart/mixed; boundary=\"simple boundary\"";
    // Response to be read
    conn.content =
        token(
            "This is the preamble.  "
                + "It is to be ignored, though it"
                + NL
                + "is a handy place for mail composers to include an"
                + CRLF
                + "explanatory note to non-MIME compliant readers."
                + CRLF
                + "--simple boundary"
                + CRLF
                + CRLF
                + "This is implicitly typed plain ASCII text."
                + CRLF
                + "It does NOT end with a linebreak."
                + CRLF
                + "--simple boundary"
                + CRLF
                + "Content-type: text/plain; charset=us-ascii"
                + CRLF
                + CRLF
                + "This is explicitly typed plain ASCII text."
                + CRLF
                + "It DOES end with a linebreak."
                + CRLF
                + CRLF
                + "--simple boundary--"
                + CRLF
                + "This is the epilogue.  It is also to be ignored.");
    // Get response as sequence of XQuery items
    final ItemList returned = new HttpResponse(null, ctx.options).getResponse(conn, true, null);

    // Construct expected result
    final ItemList expected = new ItemList();
    final String response =
        "<http:response "
            + "xmlns:http='http://expath.org/ns/http-client' "
            + "status='200' message='OK'>"
            + "<http:header name='Subject' value='Formatted text mail'/>"
            + "<http:header name='To' value='Ned "
            + "Freed &lt;[email protected]&gt;'/>"
            + "<http:header name='Content-Type' value='multipart/mixed;"
            + "boundary=&quot;simple boundary&quot;'/>"
            + "<http:header name='MIME-version' value='1.0'/>"
            + "<http:header name='From' value='Nathaniel Borenstein "
            + "&lt;[email protected]&gt;'/>"
            + "<http:multipart media-type='multipart/mixed' "
            + "boundary='simple boundary'>"
            + "<http:body media-type='text/plain'/>"
            + "<http:header name='Content-type' value='text/plain; "
            + "charset=us-ascii'/>"
            + "<http:body media-type='text/plain; charset=us-ascii'/>"
            + "</http:multipart>"
            + "</http:response>";
    expected.add(new DBNode(new IOContent(response)).children().next());
    expected.add(
        Str.get(
            "This is implicitly typed plain ASCII text.\n"
                + "It does NOT end with a linebreak.\n"));
    expected.add(
        Str.get(
            "This is explicitly typed plain ASCII text.\n" + "It DOES end with a linebreak.\n\n"));

    compare(expected, returned);
  }
示例#5
0
  /**
   * Tests ResponseHandler.getResponse() with multipart response.
   *
   * @throws IOException I/O Exception
   * @throws Exception exception
   */
  @Test
  public void multipartResponse() throws Exception {
    // Create fake HTTP connection
    final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com"));
    final Map<String, List<String>> hdrs = new HashMap<>();
    final List<String> fromVal = new ArrayList<>();
    fromVal.add("Nathaniel Borenstein <*****@*****.**>");
    // From: Nathaniel Borenstein <*****@*****.**>
    hdrs.put("From", fromVal);
    final List<String> mimeVal = new ArrayList<>();
    mimeVal.add("1.0");
    // MIME-Version: 1.0
    hdrs.put("MIME-version", mimeVal);
    final List<String> subjVal = new ArrayList<>();
    subjVal.add("Formatted text mail");
    // Subject: Formatted text mail
    hdrs.put("Subject", subjVal);
    final List<String> contTypeVal = new ArrayList<>();
    contTypeVal.add("multipart/alternative");
    contTypeVal.add("boundary=\"boundary42\"");
    // Content-Type: multipart/alternative; boundary=boundary42
    hdrs.put("Content-Type", contTypeVal);

    conn.headers = hdrs;
    conn.contentType = "multipart/alternative; boundary=\"boundary42\"";
    conn.content =
        token(
            "--boundary42"
                + CRLF
                + "Content-Type: text/plain; charset=us-ascii"
                + CRLF
                + CRLF
                + "...plain text...."
                + CRLF
                + CRLF
                + "--boundary42"
                + CRLF
                + "Content-Type: text/richtext"
                + CRLF
                + CRLF
                + ".... richtext..."
                + CRLF
                + "--boundary42"
                + CRLF
                + "Content-Type: text/x-whatever"
                + CRLF
                + CRLF
                + ".... fanciest formatted version  "
                + CRLF
                + "..."
                + CRLF
                + "--boundary42--");
    final ItemList returned = new HttpResponse(null, ctx.options).getResponse(conn, true, null);

    // Construct expected result
    final ItemList expected = new ItemList();
    final String response =
        "<http:response "
            + "xmlns:http='http://expath.org/ns/http-client' "
            + "status='200' message='OK'>"
            + "<http:header name='Subject' value='Formatted text mail'/>"
            + "<http:header name='Content-Type' "
            + "value='multipart/alternative;boundary=&quot;boundary42&quot;'/>"
            + "<http:header name='MIME-version' value='1.0'/>"
            + "<http:header name='From' value='Nathaniel Borenstein "
            + "&lt;[email protected]&gt;'/>"
            + "<http:multipart media-type='multipart/alternative' "
            + "boundary='boundary42'>"
            + "<http:header name='Content-Type' "
            + "value='text/plain; charset=us-ascii'/>"
            + "<http:body media-type='text/plain; charset=us-ascii'/>"
            + "<http:header name='Content-Type' value='text/richtext'/>"
            + "<http:body media-type='text/richtext'/>"
            + "<http:header name='Content-Type' value='text/x-whatever'/>"
            + "<http:body media-type='text/x-whatever'/>"
            + "</http:multipart>"
            + "</http:response> ";
    expected.add(new DBNode(new IOContent(response)).children().next());
    expected.add(Str.get("...plain text....\n\n"));
    expected.add(Str.get(".... richtext...\n"));
    expected.add(Str.get(".... fanciest formatted version  \n...\n"));
    compare(expected, returned);
  }