Skip to content

Commit

Permalink
Merge pull request grails#561 from dmahapatro/master
Browse files Browse the repository at this point in the history
GRAILS-11954 Fix for content negotiation response content type.
  • Loading branch information
Jeff Scott Brown committed Feb 4, 2015
2 parents 868d1f8 + 3cf824d commit 63423e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package grails.artefact.controller.support

import groovy.transform.CompileStatic
import org.grails.web.converters.Converter

/**
*
Expand Down Expand Up @@ -51,4 +52,8 @@ trait ResponseRenderer {
void render(Map args, CharSequence body) {
helper.invokeRender this, args, body
}

void render(Converter<?> converter) {
helper.invokeRender this, converter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import grails.converters.XML
import grails.test.mixin.TestFor
import org.grails.plugins.testing.GrailsMockHttpServletRequest
import org.grails.plugins.testing.GrailsMockHttpServletResponse

import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Unroll

@TestFor(ContentNegotiationController)
class ContentNegotiationSpec extends Specification {
Expand Down Expand Up @@ -50,17 +50,29 @@ class ContentNegotiationSpec extends Specification {
grailsApplication.config = config
}

@Issue("GRAILS-10897")
void "test index json content negotiation"() {
@Unroll
@Issue(["GRAILS-10897", "GRAILS-11954"])
void "test render as #converter returns response with content-type '#contentType'"() {
given:
def title = "This controller title"
controller.params.title = title

when: 'the request is set to json'
request.addHeader "Accept", "text/json"
request.addHeader "Accept", acceptType
controller.index()

then:
'json' == controller.response.format
controller.response.contentAsString == """{"title":"$title"}"""
controller.response.format == responseFormat
controller.response.contentAsString == contentAsString
controller.response.contentType.tokenize(/;/)[0] == contentType

where:
acceptType || responseFormat || contentType || contentAsString
"text/json" || 'json' || "application/json" || /{"title":"This controller title"}/ //defaults to application/json
"application/json" || 'json' || "application/json" || /{"title":"This controller title"}/
"application/xml" || 'xml' || "application/xml" || '''<?xml version="1.0" encoding="UTF-8"?><map><entry key="title">This controller title</entry></map>'''

converter = responseFormat.toUpperCase()
}
}

Expand Down

0 comments on commit 63423e3

Please sign in to comment.