Search in sources :

Example 11 with ResponseSpecification

use of io.restassured.specification.ResponseSpecification in project knox by apache.

the class GatewayBasicFuncTest method getYarnRmProxyData.

private void getYarnRmProxyData(String encryptedQuery, String path, String resource, ContentType contentType, Map<String, Matcher<?>> contentMatchers) throws Exception {
    String username = "hdfs";
    String password = "hdfs-password";
    String gatewayPath = driver.getUrl("RESOURCEMANAGER") + path + "?" + encryptedQuery + (driver.isUseGateway() ? "" : "&user.name=" + username);
    switch(contentType) {
        case JSON:
            resource += ".json";
            break;
        case XML:
            resource += ".xml";
            break;
        default:
            break;
    }
    driver.getMock("RESOURCEMANAGER").expect().method("GET").pathInfo(path).queryParam("user.name", username).respond().status(HttpStatus.SC_OK).content(driver.getResourceBytes(resource)).contentType(contentType.toString());
    ResponseSpecification responseSpecification = given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").then().statusCode(HttpStatus.SC_OK).contentType(contentType);
    if (contentMatchers != null) {
        for (Entry<String, Matcher<?>> matcher : contentMatchers.entrySet()) {
            responseSpecification.body(matcher.getKey(), matcher.getValue());
        }
    }
    Response response = responseSpecification.when().get(gatewayPath);
    if (contentMatchers == null || contentMatchers.isEmpty()) {
        switch(contentType) {
            case JSON:
                MatcherAssert.assertThat(response.getBody().asString(), sameJSONAs(driver.getResourceString(resource, UTF8)));
                break;
            case XML:
                MatcherAssert.assertThat(the(response.getBody().asString()), isEquivalentTo(the(driver.getResourceString(resource, UTF8))));
                break;
            default:
                break;
        }
    }
    driver.assertComplete();
}
Also used : Response(io.restassured.response.Response) HttpResponse(org.apache.http.HttpResponse) MockRequestMatcher(org.apache.knox.test.mock.MockRequestMatcher) Matcher(org.hamcrest.Matcher) ResponseSpecification(io.restassured.specification.ResponseSpecification) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 12 with ResponseSpecification

use of io.restassured.specification.ResponseSpecification in project knox by apache.

the class GatewayBasicFuncTest method getYarnRmApp.

private void getYarnRmApp(ContentType contentType, boolean running) throws Exception {
    String username = "hdfs";
    String password = "hdfs-password";
    String path = "/v1/cluster/apps/application_1399541193872_0033/";
    String resource;
    if (running) {
        resource = "/yarn/app_running";
    } else {
        resource = "/yarn/app_succeeded";
    }
    switch(contentType) {
        case JSON:
            resource += ".json";
            break;
        case XML:
            resource += ".xml";
            break;
        default:
            break;
    }
    String gatewayPath = driver.getUrl("RESOURCEMANAGER") + path + (driver.isUseGateway() ? "" : "?user.name=" + username);
    InetSocketAddress gatewayAddress = driver.gateway.getAddresses()[0];
    String gatewayHostName = gatewayAddress.getHostName();
    String gatewayAddrName = InetAddress.getByName(gatewayHostName).getHostAddress();
    VelocityEngine velocity = new VelocityEngine();
    velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
    velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    velocity.init();
    VelocityContext context = new VelocityContext();
    context.put("proxy_address", driver.getRealUrl("RESOURCEMANAGER"));
    String name = TestUtils.getResourceName(this.getClass(), resource);
    Template template = velocity.getTemplate(name);
    StringWriter sw = new StringWriter();
    template.merge(context, sw);
    String request = sw.toString();
    driver.getMock("RESOURCEMANAGER").expect().method("GET").pathInfo(path).queryParam("user.name", username).respond().status(HttpStatus.SC_OK).content(request.getBytes()).contentType(contentType.toString());
    ResponseSpecification response = given().auth().preemptive().basic(username, password).header("X-XSRF-Header", "jksdhfkhdsf").then().statusCode(HttpStatus.SC_OK).contentType(contentType);
    if (running) {
        response.body("app.trackingUrl", anyOf(startsWith("http://" + gatewayHostName + ":" + gatewayAddress.getPort() + "/"), startsWith("http://" + gatewayAddrName + ":" + gatewayAddress.getPort() + "/")));
    } else {
        response.body("app.trackingUrl", isEmptyString());
    }
    response.body("app.amContainerLogs", isEmptyString()).body("app.amHostHttpAddress", isEmptyString()).when().get(gatewayPath);
    driver.assertComplete();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) InetSocketAddress(java.net.InetSocketAddress) VelocityContext(org.apache.velocity.VelocityContext) ResponseSpecification(io.restassured.specification.ResponseSpecification) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) Template(org.apache.velocity.Template)

Example 13 with ResponseSpecification

use of io.restassured.specification.ResponseSpecification in project rest-assured by rest-assured.

the class SpecificationBuilderITest method expectingSpecMergesTheCurrentSpecificationWithTheSuppliedOne.

@Test
public void expectingSpecMergesTheCurrentSpecificationWithTheSuppliedOne() throws Exception {
    final ResponseSpecBuilder builder = new ResponseSpecBuilder();
    builder.expectBody("store.book.size()", is(4)).expectStatusCode(200);
    final ResponseSpecification responseSpecification = builder.build();
    expect().spec(responseSpecification).body("store.book[0].author", equalTo("Nigel Rees")).when().get("/jsonStore");
}
Also used : ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) ResponseSpecification(io.restassured.specification.ResponseSpecification) Test(org.junit.Test)

Example 14 with ResponseSpecification

use of io.restassured.specification.ResponseSpecification in project rest-assured by rest-assured.

the class SpecificationBuilderITest method responseSpecificationCanExpectContentWithArgs.

@Test
public void responseSpecificationCanExpectContentWithArgs() throws Exception {
    final ResponseSpecification spec = new ResponseSpecBuilder().rootPath("store.book[%d]").expectContent("author", withArgs(0), equalTo("Nigel Rees")).build();
    expect().spec(spec).content("title", withArgs(1), equalTo("Sword of Honour")).when().get("/jsonStore");
}
Also used : ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) ResponseSpecification(io.restassured.specification.ResponseSpecification) Test(org.junit.Test)

Example 15 with ResponseSpecification

use of io.restassured.specification.ResponseSpecification in project rest-assured by rest-assured.

the class SpecificationBuilderITest method responseSpecificationCanExpectBodyWithArgs.

@Test
public void responseSpecificationCanExpectBodyWithArgs() throws Exception {
    final ResponseSpecification spec = new ResponseSpecBuilder().rootPath("store.book[%d]").expectBody("author", withArgs(0), equalTo("Nigel Rees")).build();
    expect().spec(spec).body("title", withArgs(1), equalTo("Sword of Honour")).when().get("/jsonStore");
}
Also used : ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) ResponseSpecification(io.restassured.specification.ResponseSpecification) Test(org.junit.Test)

Aggregations

ResponseSpecification (io.restassured.specification.ResponseSpecification)23 Test (org.junit.Test)20 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)14 RequestSpecification (io.restassured.specification.RequestSpecification)5 Matchers.containsString (org.hamcrest.Matchers.containsString)2 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)2 Cookies (io.restassured.http.Cookies)1 Response (io.restassured.response.Response)1 StringWriter (java.io.StringWriter)1 InetSocketAddress (java.net.InetSocketAddress)1 HttpResponse (org.apache.http.HttpResponse)1 MockRequestMatcher (org.apache.knox.test.mock.MockRequestMatcher)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 VelocityEngine (org.apache.velocity.app.VelocityEngine)1 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)1 Matcher (org.hamcrest.Matcher)1