2011-08-17 96 views
6

我想測試我的web服務安全爲以下:測試REST風格的JSON Grails的web服務

  • urlMapping中正確的,所以有以下服務可用與否?
  • 測試GET/POST/PUT/DELETE和登錄時,他們呈現的反饋以及錯誤
  • 測試錯誤消息和

未登錄有人可以給我一些提示如何做到這一點?我不知道如何訪問grails安全服務,以及在登錄和不登錄時對我的控制器運行測試。還有,我需要一些模擬服務器或其他東西來測試我的控制器或?

對不起,我是很新的這個話題,但我想在我的web服務失去控制之前在正確的方向走。

謝謝你的幫助!

回答

7

我們使用REST Client插件與functional testing插件一起測試所有的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) 
    } 
} 
0

我有類似的代碼,它使用內置的(常規1.8)JsonSlurper,我認爲可能是更可靠的,只需要在功能測試插件,但不是REST客戶端插件。

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**)