sahaya / rest-assured

Automatically exported from code.google.com/p/rest-assured
0 stars 0 forks source link

MockMvcRequestSpecificationImpl#spec producing a different request specification than straight request setuo #340

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
GIVEN a request specification (rest-assured 2.3.2 with Groovy/Spock) like:

      def response = with().
                contentType(JSON).header(ACCEPT_JSON).
                body(resource.toPrettyString()).
                post('/{resourcePath}', resourcePath)

WHEN modifying the code such that we use a pre-built specification 

    void setup() {
        requestSpecbuilder = new MockMvcRequestSpecBuilder()
        requestSpecbuilder.setContentType(ContentType.JSON);
        requestSpecification = requestSpecbuilder.build()
        requestSpecification.header(ACCEPT_JSON)
    }

      def response = with().
                spec(requestSpecification).
                body(resource.toPrettyString()).
                post('/{resourcePath}', resourcePath)

THEN the code breaks

The request seems to be different as the execution now fails. The execution 
always fails when executed with other tests in the same test class (spock 
specification in this case) but runs fine when executed as a single test 
execution.

I have used the same approach using the with().spec() call in the regular (non 
MockMvc) environment where pretty much the same code runs fine.

Original issue reported on code.google.com by Martin.A...@gmail.com on 25 Jun 2014 at 1:18

GoogleCodeExporter commented 9 years ago
In what way does it break? The header will not be taken into account in your 
example since the specifications are immutable.

Original comment by johan.ha...@gmail.com on 29 Aug 2014 at 9:11

GoogleCodeExporter commented 9 years ago
I can't reproduce it. I've tried:

given().
        spec(new MockMvcRequestSpecBuilder().setContentType(JSON).addHeader("accept", JSON.toString()).build()).
        standaloneSetup(new GreetingController()).
        formParam("name", "Johan").
when().
        post("/greeting").
then().
        statusCode(200).
        body("id", equalTo(1)).
        body("content", equalTo("Hello, Johan!"));

And GreetingController looks like this:
@RequestMapping(value = "/greeting", method = POST, consumes = 
"application/json", produces = "application/json")
public @ResponseBody Greeting greetingWithRequiredContentType(
        @RequestParam(value="name", required=false, defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

And this works fine for me..

Original comment by johan.ha...@gmail.com on 29 Aug 2014 at 9:24

GoogleCodeExporter commented 9 years ago
Closing due to inactivity

Original comment by johan.ha...@gmail.com on 23 Sep 2014 at 9:08

GoogleCodeExporter commented 9 years ago
I have updated to the latest release 2.3.4 and have noticed that the problem 
has disappeared. Just wanted to post that in case anyone else is running into 
the same problem. However I have really never been able to reproduce that issue 
in an isolated test case.

Original comment by Martin.A...@gmail.com on 22 Oct 2014 at 8:27

GoogleCodeExporter commented 9 years ago
Thanks for the notification

Original comment by johan.ha...@gmail.com on 22 Oct 2014 at 10:00