Example #1
0
  @Test
  public void loggingRequestAndResponseAtTheSameTimeWhenResponseFilterIsAddedBeforeRequestFilter()
      throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);

    final ScalatraObject object = new ScalatraObject();
    object.setHello("Hello world");
    given()
        .filters(new ResponseLoggingFilter(captor), new RequestLoggingFilter(captor))
        .body(object)
        .expect()
        .defaultParser(JSON)
        .when()
        .post("/reflect");

    assertThat(
        writer.toString(),
        equalTo(
            "Request method:\tPOST\nRequest path:\thttp://localhost:8080/reflect\nProxy:\t\t\t<none>\nRequest params:\t<none>\nQuery params:\t<none>\nForm params:\t<none>\nPath params:\t<none>\nMultiparts:\t\t<none>\nHeaders:\t\tAccept=*/*\n\t\t\t\tContent-Type=text/plain; charset="
                + RestAssured.config().getEncoderConfig().defaultContentCharset()
                + "\nCookies:\t\t<none>\nBody:\n{\"hello\":\"Hello world\"}"
                + LINE_SEPARATOR
                + "HTTP/1.1 200 OK\nContent-Type: text/plain; charset=iso-8859-1\nContent-Length: 23\nServer: Jetty(6.1.14)\n\n{\"hello\":\"Hello world\"}"
                + LINE_SEPARATOR));
  }
Example #2
0
  @Test
  public void loggingRequestFilterWithBody() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);

    final ScalatraObject object = new ScalatraObject();
    object.setHello("Hello world");
    given()
        .filter(new RequestLoggingFilter(captor))
        .expect()
        .defaultParser(JSON)
        .given()
        .body(object)
        .when()
        .post("/reflect");

    assertThat(
        writer.toString(),
        equalTo(
            "Request method:\tPOST\nRequest path:\thttp://localhost:8080/reflect\nRequest params:\t<none>\nQuery params:\t<none>\nForm params:\t<none>\nPath params:\t<none>\nHeaders:\t\tContent-Type=*/*\nCookies:\t\t<none>\nBody:\n{\"hello\":\"Hello world\"}"
                + LINE_SEPARATOR));
  }