Search code examples
playframeworkplayframework-2.0integration-testingfunctional-testingput

Play Framework PUT Request Fails


I am using Play Framework 2.2.3 to create a RESTful API and have some trouble with my tests.

I am running a TestServer to test all the routes. There's this weird problem that I suspect to be a bug. All PUT requests with a JSON payload to a route always fails.

FakeRequest fakeRequest = new FakeRequest(httpMethod, route);

if (jsonBody != null) {
    fakeRequest.withJsonBody(jsonBody);
}

Result result = route(fakeRequest); // <== result is 'null' for PUT with JSON body
assertEquals(status, status(result));

The above excerpt is the one that I use to test my routes (GET / POST / DELETE / PUT). Here are more points to consider.

  1. ONLY the PUT test requests fail. When I run the application I am able to send successful PUT requests using Advanced REST Client Chrome Plugin and receive the expected response.

  2. If there's not JSON body the request goes through.

Please let me know if this is a bug or if I'm doing something wrong. Thanks.


Solution

  • This question was asked a while back but I recently had the exact same problem.

    The method withJsonBody(JsValue json) automatically sets the http method to POST. To overcome this you can just add the http method as a second argument.

    In your example it looks like this:

    FakeRequest fakeRequest = new FakeRequest(httpMethod, route);
    
    if (jsonBody != null) {
        fakeRequest.withJsonBody(jsonBody, httpMethod);
    }
    
    Result result = route(fakeRequest); // <== result should now work for PUT with JSON body
    assertEquals(status, status(result));