Search in sources :

Example 11 with ResponseSpecBuilder

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

the class MockMvcStaticRequestAndResponseSpecTest method response_validation_kicks_in_even_when_no_then_clause_is_specified.

@Test
public void response_validation_kicks_in_even_when_no_then_clause_is_specified() {
    RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder().addQueryParam("name", "Johan").build();
    RestAssuredMockMvc.responseSpecification = new ResponseSpecBuilder().expectStatusCode(200).expectBody("content", equalTo("Hello, John!")).build();
    exception.expect(AssertionError.class);
    exception.expectMessage("1 expectation failed.\n" + "JSON path content doesn't match.\n" + "Expected: Hello, John!\n" + "  Actual: Hello, Johan!");
    try {
        // When
        RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).when().get("/greeting");
    } finally {
        RestAssuredMockMvc.reset();
    }
}
Also used : MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) GreetingController(io.restassured.module.mockmvc.http.GreetingController) Test(org.junit.Test)

Example 12 with ResponseSpecBuilder

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

the class ParserTest method using_static_parser_its_possible_to_parse_unknown_content_types.

@Test
public void using_static_parser_its_possible_to_parse_unknown_content_types() {
    RestAssuredMockMvc.responseSpecification = new ResponseSpecBuilder().registerParser("some/thing", Parser.JSON).build();
    RestAssuredMockMvc.given().param("param", "my param").when().get("/parserWithUnknownContentType").then().statusCode(200).contentType(equalTo("some/thing")).body("param", equalTo("my param"));
}
Also used : ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) Test(org.junit.Test)

Example 13 with ResponseSpecBuilder

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

the class LoggingIfValidationFailsTest method logging_is_applied_when_using_non_static_response_specifications.

@Test
public void logging_is_applied_when_using_non_static_response_specifications() {
    RestAssuredMockMvc.config = new RestAssuredMockMvcConfig().logConfig(new LogConfig(captor, true).enableLoggingOfRequestAndResponseIfValidationFails());
    try {
        given().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().spec(new ResponseSpecBuilder().expectBody("id", equalTo(2)).expectBody("content", equalTo("Hello, Johan2!")).build());
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), not(isEmptyOrNullString()));
    }
}
Also used : RestAssuredMockMvcConfig(io.restassured.module.mockmvc.config.RestAssuredMockMvcConfig) PostController(io.restassured.module.mockmvc.http.PostController) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) LogConfig(io.restassured.config.LogConfig) Test(org.junit.Test)

Example 14 with ResponseSpecBuilder

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

the class LoggingIfValidationFailsTest 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);
    RestAssuredMockMvc.responseSpecification = new ResponseSpecBuilder().expectStatusCode(200).build();
    RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder().setConfig(config().logConfig(new LogConfig(captor, true))).addHeader("Api-Key", "1234").build();
    RestAssuredMockMvc.enableLoggingOfRequestAndResponseIfValidationFails(HEADERS);
    try {
        given().standaloneSetup(new PostController()).param("name", "Johan").when().post("/greetingPost").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan2!"));
        fail("Should throw AssertionError");
    } catch (AssertionError e) {
        assertThat(writer.toString(), equalTo("Headers:\t\tApi-Key=1234\n\t\t\t\tContent-Type=application/x-www-form-urlencoded;charset=" + RestAssuredMockMvcConfig.config().getEncoderConfig().defaultContentCharset() + "\n\nContent-Type: application/json;charset=UTF-8\n"));
    }
}
Also used : PrintStream(java.io.PrintStream) PostController(io.restassured.module.mockmvc.http.PostController) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) StringWriter(java.io.StringWriter) ResponseSpecBuilder(io.restassured.builder.ResponseSpecBuilder) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) LogConfig(io.restassured.config.LogConfig) Test(org.junit.Test)

Example 15 with ResponseSpecBuilder

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

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