So i have this filter
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!
}
}
}
}
My Unit Test looks like this:
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://ift.tt/WV2C4M", false)
xmlSlurperInstance?.setFeature("http://ift.tt/1pbdJzG", false)
def inputStream = new ByteArrayInputStream(response.text?.toString()?.getBytes())
def testXMLArray = xmlSlurperInstance?.parse(new InputSource(inputStream))
response.contentType == "text/xml"
}
}
On Output "Test 3", nothing is visible, though here should be the content which i render from the filter... it also is the same response if i code the Filter to redirect to another controller in the namespace "api", which will handle the render task.
Anyone experienced something similar already or may know a workaround to get the expected result?
Aucun commentaire:
Enregistrer un commentaire