Search in sources :

Example 36 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class URLITest method fullyQualifiedLocalhostUrlEndingWithSlashDoesntAddsPort8080.

@Test
public void fullyQualifiedLocalhostUrlEndingWithSlashDoesntAddsPort8080() throws Exception {
    // Given
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    // When
    given().contentType(JSON).filter(new RequestLoggingFilter(captor)).filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
        }
    }).expect().statusCode(200).body(equalTo("changed")).when().get("http://localhost");
    // Then
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080"));
}
Also used : PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) Filter(io.restassured.filter.Filter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 37 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class URLITest method trailingSlashesAreRetainedWhenConfiguredStatically.

@Test
public void trailingSlashesAreRetainedWhenConfiguredStatically() throws Exception {
    // Given
    RestAssured.basePath = "/v1/";
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    // When
    try {
        given().filter(new RequestLoggingFilter(captor)).filter(new Filter() {

            public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
                return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
            }
        }).expect().statusCode(200).body(equalTo("changed")).when().get("/");
    } finally {
        RestAssured.reset();
    }
    // Then
    assertThat(loggedRequestPathIn(writer), equalTo("http://localhost:8080/v1/"));
}
Also used : PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) Filter(io.restassured.filter.Filter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 38 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class URLITest method doesntAddPort8080ToFullyQualifiedUrlDefinedInHttpVerbMethod.

@Test
public void doesntAddPort8080ToFullyQualifiedUrlDefinedInHttpVerbMethod() throws Exception {
    // Given
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    // When
    given().port(8080).contentType(JSON).filter(new RequestLoggingFilter(captor)).filter(new Filter() {

        public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
            return new ResponseBuilder().setStatusCode(200).setBody("changed").build();
        }
    }).expect().statusCode(200).body(equalTo("changed")).when().get("http://tototiti.alarmesomfy.net/");
    // Then
    assertThat(loggedRequestPathIn(writer), equalTo("http://tototiti.alarmesomfy.net:8080/"));
}
Also used : PrintStream(java.io.PrintStream) FilterableResponseSpecification(io.restassured.specification.FilterableResponseSpecification) StringWriter(java.io.StringWriter) Filter(io.restassured.filter.Filter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) FilterableRequestSpecification(io.restassured.specification.FilterableRequestSpecification) ResponseBuilder(io.restassured.builder.ResponseBuilder) FilterContext(io.restassured.filter.FilterContext) Test(org.junit.Test)

Example 39 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class ResponseITest method responseTakeCharsetIntoAccount.

@Test
public void responseTakeCharsetIntoAccount() throws Exception {
    ResponseBuilder b = new ResponseBuilder();
    b.setHeaders(new Headers());
    b.setBody(new ByteArrayInputStream("äöü".getBytes("UTF-8")));
    b.setStatusCode(200);
    b.setContentType("application/json;charset=UTF-8");
    final Response response = b.build();
    assertThat("äöü", equalTo(response.asString()));
}
Also used : Response(io.restassured.response.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) Headers(io.restassured.http.Headers) ResponseBuilder(io.restassured.builder.ResponseBuilder) Test(org.junit.Test)

Example 40 with ResponseBuilder

use of io.restassured.builder.ResponseBuilder in project rest-assured by rest-assured.

the class SpookyGreetJsonResponseFilter method filter.

public Response filter(FilterableRequestSpecification requestSpec, FilterableResponseSpecification responseSpec, FilterContext ctx) {
    final Response response = ctx.next(requestSpec, responseSpec);
    final String responseAsString = response.asString();
    final JsonPath jsonPath = new JsonPath(responseAsString);
    final String firstName = jsonPath.getString("greeting.firstName");
    final String updatedResponse = responseAsString.replace(firstName, "Spooky");
    return new ResponseBuilder().clone(response).setBody(updatedResponse).build();
}
Also used : Response(io.restassured.response.Response) JsonPath(io.restassured.path.json.JsonPath) ResponseBuilder(io.restassured.builder.ResponseBuilder)

Aggregations

ResponseBuilder (io.restassured.builder.ResponseBuilder)49 Test (org.junit.Test)47 Filter (io.restassured.filter.Filter)40 FilterContext (io.restassured.filter.FilterContext)40 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)40 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)40 PrintStream (java.io.PrintStream)36 StringWriter (java.io.StringWriter)36 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)36 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)32 Response (io.restassured.response.Response)11 LogConfig (io.restassured.config.LogConfig)4 Headers (io.restassured.http.Headers)4 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)3 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 HttpClient (org.apache.http.client.HttpClient)2 AuthenticationScheme (io.restassured.authentication.AuthenticationScheme)1 PreemptiveOAuth2HeaderScheme (io.restassured.authentication.PreemptiveOAuth2HeaderScheme)1 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)1 ErrorLoggingFilter (io.restassured.filter.log.ErrorLoggingFilter)1