Search in sources :

Example 6 with Headers

use of io.restassured.http.Headers in project rest-assured by rest-assured.

the class ResponsePrinter method print.

/**
 * Prints the response to the print stream
 *
 * @return A string of representing the response
 */
public static String print(ResponseOptions responseOptions, ResponseBody responseBody, PrintStream stream, LogDetail logDetail, boolean shouldPrettyPrint) {
    final StringBuilder builder = new StringBuilder();
    if (logDetail == ALL || logDetail == STATUS) {
        builder.append(responseOptions.statusLine());
    }
    if (logDetail == ALL || logDetail == HEADERS) {
        final Headers headers = responseOptions.headers();
        if (headers.exist()) {
            appendNewLineIfAll(logDetail, builder).append(toString(headers));
        }
    } else if (logDetail == COOKIES) {
        final Cookies cookies = responseOptions.detailedCookies();
        if (cookies.exist()) {
            appendNewLineIfAll(logDetail, builder).append(cookies.toString());
        }
    }
    if (logDetail == ALL || logDetail == BODY) {
        String responseBodyToAppend;
        if (shouldPrettyPrint) {
            responseBodyToAppend = new Prettifier().getPrettifiedBodyIfPossible(responseOptions, responseBody);
        } else {
            responseBodyToAppend = responseBody.asString();
        }
        if (logDetail == ALL && !isBlank(responseBodyToAppend)) {
            builder.append("\n\n");
        }
        builder.append(responseBodyToAppend);
    }
    String response = builder.toString();
    stream.println(response);
    return response;
}
Also used : Headers(io.restassured.http.Headers) Cookies(io.restassured.http.Cookies) Prettifier(io.restassured.internal.support.Prettifier)

Example 7 with Headers

use of io.restassured.http.Headers in project rest-assured by rest-assured.

the class ResponseBuilder method setHeader.

/**
 * Set a specific header
 *
 * @return The builder
 */
public ResponseBuilder setHeader(String name, String value) {
    notNull(name, "Header name");
    notNull(value, "Header value");
    List<Header> newHeaders = new ArrayList<Header>(restAssuredResponse.headers().asList());
    newHeaders.add(new Header(name, value));
    restAssuredResponse.setResponseHeaders(new Headers(newHeaders));
    if (trim(name).equalsIgnoreCase(CONTENT_TYPE)) {
        restAssuredResponse.setContentType(value);
    }
    return this;
}
Also used : Header(io.restassured.http.Header) Headers(io.restassured.http.Headers) ArrayList(java.util.ArrayList)

Example 8 with Headers

use of io.restassured.http.Headers 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 9 with Headers

use of io.restassured.http.Headers in project rest-assured by rest-assured.

the class RequestSpecMergingITest method mergesHeadersCorrectlyWhenUsingGivenRequestSpec.

@Test
public void mergesHeadersCorrectlyWhenUsingGivenRequestSpec() {
    given(jsonRequest).filter((requestSpec, responseSpec, ctx) -> {
        Headers headers = requestSpec.getHeaders();
        assertThat(requestSpec.getContentType(), equalTo("application/json; charset=" + config().getEncoderConfig().defaultCharsetForContentType(JSON)));
        assertThat(headers.getValue("authorization"), equalTo("abracadabra"));
        assertThat(headers.getValue("accept"), equalTo("application/json+json"));
        assertThat(headers.getValue("content-type"), equalTo("application/json; charset=" + config().getEncoderConfig().defaultCharsetForContentType(JSON)));
        assertThat(headers.size(), is(3));
        return new ResponseBuilder().setStatusCode(200).build();
    }).when().get();
}
Also used : Headers(io.restassured.http.Headers) ResponseBuilder(io.restassured.builder.ResponseBuilder) Test(org.junit.Test)

Example 10 with Headers

use of io.restassured.http.Headers in project rest-assured by rest-assured.

the class RequestSpecMergingITest method mergesHeadersCorrectlyWhenUsingGivenSpecRequestSpec.

@Test
public void mergesHeadersCorrectlyWhenUsingGivenSpecRequestSpec() {
    given().spec(jsonRequest).filter((requestSpec, responseSpec, ctx) -> {
        Headers headers = requestSpec.getHeaders();
        assertThat(requestSpec.getContentType(), equalTo("application/json; charset=" + config().getEncoderConfig().defaultCharsetForContentType(JSON)));
        assertThat(headers.getValue("authorization"), equalTo("abracadabra"));
        assertThat(headers.getValue("accept"), equalTo("application/json+json"));
        assertThat(headers.getValue("content-type"), equalTo("application/json; charset=" + config().getEncoderConfig().defaultCharsetForContentType(JSON)));
        assertThat(headers.size(), is(3));
        return new ResponseBuilder().setStatusCode(200).build();
    }).when().get();
}
Also used : Headers(io.restassured.http.Headers) ResponseBuilder(io.restassured.builder.ResponseBuilder) Test(org.junit.Test)

Aggregations

Headers (io.restassured.http.Headers)12 Test (org.junit.Test)8 Header (io.restassured.http.Header)6 ResponseBuilder (io.restassured.builder.ResponseBuilder)4 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)2 Response (io.restassured.response.Response)2 RequestSpecification (io.restassured.specification.RequestSpecification)2 SecurityTest (com.capgemini.ntc.security.SecurityTest)1 RestAssuredClient (guru.nidi.ramltester.restassured3.RestAssuredClient)1 Cookies (io.restassured.http.Cookies)1 Prettifier (io.restassured.internal.support.Prettifier)1 ValidatableResponse (io.restassured.response.ValidatableResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 JSONObject (org.json.JSONObject)1