Exemple #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());
  }
Exemple #2
0
  /**
   * Tests writing of body content when @method is raw and output is xs:hexBinary.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void writeHex() throws IOException {
    // Case 1: content is xs:hexBinary
    final HttpRequest req1 = new HttpRequest();
    req1.payloadAttrs.put("method", SerialMethod.BASEX.toString());
    req1.bodyContent.add(new Hex(token("test")));
    final FakeHttpConnection fakeConn1 = new FakeHttpConnection(new URL("http://www.test.com"));
    HttpClient.setRequestContent(fakeConn1.getOutputStream(), req1);
    assertEquals(fakeConn1.out.toString(Strings.UTF8), "test");

    // Case 2: content is a node
    final HttpRequest req2 = new HttpRequest();
    req2.payloadAttrs.put("method", SerialMethod.BASEX.toString());
    final FElem e3 = new FElem("a").add("test");
    req2.bodyContent.add(e3);
    final FakeHttpConnection fakeConn2 = new FakeHttpConnection(new URL("http://www.test.com"));
    HttpClient.setRequestContent(fakeConn2.getOutputStream(), req2);
    assertEquals(fakeConn2.out.toString(), "<a>test</a>");
  }
Exemple #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());
  }
Exemple #4
0
  /**
   * Tests writing of request content when @src is set.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void writeFromResource() throws IOException {
    // Create a file form which will be read
    final IOFile file = new IOFile(Prop.TMP, Util.className(FnHttpTest.class));
    file.write(token("test"));

    // Request
    final HttpRequest req = new HttpRequest();
    req.payloadAttrs.put("src", file.url());
    req.payloadAttrs.put("method", "binary");
    // HTTP connection
    final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com"));
    HttpClient.setRequestContent(fakeConn.getOutputStream(), req);

    // Delete file
    file.delete();

    assertEquals(fakeConn.out.toString(Strings.UTF8), "test");
  }