Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test RESTful JSON Grails Webservice

I want to test my secured webservice to the following:

  • UrlMapping correct, so are the following services available or not?
  • Test GET/POST/PUT/DELETE and their rendered feedback as well as errors
  • Test error messages when logged in and not logged in

Can somebody give me some hints how to do this? I have no clue how accessing the grails security service and as well running tests against my controllers when logged in and when not. As well I need some Mock Server or something to test against my controllers or?

Sorry I am very new to this topic but I want to go in the right direction before loosing control over my webservices.

Thank you for your help!

like image 219
Gambo Avatar asked Aug 17 '11 13:08

Gambo


1 Answers

We use the REST Client plugin along with the functional testing plugin to test all our web services.

For example...

void testCreateTag() {
    def name = 'Test Name'
    def jsonText = """
         {
           "class":"Tag",
           "name":"${name}"
         }
      """

    post('/api/tag') {
      headers['x-user-external-id'] = securityUser.externalId
      headers['x-user-api-key'] = securityUser.apiKey
      headers['Content-type'] = 'application/json'
      body {
        jsonText
      }
    }

    def model = this.response.contentAsString
    def map = JSON.parse(model)

    assertNotNull(map.attributes.id)

    Tag.withNewSession {
      def tag = Tag.get(map.attributes.id)

      assertNotNull(tag)
      assertEquals(name, tag.name)
    }
}
like image 195
Gregg Avatar answered Nov 15 '22 09:11

Gregg