RESTful JSON Grails Webservice Test

I want to check my secure web service for the following:

  • UrlMapping correct, are the following services available or not?
  • Testing GET / POST / PUT / DELETE and their displayed feedback, as well as errors
  • Check for error messages when logging in and not logging in

Can someone tell me how to do this? I don’t know how to access the Grails security service, and also run tests against my controllers when I log in, and when not. Also, do I need some kind of Mock Server or something to test against my controllers or?

Sorry I'm very new to this topic, but want to go in the right direction before losing control of my web services.

Thank you for your help!

+6
source share
2 answers

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

For instance...

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) } } 
+7
source

I have a similar code that uses the built-in (groovy 1.8) JsonSlurper, which I think can be more reliable and only need a functional testing plugin, but not a REST Client plugin.

  String baseUrlString = 'http://localhost:8080/**YOURAPP**' baseURL = baseUrlString post('/j_spring_security_check?') assertStatus 200 assertContentDoesNotContain('Access Denied') get("/*your test URL*/") def jsonObj = new JsonSlurper().parseText(this.response.contentAsString) assertEquals(jsonObj.your.object.model, **yourContent**) 
0
source

Source: https://habr.com/ru/post/895246/


All Articles