测试RESTful JSON Grails Webservice

Gam*_*mbo 6 rest grails integration-testing

我想测试我的安全Web服务以下内容:

  • UrlMapping正确,以下服务是否可用?
  • 测试GET/POST/PUT/DELETE及其渲染的反馈和错误
  • 登录时测试错误消息但未登录

有人可以给我一些提示如何做到这一点?我不知道如何访问grails安全服务以及在登录时何时对我的控制器运行测试.我还需要一些模拟服务器或其他东西来测试我的控制器或?

对不起,我对这个话题很新,但我想在失去对webservices的控制权之前走向正确的方向.

谢谢您的帮助!

Gre*_*egg 7

我们使用REST Client插件和功能测试插件来测试我们所有的Web服务.

例如...

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)
    }
}
Run Code Online (Sandbox Code Playgroud)