TAP5-1900: Provide better control over Response character set, using value specified in Request if possible
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/a29d1ea9 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/a29d1ea9 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/a29d1ea9 Branch: refs/heads/master Commit: a29d1ea936802ec2f7a0494afbc2ac63fc0ae8c7 Parents: 5b6e7c1 Author: Howard M. Lewis Ship <hls...@apache.org> Authored: Fri Aug 1 16:35:03 2014 -0700 Committer: Howard M. Lewis Ship <hls...@apache.org> Committed: Fri Aug 1 16:35:03 2014 -0700 ---------------------------------------------------------------------- .../internal/services/RequestImpl.java | 15 ++++++-------- .../internal/services/RequestImplTest.java | 21 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a29d1ea9/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RequestImpl.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RequestImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RequestImpl.java index 1219f9b..6226e86 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RequestImpl.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/RequestImpl.java @@ -1,5 +1,3 @@ -// Copyright 2006-2013 The Apache Software Foundation -// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -151,15 +149,14 @@ public class RequestImpl implements Request return; } - // check if request specifies an encoding - String requestEncoding = - request.getCharacterEncoding() == null - ? applicationCharset - : request.getCharacterEncoding(); - + // The request may specify an encoding, which is better than the application, which can only + // guess at what the client is doing! + + String requestEncoding = request.getCharacterEncoding(); + try { - request.setCharacterEncoding(requestEncoding); + request.setCharacterEncoding(requestEncoding != null ? requestEncoding : applicationCharset); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex); http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/a29d1ea9/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/RequestImplTest.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/RequestImplTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/RequestImplTest.java index 6613d7d..bc9b925 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/RequestImplTest.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/RequestImplTest.java @@ -64,6 +64,23 @@ public class RequestImplTest extends InternalBaseTestCase } @Test + public void used_encoding_from_request() throws Exception { + HttpServletRequest sr = mockHttpServletRequest(); + + expect(sr.getCharacterEncoding()).andReturn("request-encoding"); + + sr.setCharacterEncoding("request-encoding"); + + expect(sr.getParameterNames()).andReturn(Collections.enumeration(Collections.EMPTY_LIST)); + + replay(); + + new RequestImpl(sr, "app-encoding-is-ignored", null).getParameterNames(); + + verify(); + } + + @Test public void set_encoding_success() throws Exception { HttpServletRequest sr = mockHttpServletRequest(); @@ -72,6 +89,8 @@ public class RequestImplTest extends InternalBaseTestCase sr.setCharacterEncoding(encoding); + expect(sr.getCharacterEncoding()).andReturn(null); + expect(sr.getParameterNames()).andReturn(Collections.enumeration(Collections.EMPTY_LIST)); replay(); @@ -92,6 +111,8 @@ public class RequestImplTest extends InternalBaseTestCase sr.setCharacterEncoding(encoding); setThrowable(exception); + expect(sr.getCharacterEncoding()).andReturn(null); + replay(); try