Search in sources :

Example 1 with RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder 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 RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder in project devonfw-testing by devonfw.

the class SessionManager method initBuilder.

/*
	 * (non-Javadoc)
	 * @see com.capgemini.ntc.security.ISessionManager#initBuilder(com.capgemini.ntc.security.SessionEnum)
	 */
@Override
public RequestSpecBuilder initBuilder(SessionEnum session) {
    Preconditions.checkNotNull(session);
    if (!authData.containsKey(session)) {
        return initBuilder();
    }
    RequestSpecBuilder result = new RequestSpecBuilder();
    for (Header header : authData.get(session).asList()) {
        result.addHeader(header.getName(), header.getValue());
    }
    return result;
}
Also used : Header(io.restassured.http.Header) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder)

Example 3 with RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder in project spring-cloud-netflix by spring-cloud.

the class AbstractDocumentationTests method spec.

private RequestSpecification spec(Object body, Filter... filters) {
    RequestSpecBuilder builder = new RequestSpecBuilder().addFilter(documentationConfiguration(this.restDocumentation).snippets().withAdditionalDefaults(new WireMockSnippet()));
    for (Filter filter : filters) {
        builder = builder.addFilter(filter);
    }
    RequestSpecification spec = builder.setPort(this.port).build();
    if (body != null) {
        spec.contentType("application/json").body(body, new EurekaObjectMapper());
    }
    return spec;
}
Also used : RestDocumentationFilter(org.springframework.restdocs.restassured3.RestDocumentationFilter) Filter(io.restassured.filter.Filter) RequestSpecification(io.restassured.specification.RequestSpecification) WireMockSnippet(org.springframework.cloud.contract.wiremock.restdocs.WireMockSnippet) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder)

Example 4 with RequestSpecBuilder

use of io.restassured.builder.RequestSpecBuilder 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)

Example 5 with RequestSpecBuilder

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

the class SpecificationBuilderITest method supportsSpecifyingQueryParametersInRequestSpecBuilderWhenPost.

@Test
public void supportsSpecifyingQueryParametersInRequestSpecBuilderWhenPost() throws Exception {
    final RequestSpecification spec = new RequestSpecBuilder().addQueryParameter("firstName", "John").addQueryParam("lastName", "Doe").build();
    given().spec(spec).expect().body("greeting.firstName", equalTo("John")).body("greeting.lastName", equalTo("Doe")).when().post("/greetXML");
}
Also used : RequestSpecification(io.restassured.specification.RequestSpecification) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) Test(org.junit.Test)

Aggregations

RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)44 RequestSpecification (io.restassured.specification.RequestSpecification)40 Test (org.junit.Test)39 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)7 Filter (io.restassured.filter.Filter)4 Header (io.restassured.http.Header)4 PrintStream (java.io.PrintStream)4 StringWriter (java.io.StringWriter)4 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)4 FilterContext (io.restassured.filter.FilterContext)3 Response (io.restassured.response.Response)3 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)3 Headers (io.restassured.http.Headers)2 List (java.util.List)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 SecurityTest (com.capgemini.ntc.security.SecurityTest)1 ResponseBuilder (io.restassured.builder.ResponseBuilder)1 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)1 LogConfig (io.restassured.config.LogConfig)1 CookieFilter (io.restassured.filter.cookie.CookieFilter)1