mirror of
https://github.com/apache/struts.git
synced 2026-08-20 22:17:12 +00:00
WW-5259 Extracts UrlHelper#parseQueryString into a dedicated bean
This commit is contained in:
@@ -29,7 +29,8 @@ import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;
|
||||
import org.apache.struts2.views.util.DefaultUrlHelper;
|
||||
import org.apache.struts2.url.StrutsQueryStringBuilder;
|
||||
import org.apache.struts2.url.StrutsUrlEncoder;
|
||||
import org.easymock.IMocksControl;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
@@ -82,7 +83,7 @@ public class ServletActionRedirectResultTest extends StrutsInternalTestCase {
|
||||
result.setEncode(false);
|
||||
result.setPrependServletContext(false);
|
||||
result.setAnchor("fragment");
|
||||
result.setUrlHelper(new DefaultUrlHelper());
|
||||
result.setQueryStringBuilder(new StrutsQueryStringBuilder(new StrutsUrlEncoder()));
|
||||
|
||||
IMocksControl control = createControl();
|
||||
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
|
||||
@@ -144,7 +145,7 @@ public class ServletActionRedirectResultTest extends StrutsInternalTestCase {
|
||||
result.setEncode(false);
|
||||
result.setPrependServletContext(false);
|
||||
result.setAnchor("fragment");
|
||||
result.setUrlHelper(new DefaultUrlHelper());
|
||||
result.setQueryStringBuilder(new StrutsQueryStringBuilder(new StrutsUrlEncoder()));
|
||||
|
||||
IMocksControl control = createControl();
|
||||
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
|
||||
@@ -210,7 +211,7 @@ public class ServletActionRedirectResultTest extends StrutsInternalTestCase {
|
||||
result.setEncode(false);
|
||||
result.setPrependServletContext(false);
|
||||
result.setAnchor("fragment");
|
||||
result.setUrlHelper(new DefaultUrlHelper());
|
||||
result.setQueryStringBuilder(new StrutsQueryStringBuilder(new StrutsUrlEncoder()));
|
||||
|
||||
IMocksControl control = createControl();
|
||||
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
|
||||
@@ -264,7 +265,7 @@ public class ServletActionRedirectResultTest extends StrutsInternalTestCase {
|
||||
result.setEncode(false);
|
||||
result.setPrependServletContext(false);
|
||||
result.setAnchor("fragment");
|
||||
result.setUrlHelper(new DefaultUrlHelper());
|
||||
result.setQueryStringBuilder(new StrutsQueryStringBuilder(new StrutsUrlEncoder()));
|
||||
|
||||
IMocksControl control = createControl();
|
||||
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
|
||||
|
||||
@@ -18,29 +18,20 @@
|
||||
*/
|
||||
package org.apache.struts2.result;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.mockobjects.dynamic.C;
|
||||
import com.mockobjects.dynamic.Mock;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.mock.MockActionInvocation;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.util.ValueStackFactory;
|
||||
import ognl.Ognl;
|
||||
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.dispatcher.HttpParameters;
|
||||
|
||||
import com.mockobjects.dynamic.C;
|
||||
import com.mockobjects.dynamic.Mock;
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import org.apache.struts2.result.ServletDispatcherResult;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class ServletDispatcherResultTest extends StrutsInternalTestCase implements StrutsStatics {
|
||||
|
||||
public void testInclude() {
|
||||
|
||||
@@ -32,8 +32,8 @@ import com.opensymphony.xwork2.util.ValueStack;
|
||||
import org.apache.struts2.ServletActionContext;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.dispatcher.mapper.ActionMapper;
|
||||
import org.apache.struts2.views.util.DefaultUrlHelper;
|
||||
import org.apache.struts2.url.StrutsQueryStringBuilder;
|
||||
import org.apache.struts2.url.StrutsUrlEncoder;
|
||||
import org.easymock.IMocksControl;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
@@ -240,7 +240,7 @@ public class ServletRedirectResultTest extends StrutsInternalTestCase implements
|
||||
result.setEncode(false);
|
||||
result.setPrependServletContext(false);
|
||||
result.setAnchor("fragment");
|
||||
result.setUrlHelper(new DefaultUrlHelper());
|
||||
result.setQueryStringBuilder(new StrutsQueryStringBuilder(new StrutsUrlEncoder()));
|
||||
|
||||
IMocksControl control = createControl();
|
||||
ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
|
||||
@@ -286,7 +286,7 @@ public class ServletRedirectResultTest extends StrutsInternalTestCase implements
|
||||
result.setParse(true);
|
||||
result.setEncode(false);
|
||||
result.setPrependServletContext(false);
|
||||
result.setUrlHelper(new DefaultUrlHelper());
|
||||
result.setQueryStringBuilder(new StrutsQueryStringBuilder(new StrutsUrlEncoder()));
|
||||
result.setSuppressEmptyParameters(true);
|
||||
|
||||
IMocksControl control = createControl();
|
||||
@@ -433,7 +433,7 @@ public class ServletRedirectResultTest extends StrutsInternalTestCase implements
|
||||
}
|
||||
}
|
||||
|
||||
public void testPassingNullInvocation() throws Exception{
|
||||
public void testPassingNullInvocation() throws Exception {
|
||||
Result result = new ServletRedirectResult();
|
||||
try {
|
||||
result.execute(null);
|
||||
|
||||
+7
-7
@@ -28,9 +28,9 @@ import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StrutsParametersStringBuilderTest {
|
||||
public class StrutsQueryStringBuilderTest {
|
||||
|
||||
private ParametersStringBuilder builder;
|
||||
private QueryStringBuilder builder;
|
||||
|
||||
@Test
|
||||
public void testBuildParametersStringWithUrlHavingSomeExistingParameters() {
|
||||
@@ -43,7 +43,7 @@ public class StrutsParametersStringBuilderTest {
|
||||
|
||||
StringBuilder url = new StringBuilder("http://localhost:8080/myContext/myPage.jsp?initParam=initValue");
|
||||
|
||||
builder.buildParametersString(parameters, url, UrlHelper.AMP);
|
||||
builder.build(parameters, url, UrlHelper.AMP);
|
||||
|
||||
assertEquals(expectedUrl, url.toString());
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public class StrutsParametersStringBuilderTest {
|
||||
|
||||
StringBuilder url = new StringBuilder("http://localhost:8080/myContext/myPage.jsp?initParam=initValue");
|
||||
|
||||
builder.buildParametersString(parameters, url, UrlHelper.AMP);
|
||||
builder.build(parameters, url, UrlHelper.AMP);
|
||||
|
||||
assertEquals(expectedUrl, url.toString());
|
||||
}
|
||||
@@ -71,7 +71,7 @@ public class StrutsParametersStringBuilderTest {
|
||||
parameters.put("param1", new String[]{});
|
||||
parameters.put("param2", new ArrayList<>());
|
||||
StringBuilder url = new StringBuilder("https://www.nowhere.com/myworld.html");
|
||||
builder.buildParametersString(parameters, url, UrlHelper.AMP);
|
||||
builder.build(parameters, url, UrlHelper.AMP);
|
||||
assertEquals(expectedUrl, url.toString());
|
||||
}
|
||||
|
||||
@@ -87,13 +87,13 @@ public class StrutsParametersStringBuilderTest {
|
||||
}
|
||||
});
|
||||
StringBuilder url = new StringBuilder("https://www.nowhere.com/myworld.html");
|
||||
builder.buildParametersString(parameters, url, "&");
|
||||
builder.build(parameters, url, "&");
|
||||
assertEquals(expectedUrl, url.toString());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
builder = new StrutsParametersStringBuilder(new StrutsUrlEncoder());
|
||||
builder = new StrutsQueryStringBuilder(new StrutsUrlEncoder());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.url;
|
||||
|
||||
import org.assertj.core.util.Arrays;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class StrutsQueryStringParserTest {
|
||||
|
||||
private QueryStringParser parser;
|
||||
|
||||
@Test
|
||||
public void testParseQuery() {
|
||||
Map<String, Object> result = parser.parse("aaa=aaaval&bbb=bbbval&ccc=&%3Ca%22%3E=%3Cval%3E", false);
|
||||
|
||||
assertEquals("aaaval", result.get("aaa"));
|
||||
assertEquals("bbbval", result.get("bbb"));
|
||||
assertEquals("", result.get("ccc"));
|
||||
assertEquals("<val>", result.get("<a\">"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseQueryIntoArray() {
|
||||
Map<String, Object> result = parser.parse("a=1&a=2&a=3", true);
|
||||
|
||||
Object actual = result.get("a");
|
||||
assertThat(actual).isInstanceOf(String[].class);
|
||||
assertThat(Arrays.asList(actual)).containsOnly("1", "2", "3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseEmptyQuery() {
|
||||
Map<String, Object> result = parser.parse("", false);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseNullQuery() {
|
||||
Map<String, Object> result = parser.parse(null, false);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(0, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeSpacesInQueryString() {
|
||||
Map<String, Object> queryParameters = parser.parse("name=value+with+space", false);
|
||||
|
||||
assertTrue(queryParameters.containsKey("name"));
|
||||
assertEquals("value with space", queryParameters.get("name"));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.parser = new StrutsQueryStringParser(new StrutsUrlDecoder());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,7 +23,7 @@ import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Scope.Strategy;
|
||||
import org.apache.struts2.StrutsInternalTestCase;
|
||||
import org.apache.struts2.url.StrutsParametersStringBuilder;
|
||||
import org.apache.struts2.url.StrutsQueryStringBuilder;
|
||||
import org.apache.struts2.url.StrutsUrlDecoder;
|
||||
import org.apache.struts2.url.StrutsUrlEncoder;
|
||||
|
||||
@@ -339,45 +339,13 @@ public class DefaultUrlHelperTest extends StrutsInternalTestCase {
|
||||
assertEquals(expectedString, urlString);
|
||||
}
|
||||
|
||||
|
||||
public void testParseQuery() {
|
||||
Map<String, Object> result = urlHelper.parseQueryString("aaa=aaaval&bbb=bbbval&ccc=&%3Ca%22%3E=%3Cval%3E", false);
|
||||
|
||||
assertEquals(result.get("aaa"), "aaaval");
|
||||
assertEquals(result.get("bbb"), "bbbval");
|
||||
assertEquals(result.get("ccc"), "");
|
||||
assertEquals(result.get("<a\">"), "<val>");
|
||||
}
|
||||
|
||||
public void testParseEmptyQuery() {
|
||||
Map<String, Object> result = urlHelper.parseQueryString("", false);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(result.size(), 0);
|
||||
}
|
||||
|
||||
public void testParseNullQuery() {
|
||||
Map<String, Object> result = urlHelper.parseQueryString(null, false);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(result.size(), 0);
|
||||
}
|
||||
|
||||
public void testDecodeSpacesInQueryString() {
|
||||
Map<String, Object> queryParameters = urlHelper.parseQueryString("name=value+with+space", false);
|
||||
|
||||
assertTrue(queryParameters.containsKey("name"));
|
||||
assertEquals("value with space", queryParameters.get("name"));
|
||||
}
|
||||
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
StubContainer stubContainer = new StubContainer(container);
|
||||
ActionContext.getContext().withContainer(stubContainer);
|
||||
urlHelper = new DefaultUrlHelper();
|
||||
StrutsUrlEncoder encoder = new StrutsUrlEncoder();
|
||||
urlHelper.setParametersStringBuilder(new StrutsParametersStringBuilder(encoder));
|
||||
urlHelper.setQueryStringBuilder(new StrutsQueryStringBuilder(encoder));
|
||||
urlHelper.setEncoder(encoder);
|
||||
urlHelper.setDecoder(new StrutsUrlDecoder());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user