代码之家  ›  专栏  ›  技术社区  ›  Dominik Tamm

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

  •  3
  • Dominik Tamm  · 技术社区  · 9 年前

    所以我有这个过滤器

    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 回复  |  直到 9 年前
        1
  •  0
  •   Dominik Tamm    9 年前

    我自己已经解决了这个问题,我只从过滤器中删除了名称空间,所以过滤器看起来像这样:

    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!
                }
            }
        }
    }
    

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

    推荐文章