Created
February 22, 2010 17:36
-
-
Save kitplummer/311284 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes | |
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest | |
import org.springframework.mock.web.MockHttpServletResponse | |
import org.springframework.mock.web.MockServletContext | |
import org.springframework.mock.web.MockMultipartHttpServletRequest | |
import org.springframework.web.context.request.RequestContextHolder | |
import grails.test.* | |
class JobControllerTests extends GroovyTestCase { | |
protected void setUp() { | |
super.setUp() | |
} | |
protected void tearDown() { | |
super.tearDown() | |
} | |
void testCreateWithXML() { | |
bindMockMultipartWebRequest() | |
def controller = new JobController() | |
controller.request.contentType = 'text/xml' | |
controller.params.job = '''<?xml version="1.0" encoding="ISO-8859-1"?><job> | |
<email>test@test.com</email> | |
<desc>test</desc> | |
</job>'''.getBytes() // note we need the bytes | |
controller.save() | |
print controller.response.contentAsString | |
def job = new XmlParser().parseText(controller.response.contentAsString) | |
println "JOB: ${job}" | |
assertTrue job.error[0].text().contains("no file uploaded") | |
assertEquals 403, controller.response.status | |
} | |
// Required to trick grails into thinking this is a multi-part form POST | |
// Somewhat documented here: http://markmail.org/message/zcpepup3udecoldw | |
GrailsWebRequest bindMockMultipartWebRequest() { | |
MockMultipartHttpServletRequest request = new | |
MockMultipartHttpServletRequest(); | |
GrailsWebRequest webRequest = new GrailsWebRequest( | |
request, | |
new MockHttpServletResponse(), | |
new MockServletContext() | |
); | |
request.setAttribute(GrailsApplicationAttributes.WEB_REQUEST, | |
webRequest); | |
RequestContextHolder.setRequestAttributes(webRequest); | |
return webRequest; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment