Search in sources :

Example 11 with WriterOutputStream

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

the class ReportServiceTest method testOutputToCsv_normal.

@Test
public void testOutputToCsv_normal() throws Exception {
    // Set the desired filters
    ReportColumnFilter columnFilter = new ReportColumnFilter();
    setAllTrue(columnFilter);
    // Test the reportService.outputToCsv method()
    StringWriter outputWriter = new StringWriter();
    reportService.outputToCsv(new WriterOutputStream(outputWriter), true, Locale.ENGLISH, configuration, columnFilter);
    // Verify the results
    String actual = outputWriter.toString().replaceAll("\r", "");
    String expected = IOUtils.toString(getClass().getResourceAsStream("allUserRecordsReport.csv")).replaceAll("\r", "");
    assertEqualsIgnoreLineEndings(actual, expected);
}
Also used : StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Test(org.junit.Test)

Example 12 with WriterOutputStream

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

the class LogIfValidationFailsITest method worksForResponseSpecificationsUsingLegacySyntax.

@Test
public void worksForResponseSpecificationsUsingLegacySyntax() throws Exception {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    try {
        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 Doe2")).when().get("/{firstName}/{lastName}");
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), equalTo("{\"firstName\":\"John\",\"lastName\":\"Doe\",\"fullName\":\"John Doe\"}" + 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 13 with WriterOutputStream

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

the class LogIfValidationFailsITest method configuringLogConfigToEnableLoggingOfRequestAndResponseIfValidationFailsWorksAsExpected.

@Test
public void configuringLogConfigToEnableLoggingOfRequestAndResponseIfValidationFailsWorksAsExpected() 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().statusCode(400);
        fail("Should throw AssertionError");
    } catch (AssertionError 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 14 with WriterOutputStream

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

the class LogIfValidationFailsITest method worksForRequestSpecificationsUsingGivenWhenThenSyntax.

@Test
public void worksForRequestSpecificationsUsingGivenWhenThenSyntax() 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))).log().ifValidationFails().param("firstName", "John").param("lastName", "Doe").header("Content-type", "application/json").when().get("/greet").then().statusCode(400);
        fail("Should throw AssertionError");
    } catch (AssertionError 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));
    }
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Test(org.junit.Test)

Example 15 with WriterOutputStream

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

the class LogIfValidationFailsITest method logging_of_both_request_and_response_validation_works_when_test_fails_when_using_static_response_and_request_specs_declared_before_enable_logging.

@Test
public void logging_of_both_request_and_response_validation_works_when_test_fails_when_using_static_response_and_request_specs_declared_before_enable_logging() {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    RestAssured.responseSpecification = new ResponseSpecBuilder().expectStatusCode(200).build();
    RestAssured.requestSpecification = new RequestSpecBuilder().setConfig(RestAssuredConfig.config().logConfig(new LogConfig(captor, true))).addHeader("Api-Key", "1234").build();
    RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(LogDetail.HEADERS);
    try {
        RestAssured.given().param("firstName", "John").param("lastName", "Doe").header("Content-type", "application/json").when().get("/greet").then().body("firstName", equalTo("Hello, Johan2!"));
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), equalTo("Headers:\t\t" + "Api-Key=1234" + LINE_SEPARATOR + "\t\t\t\tAccept=*/*" + LINE_SEPARATOR + "\t\t\t\tContent-Type=application/json; charset=" + RestAssured.config().getEncoderConfig().defaultCharsetForContentType(ContentType.JSON) + LINE_SEPARATOR + LINE_SEPARATOR + "Content-Type: application/json;charset=utf-8\nContent-Length: 33\nServer: Jetty(9.3.2.v20150730)\n"));
    }
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) 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