带过滤器的Grails单元测试不返回内容

时间:2015-12-30 11:39:41

标签: unit-testing grails filter spock

所以我有这个过滤器

package filter.api

import grails.converters.JSON

class ApiFilters {

    def apiService

    def filters = {
        apiSafetyCheck(namespace: "api", controller: "customer|status", action: "*") {
        before = {
            if(!apiService?.checkIncludedApiKey(params)) { //Simply returns true if the apiKey was found in the database!
                def returnMap = [
                    "error": "API key was not found",
                    "statusCode": 401 //Just for testing!
                ]
                if(params.outputFormat && params.outputFormat ?.equals("xml")) {
                    def textToRender = apiService?.createXmlFromMapInstance(returnMap)
                    owner?.render(status: 401, contentType: "text/xml", text: textToRender)
                } else owner?.render(status: 401, contentType: "application/json", text: (returnMap as JSON))
                return false //We don't want the action to be processed!
            }
        }
    }
}

我的单元测试看起来像这样:

package api

import grails.test.mixin.Mock
import grails.test.mixin.support.GrailsUnitTestMixin
import grails.test.mixin.TestMixin
import grails.test.mixin.TestFor
import groovy.json.JsonSlurper
import groovy.util.XmlSlurper
import java.io.ByteArrayInputStream
import java.io.InputStream
import org.xml.sax.InputSource
import static javax.servlet.http.HttpServletResponse.*
import spock.lang.Specification

@TestMixin([GrailsUnitTestMixin])
@TestFor(BearbeitungController)
@Mock(filter.api.ApiFilters)
class ApiZugriffSpec extends Specification {

    void "Test API wihtout Api Key JSON"() {
        when:
            withFilters(action: "customer") {
                controller.someAction()
            }
        then:
            println "Response from Server " + response.text?.toString()
            println "Test 1 " + response?.getRedirectUrl()
            println "Test 2 " + response?.getRedirectedUrl()
            println "Test 3 " + response?.text
            println "Test 4 " + response.contentType
            def obj = new JsonSlurper().parseText(response.text?.toString())
            println "obj?.statusCode " + obj?.statusCode
            response.contentType == "application/json;charset=UTF-8"
            obj?.statusCode?.toString() == SC_UNAUTHORIZED.toString()
    }

    void "Test API wihtout Api Key XML"() {
        when:
            params.outputFormat = "xml"
            withFilters(action: "customer") {
                controller.someAction()
            }
        then:
            println "Response from Server " + response.text?.toString()
            println "Test 1 " + response?.getRedirectUrl()
            println "Test 2 " + response?.getRedirectedUrl()
            println "Test 3 " + response?.text
            println "Test 4 " + response.contentType
            def xmlSlurperInstance = new XmlSlurper()
            xmlSlurperInstance?.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false)
            xmlSlurperInstance?.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
            def inputStream = new ByteArrayInputStream(response.text?.toString()?.getBytes())
            def testXMLArray = xmlSlurperInstance?.parse(new InputSource(inputStream))
            response.contentType == "text/xml"
    }
}

在输出“测试3”时,没有任何内容可见,但这里应该是我从过滤器呈现的内容...如果我将过滤器编码重定向到名称空间“api”中的另一个控制器,它也是相同的响应“,它将处理渲染任务。

任何经历过类似事情的人或者可能知道解决方法以获得预期结果?

1 个答案:

答案 0 :(得分:0)

我已经解决了自己,我只从我的过滤器中删除了命名空间,以便过滤器看起来像这样:

package filter.api

import grails.converters.JSON

class ApiFilters {

    def apiService

    def filters = {
        apiSafetyCheck(controller: "customer|status", action: "*") {
        before = {
            if(!apiService?.checkIncludedApiKey(params)) { //Simply returns true if the apiKey was found in the database!
                def returnMap = [
                    "error": "API key was not found",
                    "statusCode": 401 //Just for testing!
                ]
                if(params.outputFormat && params.outputFormat ?.equals("xml")) {
                    def textToRender = apiService?.createXmlFromMapInstance(returnMap)
                    owner?.render(status: 401, contentType: "text/xml", text: textToRender)
                } else owner?.render(status: 401, contentType: "application/json", text: (returnMap as JSON))
                return false //We don't want the action to be processed!
            }
        }
    }
}

就是这样,它对我有用: - )