Search in sources :

Example 6 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class MockMvcStaticRequestAndResponseSpecTest method response_validation_fails_if_any_property_in_the_response_is_not_valid.

@Test
public void response_validation_fails_if_any_property_in_the_response_is_not_valid() {
    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").then().body("id", equalTo(1));
    } 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 7 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController 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 8 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class RequestLoggingTest method logging_query_param_works.

@Test
public void logging_query_param_works() {
    RestAssuredMockMvc.given().log().all().standaloneSetup(new GreetingController()).queryParam("name", "Johan").when().get("/greeting").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan!"));
    assertThat(writer.toString(), equalTo("Request method:\tGET\nRequest URI:\thttp://localhost:8080/greeting?name=Johan\nProxy:\t\t\t<none>\nRequest params:\t<none>\nQuery params:\tname=Johan\nForm params:\t<none>\nPath params:\t<none>\nHeaders:\t\t<none>\nCookies:\t\t<none>\nMultiparts:\t\t<none>\nBody:\t\t\t<none>\n"));
}
Also used : GreetingController(io.restassured.module.mockmvc.http.GreetingController) Test(org.junit.Test)

Example 9 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method logging_is_not_overwritten_when_not_defined_in_specification.

@Test
public void logging_is_not_overwritten_when_not_defined_in_specification() {
    // Given
    StringWriter writer = new StringWriter();
    PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().setConfig(RestAssuredMockMvcConfig.newConfig().logConfig(LogConfig.logConfig().defaultStream(captor))).addQueryParam("name", "Johan").build();
    // When
    RestAssuredMockMvc.given().spec(specToMerge).log().params().standaloneSetup(new GreetingController()).when().get("/greeting").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan!"));
    // Then
    assertThat(writer.toString()).isEqualTo("Request params:\t<none>\n" + "Query params:\tname=Johan\n" + "Form params:\t<none>\n" + "Path params:\t<none>\n" + "Multiparts:\t\t<none>\n");
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) PrintStream(java.io.PrintStream) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) StringWriter(java.io.StringWriter) GreetingController(io.restassured.module.mockmvc.http.GreetingController) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Test(org.junit.Test)

Example 10 with GreetingController

use of io.restassured.module.mockmvc.http.GreetingController in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method logging_is_overwritten_when_defined_in_specification.

@Test
public void logging_is_overwritten_when_defined_in_specification() {
    // Given
    StringWriter writer = new StringWriter();
    PrintStream captor = new PrintStream(new WriterOutputStream(writer), true);
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().setConfig(RestAssuredMockMvcConfig.newConfig().logConfig(LogConfig.logConfig().defaultStream(captor))).and().log(LogDetail.ALL).build();
    // When
    RestAssuredMockMvc.given().log().params().spec(specToMerge).standaloneSetup(new GreetingController()).when().get("/greeting?name={name}", "Johan").then().body("id", equalTo(1)).body("content", equalTo("Hello, Johan!"));
    // Then
    assertThat(writer.toString()).isEqualTo("Request method:\tGET\n" + "Request URI:\thttp://localhost:8080/greeting?name=Johan\n" + "Proxy:\t\t\t<none>\n" + "Request params:\t<none>\n" + "Query params:\t<none>\n" + "Form params:\t<none>\n" + "Path params:\t<none>\n" + "Headers:\t\t<none>\n" + "Cookies:\t\t<none>\n" + "Multiparts:\t\t<none>\n" + "Body:\t\t\t<none>\n");
}
Also used : MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) PrintStream(java.io.PrintStream) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) StringWriter(java.io.StringWriter) GreetingController(io.restassured.module.mockmvc.http.GreetingController) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) Test(org.junit.Test)

Aggregations

GreetingController (io.restassured.module.mockmvc.http.GreetingController)19 Test (org.junit.Test)19 MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)7 MockMvc (org.springframework.test.web.servlet.MockMvc)5 MockHttpServletRequestBuilderInterceptor (io.restassured.module.mockmvc.intercept.MockHttpServletRequestBuilderInterceptor)4 MockMvcRequestSpecification (io.restassured.module.mockmvc.specification.MockMvcRequestSpecification)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)4 MultiValueMap (org.springframework.util.MultiValueMap)4 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)3 PrintStream (java.io.PrintStream)2 StringWriter (java.io.StringWriter)2 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)2 PostController (io.restassured.module.mockmvc.http.PostController)1 MockMvcResponse (io.restassured.module.mockmvc.response.MockMvcResponse)1 JsonPath (io.restassured.path.json.JsonPath)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1