Search in sources :

Example 1 with RequestSpecification

use of io.restassured.specification.RequestSpecification in project IPK-BrAPI-Validator by plantbreeding.

the class TestItemRunner method connect.

/**
 * Connect to an endpoint and store the server response.
 *
 * @return server response
 */
private ValidatableResponse connect() {
    LOGGER.info("New Request. URL: " + this.url);
    RestAssured.useRelaxedHTTPSValidation();
    try {
        URL u = new URL(url);
        if ((Config.get("advancedMode") != null && Config.get("advancedMode").equals("true")) && u.getPort() != 80 && u.getPort() != -1) {
            throw new IllegalArgumentException();
        }
        RequestSpecification rs = given().contentType("application/json");
        List<Param> params = this.item.getParameters();
        if (this.method.equals("GET")) {
            if (params != null) {
                for (Param p : params) {
                    String value = RunnerService.replaceVariablesUrl(p.getValue(), this.variables);
                    rs.param(p.getParam(), value);
                }
            }
        } else if (this.method.equals("POST") || this.method.equals("PUT")) {
            ObjectNode bodyParams = (new ObjectMapper()).createObjectNode();
            if (params != null) {
                for (Param p : params) {
                    String value = RunnerService.replaceVariablesUrl(p.getValue(), this.variables);
                    bodyParams.put(p.getParam(), value);
                }
            }
            rs.body(bodyParams.toString());
        }
        ValidatableResponse vr = rs.request(this.method, this.url).then();
        return vr;
    } catch (AssertionError e) {
        LOGGER.info("Connection error");
        LOGGER.info("== cause ==");
        LOGGER.info(e.getMessage());
    } catch (IllegalArgumentException e) {
        LOGGER.info("Connection error. Invalid port");
        LOGGER.info("== cause ==");
        LOGGER.info(e.getMessage());
    } catch (Exception e) {
        if (e.getClass().equals(SSLHandshakeException.class)) {
            LOGGER.info("Connection error");
            LOGGER.info("== cause ==");
            LOGGER.info(e.getMessage());
        }
    }
    return null;
}
Also used : ValidatableResponse(io.restassured.response.ValidatableResponse) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RequestSpecification(io.restassured.specification.RequestSpecification) Param(de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.config.Param) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) IOException(java.io.IOException) ConnectionClosedException(org.apache.http.ConnectionClosedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 2 with RequestSpecification

use of io.restassured.specification.RequestSpecification in project devonfw-testing by devonfw.

the class XXssProtectionTest method testHeader.

@Test
@Parameters(method = "addParameters")
public void testHeader(SessionEnum session, EnvironmentParam origin, SubUrlEnum path) {
    RequestSpecification rs = getSessionManager().initBuilder(session).setBaseUri(origin.getValue()).setBasePath(path.getValue()).build();
    given(rs).when().get().then().statusCode(200).header("X-XSS-Protection", "1; mode=block");
}
Also used : RequestSpecification(io.restassured.specification.RequestSpecification) Parameters(junitparams.Parameters) Test(org.junit.Test) SecurityTest(com.capgemini.ntc.security.SecurityTest)

Example 3 with RequestSpecification

use of io.restassured.specification.RequestSpecification in project devonfw-testing by devonfw.

the class AuthRequiredTest method testHeader.

@Test
@Parameters(method = "addParameters")
public void testHeader(SessionEnum session, EnvironmentParam origin, SubUrlEnum path, String body, int statusCode) {
    RequestSpecification rs = getSessionManager().initBuilder(session).setBaseUri(origin.getValue()).setBasePath(path.getValue()).addHeader("Content-Type", "application/json").setBody(body).build();
    given(rs).when().request(Method.POST).then().statusCode(statusCode);
}
Also used : RequestSpecification(io.restassured.specification.RequestSpecification) Parameters(junitparams.Parameters) Test(org.junit.Test) SecurityTest(com.capgemini.ntc.security.SecurityTest)

Example 4 with RequestSpecification

use of io.restassured.specification.RequestSpecification in project devonfw-testing by devonfw.

the class DriverManager method closeDriver.

public static void closeDriver() {
    RequestSpecification driver = drivers.get();
    if (driver == null) {
        BFLogger.logDebug("closeDriver() was called but there was no driver for this thread.");
    } else {
        driver = null;
        drivers.remove();
    }
}
Also used : RequestSpecification(io.restassured.specification.RequestSpecification)

Example 5 with RequestSpecification

use of io.restassured.specification.RequestSpecification 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)

Aggregations

RequestSpecification (io.restassured.specification.RequestSpecification)55 Test (org.junit.Test)47 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)40 FilterableRequestSpecification (io.restassured.specification.FilterableRequestSpecification)7 SecurityTest (com.capgemini.ntc.security.SecurityTest)6 ResponseSpecification (io.restassured.specification.ResponseSpecification)5 Filter (io.restassured.filter.Filter)4 Parameters (junitparams.Parameters)4 FilterContext (io.restassured.filter.FilterContext)3 Header (io.restassured.http.Header)3 Response (io.restassured.response.Response)3 FilterableResponseSpecification (io.restassured.specification.FilterableResponseSpecification)3 PrintStream (java.io.PrintStream)3 StringWriter (java.io.StringWriter)3 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)3 RestDocumentationFilter (org.springframework.restdocs.restassured3.RestDocumentationFilter)3 Headers (io.restassured.http.Headers)2 List (java.util.List)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1