Skip to content
Browse files

Merge branch '2.2.x'

  • Loading branch information...
2 parents eac1d85 + eacd706 commit a2fdbc888af356f5350c9298c2bd87fa6ce5b747 @graemerocher graemerocher committed Nov 19, 2012
View
2 ...in/groovy/org/codehaus/groovy/grails/cli/interactive/completors/ClassNameCompletor.groovy
@@ -21,7 +21,7 @@ import jline.SimpleCompletor
import org.codehaus.groovy.grails.cli.support.BuildSettingsAware
import org.codehaus.groovy.grails.io.support.GrailsResourceUtils
import org.codehaus.groovy.grails.plugins.GrailsPluginUtils;
-import org.springframework.core.io.Resource
+import org.codehaus.groovy.grails.io.support.Resource
/**
* A completor that completes
View
2 ...rvlets/src/main/groovy/org/codehaus/groovy/grails/plugins/web/ServletsGrailsPlugin.groovy
@@ -31,6 +31,6 @@ class ServletsGrailsPlugin {
def dependsOn = [core:version]
def doWithDynamicMethods = { ctx ->
- ServletsGrailsPluginSupport.enhanceServletApi()
+ ServletsGrailsPluginSupport.enhanceServletApi(application.config)
}
}
View
9 ...src/main/groovy/org/codehaus/groovy/grails/plugins/web/ServletsGrailsPluginSupport.groovy
@@ -32,9 +32,14 @@ import org.codehaus.groovy.grails.plugins.web.api.ServletRequestApi
class ServletsGrailsPluginSupport {
- static enhanceServletApi() {
+ static enhanceServletApi(ConfigObject config = new ConfigObject()) {
def requestEnhancer = new MetaClassEnhancer()
- requestEnhancer.addApi new ServletRequestApi()
+ final servletRequestApi = new ServletRequestApi()
+ final xhrIdentifier = grails?.web?.xhr?.identifier
+ if(xhrIdentifier instanceof Closure) {
+ servletRequestApi.xhrRequestIdentifier = xhrIdentifier
+ }
+ requestEnhancer.addApi servletRequestApi
def getAttributeClosure = { String name ->
def mp = delegate.class.metaClass.getMetaProperty(name)
View
4 ...vlets/src/main/groovy/org/codehaus/groovy/grails/plugins/web/api/ServletRequestApi.groovy
@@ -29,6 +29,8 @@ import org.codehaus.groovy.grails.web.util.WebUtils
*/
class ServletRequestApi {
+ Closure xhrRequestIdentifier = { false }
+
/**
* Whether the request has been redirected
*
@@ -50,7 +52,7 @@ class ServletRequestApi {
* @return test whether the current request is an XHR request
*/
boolean isXhr(HttpServletRequest instance) {
- instance.getHeader('X-Requested-With') != null
+ instance.getHeader('X-Requested-With') == "XMLHttpRequest" || xhrRequestIdentifier(instance)
}
/**
View
41 ...s/src/test/groovy/org/codehaus/groovy/grails/plugins/web/api/ServletRequestApiSpec.groovy
@@ -0,0 +1,41 @@
+package org.codehaus.groovy.grails.plugins.web.api
+
+import spock.lang.Specification
+import org.springframework.mock.web.MockHttpServletRequest
+
+/**
+ */
+class ServletRequestApiSpec extends Specification{
+
+
+ void "Test identification of XHR requests"() {
+ given:"An instance of the servlet API extension"
+ def api = new ServletRequestApi()
+
+ when:"a regular request is used"
+ def request = new MockHttpServletRequest()
+ then:"it isn't an XHR request"
+ api.isXhr(request) == false
+
+ when:"A non XHR request is sent with the X-Requested-With header"
+ request.addHeader("X-Requested-With","com.android.browser")
+
+ then:"It is not an XHR request"
+ api.isXhr(request) == false
+
+ when:"A request is sent with a X-Requested-With value of XMLHttpRequest"
+ request = new MockHttpServletRequest()
+ request.addHeader("X-Requested-With", "XMLHttpRequest")
+
+ then:"It is an XHR request"
+ api.isXhr(request) == true
+
+ when:"A custom XHR request identifier is used"
+ api.xhrRequestIdentifier = { r -> r.getHeader('X-Requested-With') == 'Ext.basex' }
+ request = new MockHttpServletRequest()
+ request.addHeader("X-Requested-With", "Ext.basex")
+
+ then:"It is an XHR request"
+ api.isXhr(request) == true
+ }
+}

0 comments on commit a2fdbc8

Please sign in to comment.
Something went wrong with that request. Please try again.