Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Test and fix for GRAILS-7444

  • Loading branch information...
commit cdcb7845c10191de6547597313fa21660176bd44 1 parent 94b8cd9
@lhotari lhotari authored
View
10 ...st-suite-uber/src/test/groovy/org/codehaus/groovy/grails/web/servlet/mvc/RenderDynamicMethodTests.groovy
@@ -57,6 +57,12 @@ class RenderDynamicMethodTests extends AbstractGrailsControllerTests {
assertEquals "<foo>bar</foo>", response.contentAsString
}
+ void testRenderTextWithContentTypeAndCharset() {
+ testCtrl.renderTextWithContentTypeAndCharset()
+ assertEquals "text/xml;charset=utf-16", response.contentType
+ assertEquals "<foo>bar</foo>", response.contentAsString
+ }
+
void testRenderXml() {
testCtrl.renderXml()
assertEquals "text/xml;charset=utf-8", response.contentType
@@ -105,6 +111,10 @@ class RenderDynamicMethodTestController {
render(text:"<foo>bar</foo>",contentType:"text/xml", encoding:"utf-16")
}
+ def renderTextWithContentTypeAndCharset = {
+ render(text:"<foo>bar</foo>",contentType:"text/xml;charset=utf-16")
+ }
+
def renderXml = {
render(contentType:"text/xml") {
foo {
View
10 grails-web/src/main/groovy/grails/util/GrailsWebUtil.java
@@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.Map;
+import java.util.regex.Pattern;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
@@ -47,6 +48,7 @@
public static final String DEFAULT_ENCODING = "UTF-8";
private static final String CHARSET_ATTRIBUTE = ";charset=";
+ private static final Pattern CHARSET_IN_CONTENT_TYPE_REGEXP = Pattern.compile(";\\s*charset\\s*=", Pattern.CASE_INSENSITIVE);
/**
* Looks up a GrailsApplication instance from the ServletContext.
@@ -178,7 +180,11 @@ public static GroovyObject getControllerFromRequest(HttpServletRequest request)
}
public static String getContentType(String name, String encoding) {
- if (StringUtils.isBlank(encoding)) encoding = DEFAULT_ENCODING;
- return name + CHARSET_ATTRIBUTE + encoding;
+ if(CHARSET_IN_CONTENT_TYPE_REGEXP.matcher(name).find()) {
+ return name;
+ } else {
+ if (StringUtils.isBlank(encoding)) encoding = DEFAULT_ENCODING;
+ return name + CHARSET_ATTRIBUTE + encoding;
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.