Author: lukaszlenart Date: Wed Apr 11 17:12:35 2012 New Revision: 1324870 URL: http://svn.apache.org/viewvc?rev=1324870&view=rev Log: WW-3059 Changes logic to set encoding for XMLHttpRequest type
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java?rev=1324870&r1=1324869&r2=1324870&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java Wed Apr 11 17:12:35 2012 @@ -679,6 +679,10 @@ public class Dispatcher { if (defaultEncoding != null) { encoding = defaultEncoding; } + // check for Ajax request to use UTF-8 encoding strictly http://www.w3.org/TR/XMLHttpRequest/#the-send-method + if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { + encoding = "utf-8"; + } Locale locale = null; if (defaultLocale != null) { Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java?rev=1324870&r1=1324869&r2=1324870&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java Wed Apr 11 17:12:35 2012 @@ -21,28 +21,8 @@ package org.apache.struts2.dispatcher; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.struts2.StrutsConstants; -import org.apache.struts2.StrutsTestCase; -import org.apache.struts2.dispatcher.FilterDispatcherTest.InnerActionMapper; -import org.apache.struts2.dispatcher.FilterDispatcherTest.InnerDestroyableObjectFactory; -import org.apache.struts2.dispatcher.FilterDispatcherTest.InnerDispatcher; -import org.springframework.mock.web.MockFilterConfig; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.mock.web.MockServletContext; - import com.mockobjects.dynamic.C; import com.mockobjects.dynamic.Mock; -import com.mockobjects.servlet.MockFilterChain; import com.opensymphony.xwork2.ObjectFactory; import com.opensymphony.xwork2.config.Configuration; import com.opensymphony.xwork2.config.ConfigurationManager; @@ -50,11 +30,20 @@ import com.opensymphony.xwork2.config.en import com.opensymphony.xwork2.config.entities.InterceptorStackConfig; import com.opensymphony.xwork2.config.entities.PackageConfig; import com.opensymphony.xwork2.inject.Container; -import com.opensymphony.xwork2.inject.ContainerBuilder; -import com.opensymphony.xwork2.inject.Context; -import com.opensymphony.xwork2.inject.Factory; import com.opensymphony.xwork2.interceptor.Interceptor; import com.opensymphony.xwork2.util.LocalizedTextUtil; +import org.apache.struts2.StrutsConstants; +import org.apache.struts2.StrutsTestCase; +import org.apache.struts2.dispatcher.FilterDispatcherTest.InnerDestroyableObjectFactory; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockServletContext; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; /** * Test case for Dispatcher. @@ -89,6 +78,23 @@ public class DispatcherTest extends Stru assertEquals(req.getCharacterEncoding(), "utf-8"); } + public void testEncodingForXMLHttpRequest() throws Exception { + // given + MockHttpServletRequest req = new MockHttpServletRequest(); + req.addHeader("X-Requested-With", "XMLHttpRequest"); + HttpServletResponse res = new MockHttpServletResponse(); + + Dispatcher du = initDispatcher(new HashMap() {{ + put(StrutsConstants.STRUTS_I18N_ENCODING, "latin-2"); + }}); + + // when + du.prepare(req, res); + + // then + assertEquals(req.getCharacterEncoding(), "utf-8"); + } + public void testPrepareSetEncodingPropertyWithMultipartRequest() throws Exception { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse();