From afab3392b366db19b8892ffcf0ec26f5cd628ccb Mon Sep 17 00:00:00 2001 From: hiroshi Date: Thu, 5 Jul 2018 11:56:37 +0900 Subject: [PATCH] Only internal redirect when urlPattern is path --- .../wovnio/wovnjava/WovnServletFilter.java | 2 +- .../com/github/wovnio/wovnjava/TestUtil.java | 30 ++++++++++++++----- .../wovnjava/WovnServletFilterTest.java | 26 +++++++++++++++- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/github/wovnio/wovnjava/WovnServletFilter.java b/src/main/java/com/github/wovnio/wovnjava/WovnServletFilter.java index 76aa3a01..2546df0c 100644 --- a/src/main/java/com/github/wovnio/wovnjava/WovnServletFilter.java +++ b/src/main/java/com/github/wovnio/wovnjava/WovnServletFilter.java @@ -48,7 +48,7 @@ private void tryTranslate(Headers headers, HttpServletRequest request, ServletRe WovnHttpServletRequest wovnRequest = new WovnHttpServletRequest(request, headers); WovnHttpServletResponse wovnResponse = new WovnHttpServletResponse((HttpServletResponse)response); - if (headers.getPathLang().length() > 0) { + if (settings.urlPattern.equals("path") && headers.getPathLang().length() > 0) { wovnRequest.getRequestDispatcher(headers.pathNameKeepTrailingSlash).forward(wovnRequest, wovnResponse); } else { chain.doFilter(wovnRequest, wovnResponse); diff --git a/src/test/java/com/github/wovnio/wovnjava/TestUtil.java b/src/test/java/com/github/wovnio/wovnjava/TestUtil.java index 53392b2a..a19c5c23 100644 --- a/src/test/java/com/github/wovnio/wovnjava/TestUtil.java +++ b/src/test/java/com/github/wovnio/wovnjava/TestUtil.java @@ -15,13 +15,13 @@ public class TestUtil { - private static final HashMap emptySettings = new HashMap(); + private static final HashMap emptyOption = new HashMap(); - public static Settings makeSettings() { - return makeSettings(emptySettings); + public static FilterConfig makeConfig() { + return makeConfig(emptyOption); } - public static Settings makeSettings(HashMap option) { + public static FilterConfig makeConfig(HashMap option) { FilterConfig mock = EasyMock.createMock(FilterConfig.class); String[] keys = {"userToken", "projectToken", "sitePrefixPath", "secretKey", "urlPattern", "urlPatternReg", "query", "apiUrl", "defaultLang", "supportedLangs", "testMode", "testUrl", "useProxy", "debugMode", "originalUrlHeader", "originalQueryStringHeader", "strictHtmlCheck", "deleteInvalidClosingTag", "deleteInvalidUTF8", "connectTimeout", "readTimeout"}; for (int i=0; i option) { EasyMock.expect(mock.getInitParameter(key)).andReturn(val); } EasyMock.replay(mock); - return new Settings(mock); + return mock; + } + + public static Settings makeSettings() { + return makeSettings(emptyOption); + } + + public static Settings makeSettings(HashMap option) { + return new Settings(makeConfig(option)); } public static HttpServletRequest mockRequestPath(String path) { @@ -93,14 +101,22 @@ public static FilterConfig mockFilterConfig() { } public static FilterChainMock doServletFilter(String contentType, String path) throws ServletException, IOException { - return doServletFilter(contentType, path, path); + return doServletFilter(contentType, path, path, emptyOption); + } + + public static FilterChainMock doServletFilter(String contentType, String path, HashMap option) throws ServletException, IOException { + return doServletFilter(contentType, path, path, option); } public static FilterChainMock doServletFilter(String contentType, String path, String forwardPath) throws ServletException, IOException { + return doServletFilter(contentType, path, forwardPath, emptyOption); + } + + public static FilterChainMock doServletFilter(String contentType, String path, String forwardPath, HashMap option) throws ServletException, IOException { RequestDispatcherMock dispatcher = new RequestDispatcherMock(); HttpServletRequest req = mockRequestPath(path, forwardPath, dispatcher); ServletResponse res = TestUtil.mockResponse(contentType, ""); - FilterConfig filterConfig = mockFilterConfig(); + FilterConfig filterConfig = makeConfig(option); FilterChainMock filterChain = new FilterChainMock(); WovnServletFilter filter = new WovnServletFilter(); filter.init(filterConfig); diff --git a/src/test/java/com/github/wovnio/wovnjava/WovnServletFilterTest.java b/src/test/java/com/github/wovnio/wovnjava/WovnServletFilterTest.java index ea27c29b..f3e1aed8 100644 --- a/src/test/java/com/github/wovnio/wovnjava/WovnServletFilterTest.java +++ b/src/test/java/com/github/wovnio/wovnjava/WovnServletFilterTest.java @@ -1,6 +1,7 @@ package com.github.wovnio.wovnjava; import java.io.IOException; +import java.util.HashMap; import javax.servlet.FilterConfig; import javax.servlet.FilterChain; import javax.servlet.ServletRequest; @@ -15,7 +16,6 @@ public class WovnServletFilterTest extends TestCase { - public void testHtml() throws ServletException, IOException { FilterChainMock mock = TestUtil.doServletFilter("text/html; charset=utf-8", "/"); assertEquals("text/html; charset=utf-8", mock.res.getContentType()); @@ -28,6 +28,18 @@ public void testHtmlWithLang() throws ServletException, IOException { assertEquals("/", mock.req.getRequestURI()); } + public void testHtmlWithQueryLang() throws ServletException, IOException { + FilterChainMock mock = TestUtil.doServletFilter("text/html; charset=utf-8", "/?wovn=ja", queryOption); + assertEquals("text/html; charset=utf-8", mock.res.getContentType()); + assertEquals("/", mock.req.getRequestURI()); + } + + public void testHtmlWithSubdomain() throws ServletException, IOException { + FilterChainMock mock = TestUtil.doServletFilter("text/html; charset=utf-8", "/", subdomainOption("ja.wovn.io")); + assertEquals("text/html; charset=utf-8", mock.res.getContentType()); + assertEquals("/", mock.req.getRequestURI()); + } + public void testCss() throws ServletException, IOException { FilterChainMock mock = TestUtil.doServletFilter("text/css", "/dir/style.css"); assertEquals("text/css", mock.res.getContentType()); @@ -51,4 +63,16 @@ public void testImageWithLang() throws ServletException, IOException { assertEquals("image/png", mock.res.getContentType()); assertEquals("/image.png", mock.req.getRequestURI()); } + + private final HashMap queryOption = new HashMap() {{ + put("urlPattern", "query"); + }}; + + private HashMap subdomainOption(String host) { + return new HashMap() {{ + put("urlPattern", "subdomain"); + put("host", host); + }}; + } + }