Skip to content

Commit 5f32e85

Browse files
committed
Merge branch '2.3.x' of github.com:grails/grails-core into 2.3.x
2 parents 5bfac22 + 66efffb commit 5f32e85

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

grails-plugin-mimetypes/src/main/groovy/org/codehaus/groovy/grails/plugins/web/api/MimeTypesApiSupport.groovy

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ class MimeTypesApiSupport {
4949
String format = lookupFormat(formatProvider)
5050
if (formats) {
5151
if (format == 'all') {
52-
def firstKey = formats.keySet().iterator().next()
53-
result = getResponseForFormat(formats[firstKey], firstKey, formatProvider)
52+
result = resolveAllFormat(formatProvider, formats)
5453
}
5554
else {
5655
// if the format has been specified then use that
@@ -69,8 +68,7 @@ class MimeTypesApiSupport {
6968
else {
7069
if (mime.extension == 'all') {
7170
matchFound = true
72-
def firstKey = formats.keySet().iterator().next()
73-
result = getResponseForFormat(formats[firstKey], firstKey, formatProvider)
71+
result = resolveAllFormat(formatProvider, formats)
7472
break
7573
}
7674
}
@@ -84,6 +82,27 @@ class MimeTypesApiSupport {
8482
return result
8583
}
8684

85+
/**
86+
* implementation for resolving "all" format
87+
*
88+
* @param formatProvider
89+
* @param formats
90+
* @return
91+
*/
92+
protected Object resolveAllFormat(formatProvider, LinkedHashMap<String, Object> formats) {
93+
def formatKey
94+
def format
95+
if(formats.containsKey('*')) {
96+
formatKey = '*'
97+
format = 'all'
98+
} else {
99+
// choose first key
100+
formatKey = formats.keySet().iterator().next()
101+
format = formatKey
102+
}
103+
getResponseForFormat(formats[formatKey], format, formatProvider)
104+
}
105+
87106
@CompileStatic(TypeCheckingMode.SKIP)
88107
protected MimeType[] lookupMimeTypes(formatProvider) {
89108
formatProvider.mimeTypes

grails-plugin-mimetypes/src/test/groovy/org/codehaus/groovy/grails/plugins/web/api/RequestAndResponseMimeTypesApiSpec.groovy

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import javax.servlet.http.HttpServletRequest
55
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
66
import org.codehaus.groovy.grails.commons.metaclass.MetaClassEnhancer
77
import org.codehaus.groovy.grails.plugins.web.mimes.MimeTypesFactoryBean
8+
9+
import spock.lang.Issue;
810
import spock.lang.Specification
911
import javax.servlet.http.HttpServletResponse
1012
import org.springframework.mock.web.MockHttpServletRequest
@@ -43,13 +45,13 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{
4345
response.format == 'all'
4446
}
4547

46-
void "Test format property is valid for CONTENT_TYPE and ACCEPT header"() {
48+
void "Test format property is valid for CONTENT_TYPE and Accept header"() {
4749
when: "The request CONTENT_TYPE header is 'text/xml'"
4850
final webRequest = GrailsWebUtil.bindMockWebRequest()
4951
MockHttpServletRequest request = webRequest.currentRequest
5052
def response = webRequest.currentResponse
5153
request.contentType = "text/xml"
52-
request.addHeader('ACCEPT', 'text/json')
54+
request.addHeader('Accept', 'text/json')
5355

5456
then: "The request format should be 'xml'"
5557
requestMimeTypesApi.getFormat(request) == "xml"
@@ -58,17 +60,17 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{
5860
response.format == 'json'
5961
}
6062

61-
void "Test format property is valid for ACCEPT header only"() {
63+
void "Test format property is valid for Accept header only"() {
6264
when: "The request CONTENT_TYPE header is 'text/xml'"
6365
final webRequest = GrailsWebUtil.bindMockWebRequest()
6466
MockHttpServletRequest request = webRequest.currentRequest
6567
def response = webRequest.currentResponse
66-
request.addHeader('ACCEPT', 'text/json')
68+
request.addHeader('Accept', 'text/json')
6769

6870
then: "The request format should be 'xml'"
69-
requestMimeTypesApi.getFormat(request) == "html"
70-
requestMimeTypesApi.getFormat(request) == "html" // call twice to test cached value
71-
request.format == 'html'
71+
requestMimeTypesApi.getFormat(request) == "all"
72+
requestMimeTypesApi.getFormat(request) == "all" // call twice to test cached value
73+
request.format == 'all'
7274
response.format == 'json'
7375
}
7476

@@ -95,8 +97,8 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{
9597

9698
}
9799

98-
void "Test withFormat method with ACCEPT header only"() {
99-
when: "The request ACCEPT header is 'text/xml' and withFormat is used"
100+
void "Test withFormat method with Accept header only"() {
101+
when: "The request Accept header is 'text/xml' and withFormat is used"
100102
final webRequest = GrailsWebUtil.bindMockWebRequest()
101103
def request = webRequest.currentRequest
102104
def response = webRequest.currentResponse
@@ -118,7 +120,7 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{
118120
requestResult == 'got html'
119121
responseResult == 'got xml'
120122

121-
when:"The ACCEPT header is JSON and there is a catch-all"
123+
when:"The Accept header is JSON and there is a catch-all"
122124
webRequest = GrailsWebUtil.bindMockWebRequest()
123125
request = webRequest.currentRequest
124126
response = webRequest.currentResponse
@@ -132,8 +134,23 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{
132134
responseResult == 'got everything'
133135

134136
}
135-
136-
137+
138+
@Issue("GRAILS-10973")
139+
void "request.withFormat should choose wildcard choice when format == all"() {
140+
when:
141+
final webRequest = GrailsWebUtil.bindMockWebRequest()
142+
def request = webRequest.currentRequest
143+
def response = webRequest.currentResponse
144+
def requestResult = request.withFormat {
145+
html { 'got html' }
146+
xml { 'got xml' }
147+
'*' { 'got everything' }
148+
}
149+
then: 'format is all'
150+
request.format == 'all'
151+
then: 'The * closure is invoked'
152+
requestResult == 'got everything'
153+
}
137154

138155
void "Test withFormat returns first block if no format provided"() {
139156
when: "No Accept header, URI extension or format param"
@@ -198,15 +215,16 @@ class RequestAndResponseMimeTypesApiSpec extends Specification{
198215
s.parse '''
199216
grails.mime.file.extensions = true // enables the parsing of file extensions from URLs into the request format
200217
grails.mime.use.accept.header = true
201-
grails.mime.types = [ html: ['text/html','application/xhtml+xml'],
218+
grails.mime.types = [
219+
all: '*/*',
220+
html: ['text/html','application/xhtml+xml'],
202221
xml: ['text/xml', 'application/xml'],
203222
text: 'text/plain',
204223
js: 'text/javascript',
205224
rss: 'application/rss+xml',
206225
atom: 'application/atom+xml',
207226
css: 'text/css',
208227
csv: 'text/csv',
209-
all: '*/*',
210228
json: ['application/json','text/json'],
211229
form: 'application/x-www-form-urlencoded',
212230
multipartForm: 'multipart/form-data'

grails-resources/src/grails/grails-app/conf/Config.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ grails.project.groupId = appName // change this to alter the default package nam
1515

1616
// The ACCEPT header will not be used for content negotiation for user agents containing the following strings (defaults to the 4 major rendering engines)
1717
grails.mime.disable.accept.header.userAgents = ['Gecko', 'WebKit', 'Presto', 'Trident']
18-
grails.mime.types = [
18+
grails.mime.types = [ // the first one is the default format
19+
html: ['text/html','application/xhtml+xml'],
1920
all: '*/*',
2021
atom: 'application/atom+xml',
2122
css: 'text/css',
2223
csv: 'text/csv',
2324
form: 'application/x-www-form-urlencoded',
24-
html: ['text/html','application/xhtml+xml'],
2525
js: 'text/javascript',
2626
json: ['application/json', 'text/json'],
2727
multipartForm: 'multipart/form-data',

0 commit comments

Comments
 (0)