/common/util/src/test/java/com/alibaba/citrus/util/ServletUtilTests.java

https://github.com/webx/citrus · Java · 245 lines · 176 code · 45 blank · 24 comment · 1 complexity · 4823aaf27dfd1fe19e1ffa778d960cbc MD5 · raw file

  1. /*
  2. * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.alibaba.citrus.util;
  18. import static com.alibaba.citrus.util.CollectionUtil.*;
  19. import static com.alibaba.citrus.util.ServletUtil.*;
  20. import static org.easymock.EasyMock.*;
  21. import static org.junit.Assert.*;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import javax.servlet.http.HttpServletRequest;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import org.junit.runner.RunWith;
  28. import org.junit.runners.Parameterized;
  29. import org.junit.runners.Parameterized.Parameters;
  30. @RunWith(Parameterized.class)
  31. public class ServletUtilTests implements Cloneable {
  32. private HttpServletRequest request;
  33. private final boolean isPrefixServletMapping;
  34. private final String servletPath;
  35. private final String pathInfo;
  36. private final String resourcePath;
  37. private final String baseURL;
  38. private final String servletResourcePath;
  39. private final String servletBaseURL;
  40. private final String fullURL;
  41. public ServletUtilTests(boolean isPrefixServletMapping, String servletPath, String pathInfo, String resourcePath,
  42. String baseURL, String servletResourcePath, String servletBaseURL, String fullURL) {
  43. this.isPrefixServletMapping = isPrefixServletMapping;
  44. this.servletPath = servletPath;
  45. this.pathInfo = pathInfo;
  46. this.resourcePath = resourcePath;
  47. this.baseURL = baseURL;
  48. this.servletResourcePath = servletResourcePath;
  49. this.servletBaseURL = servletBaseURL;
  50. this.fullURL = fullURL;
  51. }
  52. @Parameters
  53. public static Collection<Object[]> data() {
  54. List<Object[]> data = createLinkedList();
  55. // 前缀映射,pathInfo不为空
  56. add(data, true, "/turbine", "/aaa/bbb", //
  57. "/turbine/aaa/bbb", "http://localhost:8080/myapp", //
  58. "/aaa/bbb", "http://localhost:8080/myapp/turbine", //
  59. "http://localhost:8080/myapp/turbine/aaa/bbb");
  60. add(data, true, "/hello.world", "/turbine", //
  61. "/hello.world/turbine", "http://localhost:8080/myapp", //
  62. "/turbine", "http://localhost:8080/myapp/hello.world", //
  63. "http://localhost:8080/myapp/hello.world/turbine");
  64. add(data, true, " hello.world ", " turbine/ ", //
  65. "/hello.world/turbine/", "http://localhost:8080/myapp", //
  66. "/turbine/", "http://localhost:8080/myapp/hello.world", //
  67. "http://localhost:8080/myapp/hello.world/turbine/");
  68. add(data, true, null, " / ", //
  69. "/", "http://localhost:8080/myapp", //
  70. "/", "http://localhost:8080/myapp", //
  71. "http://localhost:8080/myapp/");
  72. // 前缀映射,但pathInfo为空
  73. add(data, true, "/turbine", null, //
  74. "/turbine", "http://localhost:8080/myapp", //
  75. "", "http://localhost:8080/myapp/turbine", //
  76. "http://localhost:8080/myapp/turbine");
  77. add(data, true, "/turbine", "", //
  78. "/turbine", "http://localhost:8080/myapp", //
  79. "", "http://localhost:8080/myapp/turbine", //
  80. "http://localhost:8080/myapp/turbine");
  81. add(data, true, "/hello.world/turbine", null, //
  82. "/hello.world/turbine", "http://localhost:8080/myapp", //
  83. "", "http://localhost:8080/myapp/hello.world/turbine", //
  84. "http://localhost:8080/myapp/hello.world/turbine");
  85. add(data, true, "/hello.world/turbine", "", //
  86. "/hello.world/turbine", "http://localhost:8080/myapp", //
  87. "", "http://localhost:8080/myapp/hello.world/turbine", //
  88. "http://localhost:8080/myapp/hello.world/turbine");
  89. add(data, true, " hello.world/turbine/ ", " ", //
  90. "/hello.world/turbine/", "http://localhost:8080/myapp", //
  91. "", "http://localhost:8080/myapp/hello.world/turbine", //
  92. "http://localhost:8080/myapp/hello.world/turbine/");
  93. // 后缀映射
  94. add(data, false, "/aaa/bbb.htm", null, //
  95. "/aaa/bbb.htm", "http://localhost:8080/myapp", //
  96. "/aaa/bbb.htm", "http://localhost:8080/myapp", //
  97. "http://localhost:8080/myapp/aaa/bbb.htm");
  98. add(data, false, "/aaa/bbb.htm", "", //
  99. "/aaa/bbb.htm", "http://localhost:8080/myapp", //
  100. "/aaa/bbb.htm", "http://localhost:8080/myapp", //
  101. "http://localhost:8080/myapp/aaa/bbb.htm");
  102. add(data, false, " aaa/bbb.htm ", " ", //
  103. "/aaa/bbb.htm", "http://localhost:8080/myapp", //
  104. "/aaa/bbb.htm", "http://localhost:8080/myapp", //
  105. "http://localhost:8080/myapp/aaa/bbb.htm");
  106. return data;
  107. }
  108. private static void add(List<Object[]> data, Object... values) {
  109. data.add(values);
  110. }
  111. @Before
  112. public void init() {
  113. request = createMock(HttpServletRequest.class);
  114. expect(request.getRequestURL()).andReturn(new StringBuffer("http://localhost:8080/test")).anyTimes();
  115. expect(request.getContextPath()).andReturn(" myapp ").anyTimes();
  116. expect(request.getServletPath()).andReturn(servletPath).anyTimes();
  117. expect(request.getPathInfo()).andReturn(pathInfo).anyTimes();
  118. replay(request);
  119. }
  120. @Test
  121. public void isPrefixServletMapping_() {
  122. assertEquals(isPrefixServletMapping, isPrefixServletMapping(request));
  123. }
  124. @Test
  125. public void getResourcePath_() {
  126. assertEquals(resourcePath, getResourcePath(request));
  127. }
  128. @Test
  129. public void getBaseURL_() {
  130. assertEquals(baseURL, getBaseURL(request));
  131. }
  132. @Test
  133. public void getServletResourcePath_() {
  134. assertEquals(servletResourcePath, getServletResourcePath(request));
  135. }
  136. @Test
  137. public void getServletBaseURL_() {
  138. assertEquals(servletBaseURL, getServletBaseURL(request));
  139. }
  140. @Test
  141. public void getFullURL_() {
  142. assertEquals(fullURL, getBaseURL(request) + getResourcePath(request));
  143. String servletFullURL = getServletBaseURL(request) + getServletResourcePath(request);
  144. assertTrue(fullURL.equals(servletFullURL) || fullURL.equals(servletFullURL + "/"));
  145. }
  146. @Test
  147. public void normalizePath_() {
  148. assertEquals("", normalizePath(null));
  149. assertEquals("", normalizePath(" "));
  150. assertEquals("/aa/bb", normalizePath(" //aa/bb "));
  151. assertEquals("/", normalizePath(" /"));
  152. assertEquals("/", normalizePath(" // "));
  153. assertEquals("/aa/bb/", normalizePath(" //aa/bb/ "));
  154. assertEquals("/aa/bb/", normalizePath(" //aa/bb/ "));
  155. }
  156. @Test
  157. public void normalizePath_removeTrailingSlash() {
  158. assertEquals("", normalizePath(null, false));
  159. assertEquals("", normalizePath(" ", false));
  160. assertEquals("/aa/bb", normalizePath(" //aa/bb ", false));
  161. assertEquals("", normalizePath(" /", false));
  162. assertEquals("", normalizePath(" // ", false));
  163. assertEquals("/aa/bb", normalizePath(" //aa/bb/ ", false));
  164. }
  165. private String normalizePath(String path) {
  166. return FileUtil.normalizeAbsolutePath(path, false);
  167. }
  168. private String normalizePath(String path, boolean reserveTrailingSlash) {
  169. return FileUtil.normalizeAbsolutePath(path, !reserveTrailingSlash);
  170. }
  171. @Test
  172. public void normalizeURI_() {
  173. assertEquals("", normalizeURI(null));
  174. assertEquals("", normalizeURI(" "));
  175. assertEquals("aaa/bbb/ccc", normalizeURI("aaa/bbb/ccc"));
  176. assertEquals("aaa/ccc", normalizeURI("aaa///bbb/../ccc"));
  177. assertEquals("/aaa/bbb/ccc", normalizeURI("/aaa/bbb/ccc"));
  178. assertEquals("/aaa/bbb/ccc/", normalizeURI("/aaa/bbb/ccc/"));
  179. assertEquals("http://localhost:8080/bbb/ccc/", normalizeURI("http://localhost:8080//aaa//../bbb/ccc/"));
  180. }
  181. @Test
  182. public void startsWithPath_() {
  183. // null
  184. assertFalse(startsWithPath(null, "aa"));
  185. assertFalse(startsWithPath("aa", null));
  186. assertFalse(startsWithPath(null, null));
  187. // path/
  188. assertTrue(startsWithPath("path/", "path/index"));
  189. assertFalse(startsWithPath("path/", "path2/index"));
  190. // path
  191. assertTrue(startsWithPath("path", "path/index"));
  192. assertFalse(startsWithPath("path", "path2/index"));
  193. // equals
  194. assertTrue(startsWithPath("path", "path"));
  195. assertTrue(startsWithPath("path/", "path/"));
  196. // fullpath is shorter
  197. assertFalse(startsWithPath("path", "pat"));
  198. assertFalse(startsWithPath("path/", "pat"));
  199. }
  200. }