Search in sources :

Example 16 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project rest-assured by rest-assured.

the class LogIfValidationFailsITest method doesntLogResponseSpecUsingLegacySyntaxWhenValidationSucceeds.

@Test
public void doesntLogResponseSpecUsingLegacySyntaxWhenValidationSucceeds() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    RestAssured.given().config(RestAssuredConfig.config().logConfig(new LogConfig(captor, false))).pathParam("firstName", "John").pathParam("lastName", "Doe").expect().log().ifValidationFails(LogDetail.BODY).body("fullName", equalTo("John Doe")).when().get("/{firstName}/{lastName}");
    assertThat(writer.toString(), isEmptyString());
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) LogConfig(io.restassured.config.LogConfig) Test(org.junit.Test)

Example 17 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project rest-assured by rest-assured.

the class LogIfValidationFailsITest method configuringLogConfigToEnableLoggingOfRequestAndResponseIfValidationFailsWorksAsExpected2.

@Test
public void configuringLogConfigToEnableLoggingOfRequestAndResponseIfValidationFailsWorksAsExpected2() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    try {
        RestAssured.given().config(RestAssured.config().logConfig(LogConfig.logConfig().defaultStream(captor).and().enableLoggingOfRequestAndResponseIfValidationFails())).param("firstName", "John").param("lastName", "Doe").header("Content-type", "application/json").when().get("/greet").then().body("room.size()", is(2));
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertThat(writer.toString(), equalTo("Request method:\tGET" + LINE_SEPARATOR + "Request URI:\thttp://localhost:8080/greet?firstName=John&lastName=Doe" + LINE_SEPARATOR + "Proxy:\t\t\t<none>" + LINE_SEPARATOR + "Request params:\tfirstName=John" + LINE_SEPARATOR + "\t\t\t\tlastName=Doe" + LINE_SEPARATOR + "Query params:\t<none>" + LINE_SEPARATOR + "Form params:\t<none>" + LINE_SEPARATOR + "Path params:\t<none>" + LINE_SEPARATOR + "Headers:\t\tAccept=*/*" + LINE_SEPARATOR + "\t\t\t\tContent-Type=application/json; charset=" + RestAssured.config().getEncoderConfig().defaultCharsetForContentType(ContentType.JSON) + LINE_SEPARATOR + "Cookies:\t\t<none>" + LINE_SEPARATOR + "Multiparts:\t\t<none>" + LINE_SEPARATOR + "Body:\t\t\t<none>" + LINE_SEPARATOR + LINE_SEPARATOR + "HTTP/1.1 200 OK" + LINE_SEPARATOR + "Content-Type: application/json;charset=utf-8" + LINE_SEPARATOR + "Content-Length: 33" + LINE_SEPARATOR + "Server: Jetty(9.3.2.v20150730)" + LINE_SEPARATOR + "" + LINE_SEPARATOR + "{" + LINE_SEPARATOR + "    \"greeting\": \"Greetings John Doe\"" + LINE_SEPARATOR + "}" + LINE_SEPARATOR + ""));
    }
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Test(org.junit.Test)

Example 18 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project rest-assured by rest-assured.

the class LogIfValidationFailsITest method logging_is_applied_when_using_non_static_response_specifications.

@Test
public void logging_is_applied_when_using_non_static_response_specifications() {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    try {
        RestAssured.given().config(RestAssured.config().logConfig(LogConfig.logConfig().defaultStream(captor).and().enableLoggingOfRequestAndResponseIfValidationFails())).param("firstName", "John").param("lastName", "Doe").when().get("/greet").then().spec(new ResponseSpecBuilder().expectStatusCode(400).build());
        fail("Test out to have failed by now");
    } catch (AssertionError e) {
        assertThat(writer.toString(), not(isEmptyOrNullString()));
    }
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Test(org.junit.Test)

Example 19 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project rest-assured by rest-assured.

the class LoggingITest method logOnlyResponseBodyWithNoPrettyPrintingUsingDSLWhenXml.

@Test
public void logOnlyResponseBodyWithNoPrettyPrintingUsingDSLWhenXml() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    given().config(config().logConfig(new LogConfig(captor, true))).expect().log().body(false).body("videos.music[0].title", is("Video Title 1")).when().get("/videos-not-formatted");
    assertThat(writer.toString(), equalTo("<videos><music><title>Video Title 1</title><artist>Artist 1</artist></music><music><title>Video Title 2</title><artist>Artist 2</artist><artist>Artist 3</artist></music></videos>" + LINE_SEPARATOR));
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) LogConfig(io.restassured.config.LogConfig) Test(org.junit.Test)

Example 20 with WriterOutputStream

use of org.apache.commons.io.output.WriterOutputStream in project rest-assured by rest-assured.

the class LoggingITest method logAllWithPrettyPrintingUsingDSLWhenJson.

@Test
public void logAllWithPrettyPrintingUsingDSLWhenJson() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    given().config(config().logConfig(new LogConfig(captor, false))).pathParam("firstName", "John").pathParam("lastName", "Doe").expect().log().all(true).body("fullName", equalTo("John Doe")).when().get("/{firstName}/{lastName}");
    assertThat(writer.toString(), equalTo("HTTP/1.1 200 OK\nContent-Type: application/json;charset=utf-8\nContent-Length: 59\nServer: Jetty(9.3.2.v20150730)\n\n{\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\",\n    \"fullName\": \"John Doe\"\n}" + LINE_SEPARATOR));
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) LogConfig(io.restassured.config.LogConfig) Test(org.junit.Test)

Aggregations

WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)149 StringWriter (java.io.StringWriter)142 PrintStream (java.io.PrintStream)137 Test (org.junit.Test)132 LogConfig (io.restassured.config.LogConfig)46 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)44 ResponseBuilder (io.restassured.builder.ResponseBuilder)36 Filter (io.restassured.filter.Filter)35 FilterContext (io.restassured.filter.FilterContext)35 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)35 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)35 IOException (java.io.IOException)6 Col (org.apache.karaf.shell.table.Col)6 ShellTable (org.apache.karaf.shell.table.ShellTable)6 OutputStream (java.io.OutputStream)5 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)4 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)4 InputStream (java.io.InputStream)4 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)3 ScalatraObject (io.restassured.itest.java.objects.ScalatraObject)3