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;
}
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;
}
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()));
}
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();
}
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();
}
Aggregations