vendredi 27 mars 2015

Grails 2.2 functional test for a remote web service that requires JSON content in request

I am using Grails 2.2 and am writing a functional test for validating the JSON response received from a remote web service. The web service requires some JSON content to be passed in the request.


When I test this web service using a tool like "Postman", I receive the correct JSON response back.


Remote Web Service POST Request With JSON


I have written a functional test case in Grails 2.2 for this. Snippet of my sample code is provided below.





import com.grailsrocks.functionaltest.*
import com.grailsrocks.functionaltest.client.APIClient

class RemoteWebServiceFunctionalTests extends TestCaseBase{

void testPost() {
post("http://remoteAddress:8080/services/getData", {
param1 = "value1"
param2 = "value2"
...
...
param3 = "value3"
})

assertStatus 200
}

@Override
Class getDefaultClientType() {
APIClient
}

}



The output of running the test case is provided below for reference.





|Running 1 functional test... 1 of 1
--Output from testPost--
Switching to browser client [default]
Creating to new client [default] of type [class com.grailsrocks.functionaltest.client.APIClient]

== Making request POST http://remoteAddress:8080/services/getData parameters: ==
param1: value1
param2: value2
...
...
param3: value3

== Request headers: ============================================================
== Content =====================================================================

================================================================================
== Response was 200 (OK) headers: ==============================================
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 148
Date: Fri, 27 Mar 2015 17:36:33 GMT
================================================================================
== Content =====================================================================
{"errors":[{"Error0001":"Invalid request. Please refer to API definition"}],"result":null,"warnings":null}
================================================================================



When I run the test, I do get the status 200 back but the correct JSON response content is not received. I think I am not formatting the request correctly. I need to know the correct way of supplying the JSON content to a remote web service call in a functional test case.


Any help in this regard is highly appreciated.


Aucun commentaire:

Enregistrer un commentaire