Example #1
0
 /**
  * Tests response handling with specified charset in the header 'Content-Type'.
  *
  * @throws IOException I/O Exception
  * @throws QueryException query exception
  */
 @Test
 public void responseWithCharset() throws IOException, QueryException {
   // Create fake HTTP connection
   final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com"));
   // Set content type
   conn.contentType = "text/plain; charset=CP1251";
   // set content encoded in CP1251
   final String test = "\u0442\u0435\u0441\u0442";
   conn.content = Charset.forName("CP1251").encode(test).array();
   final ItemList res = new HttpResponse(null, ctx.options).getResponse(conn, true, null);
   // compare results
   assertEquals(test, string(res.get(1).string(null)));
 }
Example #2
0
  /**
   * Tests nested multipart responses.
   *
   * @throws Exception exception
   */
  @Test
  public void nestedMultipart() throws Exception {
    // Create fake HTTP connection
    final String boundary = "batchresponse_4c4c5223-efa7-4aba-9865-fb4cb102cfd2";

    final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com"));
    final Map<String, List<String>> hdrs = new HashMap<>();
    final List<String> contTypeVal = new ArrayList<>();
    contTypeVal.add("multipart/mixed");
    contTypeVal.add("boundary=\"" + boundary + "\"");
    hdrs.put("Content-Type", contTypeVal);

    conn.headers = hdrs;
    conn.contentType = "multipart/alternative; boundary=\"" + boundary + "\"";
    conn.content = new IOFile("src/test/resources/response.txt").read();

    new HttpResponse(null, ctx.options).getResponse(conn, true, null);
  }
Example #3
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);
  }
Example #4
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);
  }