Search in sources :

Example 1 with Headers

use of io.restassured.http.Headers in project devonfw-testing by devonfw.

the class SessionManager method authenticateSession.

/**
 * Authenticates a single user session and stores the session related identifiers (headers) localy.
 *
 * @param session
 *          Session name.
 * @param user
 *          User name.
 * @param password
 *          User password.
 */
private void authenticateSession(SessionEnum session, EnvironmentParam user, EnvironmentParam password) {
    RestAssured.defaultParser = Parser.TEXT;
    JSONObject request = new JSONObject();
    request.put("username", user);
    request.put("password", password);
    RequestSpecification rs = new RequestSpecBuilder().setBody(request.toString()).setBaseUri(EnvironmentParam.SECURITY_SERVER_ORIGIN.getValue()).setBasePath(SubUrlEnum.LOGIN.getValue()).build();
    Headers headers = given(rs).when().post().getHeaders();
    if (!headers.hasHeaderWithName(AUTHORIZATION_HEADER)) {
        throw new RuntimeException("No authorization header found. " + "Expected a header 'Authorization' holding a Bearer token.");
    }
    Header authHeader = headers.get(AUTHORIZATION_HEADER);
    Headers authHeaders = new Headers(authHeader);
    authData.put(session, authHeaders);
}
Also used : JSONObject(org.json.JSONObject) Header(io.restassured.http.Header) Headers(io.restassured.http.Headers) RequestSpecification(io.restassured.specification.RequestSpecification) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder)

Example 2 with Headers

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

the class HeaderITest method requestSpecificationAllowsSpecifyingHeadersObject.

@Test
public void requestSpecificationAllowsSpecifyingHeadersObject() throws Exception {
    final Header header1 = new Header("MyHeader", "Something");
    final Header header2 = new Header("MyHeader", "Something else");
    final Headers headers = new Headers(header1, header2);
    final List<String> myHeaderValues = given().headers(headers).when().get("/multiHeaderReflect").headers().getValues("MyHeader");
    assertThat(myHeaderValues.size(), is(2));
    assertThat(myHeaderValues, hasItems("Something", "Something else"));
}
Also used : Header(io.restassured.http.Header) Headers(io.restassured.http.Headers) Test(org.junit.Test)

Example 3 with Headers

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

the class HeaderITest method orderIsMaintainedForMultiValueHeaders.

@Test
public void orderIsMaintainedForMultiValueHeaders() throws Exception {
    Headers headers = when().get("/multiValueHeader").headers();
    final List<String> headerListString = headers.getValues("MultiHeader");
    final String firstValue = headers.getValue("MultiHeader");
    final List<Header> headerListHeader = headers.getList("MultiHeader");
    assertThat(headerListString, contains("Value 1", "Value 2"));
    assertThat(headerListHeader, contains(new Header("MultiHeader", "Value 1"), new Header("MultiHeader", "Value 2")));
    assertThat(firstValue, equalTo("Value 2"));
}
Also used : Header(io.restassured.http.Header) Headers(io.restassured.http.Headers) Test(org.junit.Test)

Example 4 with Headers

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

the class RequestSpecMergingITest method mergesHeadersCorrectlyWhenOnlyStaticMerging.

@Test
public void mergesHeadersCorrectlyWhenOnlyStaticMerging() {
    given().filter((requestSpec, responseSpec, ctx) -> {
        Headers headers = requestSpec.getHeaders();
        assertThat(requestSpec.getContentType(), nullValue());
        assertThat(headers.getValue("authorization"), equalTo("abracadabra"));
        assertThat(headers.getValue("accept"), equalTo("*/*"));
        assertThat(headers.size(), is(2));
        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 5 with Headers

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

the class RequestPrinter method addHeaders.

private static void addHeaders(FilterableRequestSpecification requestSpec, StringBuilder builder) {
    builder.append("Headers:");
    final Headers headers = requestSpec.getHeaders();
    if (!headers.exist()) {
        appendTwoTabs(builder).append(NONE).append(NEW_LINE);
    } else {
        int i = 0;
        for (Header header : headers) {
            if (i++ == 0) {
                appendTwoTabs(builder);
            } else {
                appendFourTabs(builder);
            }
            builder.append(header).append(NEW_LINE);
        }
    }
}
Also used : Header(io.restassured.http.Header) Headers(io.restassured.http.Headers)

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