Permalink
Browse files

partial fix for GRAILS-9196 "GrailsMockHttpServletRequest#getHeaders(…

…headerName) throws an AbstractMethodError"

Provides an alternative way to get the headers using the 'header' and 'headers' methods. The original issue is some incompatibility between Spring and Servlet 3.0
  • Loading branch information...
1 parent 9527c4e commit af137685a4757b4ef6f0f38a23c8719159ce2cf0 @graemerocher graemerocher committed Sep 3, 2012
@@ -0,0 +1,35 @@
+package grails.test.web
+
+import spock.lang.Specification
+import grails.test.mixin.TestFor
+import spock.lang.Issue
+
+@TestFor(YourController)
+@Issue('GRAILS-9196')
+class GetHeadersFromResponseSpec extends Specification{
+ def "Test inspection of response headers"() {
+
+ when:"An action that sets response headers is called"
+ controller.index()
+
+ then:"It is possible to inspect the mock response"
+ response.header('Cache-Control') == 'no-cache' // that's fine
+ "0" in response.headers('Expires') // will throw the exception
+
+ }
+}
+
+class YourController {
+
+ def index() {
+ nocache(response)
+ render ("foo")
+ }
+
+ void nocache(response) {
+ response.setHeader('Cache-Control', 'no-cache') // HTTP 1.1
+ response.addDateHeader('Expires', 0)
+ response.setDateHeader('max-age', 0)
+ response.addHeader('cache-Control', 'private')
+ }
+}
@@ -51,6 +51,27 @@ abstract class AbstractGrailsMockHttpServletResponse extends MockHttpServletResp
writer << content
}
+
+ /**
+ * Return the primary value for the given header as a String, if any.
+ * Will return the first value in case of multiple values.
+ *
+ * @param name the name of the header
+ * @return the associated header value, or <code>null<code> if none
+ */
+ String header(String name) {
+ super.getHeader(name)
+ }
+
+ /**
+ * Return all values for the given header as a List of Strings.
+ * @param name the name of the header
+ * @return the associated header values, or an empty List if none
+ */
+ List<String> headers(String name) {
+ super.getHeaders(name)
+ }
+
/**
* Get the response XML
*
@@ -14,14 +14,13 @@
*/
package org.codehaus.groovy.grails.plugins.testing;
-import java.util.Collection;
-import java.util.Set;
+import java.util.*;
/**
* Refer to the groovydoc of {@link GrailsMockHttpServletResponse} for further information
*
* @author Graeme Rocher
* @since 2.1
*/
- public class GrailsMockHttpServletResponse extends AbstractGrailsMockHttpServletResponse{
- }
+public class GrailsMockHttpServletResponse extends AbstractGrailsMockHttpServletResponse{
+}

0 comments on commit af13768

Please sign in to comment.