Search in sources :

Example 1 with ResponseSpecBuilder

use of io.restassured.builder.ResponseSpecBuilder 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 2 with ResponseSpecBuilder

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

the class LogIfValidationFailsITest method logging_is_applied_when_using_non_static_response_specifications.

@Test
public void logging_is_applied_when_using_non_static_response_specifications() {
    final StringWriter writer = new StringWriter();
    final PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    try {
        RestAssured.given().config(RestAssured.config().logConfig(LogConfig.logConfig().defaultStream(captor).and().enableLoggingOfRequestAndResponseIfValidationFails())).param("firstName", "John").param("lastName", "Doe").when().get("/greet").then().spec(new ResponseSpecBuilder().expectStatusCode(400).build());
        fail("Test out to have failed by now");
    } catch (AssertionError e) {
        assertThat(writer.toString(), not(isEmptyOrNullString()));
    }
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Test(org.junit.Test)

Example 3 with ResponseSpecBuilder

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

the class SpecificationBuilderITest method mergesStaticallyDefinedResponseSpecificationsCorrectly.

@Test
public void mergesStaticallyDefinedResponseSpecificationsCorrectly() throws Exception {
    RestAssured.responseSpecification = new ResponseSpecBuilder().expectCookie("Cookie1", "Value1").build();
    ResponseSpecification reqSpec1 = new ResponseSpecBuilder().expectCookie("Cookie2", "Value2").build();
    ResponseSpecification reqSpec2 = new ResponseSpecBuilder().expectCookie("Cookie3", "Value3").build();
    try {
        Cookies cookies = given().cookie("Cookie1", "Value1").cookie("Cookie2", "Value2").when().get("/reflect").then().assertThat().spec(reqSpec1).extract().detailedCookies();
        assertThat(cookies.size(), is(2));
        assertThat(cookies.hasCookieWithName("Cookie1"), is(true));
        assertThat(cookies.hasCookieWithName("Cookie2"), is(true));
        cookies = given().cookie("Cookie1", "Value1").cookie("Cookie3", "Value3").when().get("/reflect").then().assertThat().spec(reqSpec2).extract().detailedCookies();
        assertThat(cookies.size(), is(2));
        assertThat(cookies.hasCookieWithName("Cookie1"), is(true));
        assertThat(cookies.hasCookieWithName("Cookie3"), is(true));
    } finally {
        RestAssured.reset();
    }
}
Also used : ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) ResponseSpecification(io.restassured.specification.ResponseSpecification) Cookies(io.restassured.http.Cookies) Test(org.junit.Test)

Example 4 with ResponseSpecBuilder

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

the class SpecificationBuilderITest method expectingSpecificationMergesTheCurrentSpecificationWithTheSuppliedOne.

@Test
public void expectingSpecificationMergesTheCurrentSpecificationWithTheSuppliedOne() throws Exception {
    final ResponseSpecBuilder builder = new ResponseSpecBuilder();
    builder.expectBody("store.book.size()", is(4)).expectStatusCode(200);
    final ResponseSpecification responseSpecification = builder.build();
    expect().specification(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 5 with ResponseSpecBuilder

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

the class GivenWhenThenResponseSpecITest method simple_given_when_then_works.

@Test
public void simple_given_when_then_works() {
    exception.expect(AssertionError.class);
    exception.expectMessage("2 expectations failed.\n" + "Expected status code <201> but was <200>.\n" + "\n" + "JSON path greeting doesn't match.\n" + "Expected: Greetings John Doo\n" + "  Actual: Greetings John Doe");
    ResponseSpecification specification = new ResponseSpecBuilder().expectStatusCode(201).expectBody("greeting", equalTo("Greetings John Doo")).build();
    given().param("firstName", "John").param("lastName", "Doe").when().get("/greet").then().spec(specification);
}
Also used : ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) ResponseSpecification(io.restassured.specification.ResponseSpecification) Test(org.junit.Test)

Aggregations

ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)22 Test (org.junit.Test)22 ResponseSpecification (io.restassured.specification.ResponseSpecification)14 MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)4 LogConfig (io.restassured.config.LogConfig)3 GreetingController (io.restassured.module.mockmvc.http.GreetingController)3 PrintStream (java.io.PrintStream)3 StringWriter (java.io.StringWriter)3 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)3 PostController (io.restassured.module.mockmvc.http.PostController)2 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)1 Cookies (io.restassured.http.Cookies)1 RestAssuredMockMvcConfig (io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig)1