PageRenderTime 45ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/servlet/test/com/google/inject/servlet/ContextPathTest.java

https://gitlab.com/metamorphiccode/guice
Java | 293 lines | 171 code | 52 blank | 70 comment | 0 complexity | fdf99190e75a433d98f39ea2043825bf MD5 | raw file
  1. /**
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.inject.servlet;
  17. import static org.easymock.EasyMock.createControl;
  18. import static org.easymock.EasyMock.expect;
  19. import com.google.inject.Guice;
  20. import com.google.inject.Inject;
  21. import com.google.inject.Injector;
  22. import com.google.inject.Key;
  23. import com.google.inject.Scopes;
  24. import com.google.inject.name.Named;
  25. import com.google.inject.name.Names;
  26. import junit.framework.TestCase;
  27. import org.easymock.IMocksControl;
  28. import java.io.IOException;
  29. import javax.servlet.FilterChain;
  30. import javax.servlet.FilterConfig;
  31. import javax.servlet.ServletContext;
  32. import javax.servlet.ServletException;
  33. import javax.servlet.ServletRequest;
  34. import javax.servlet.ServletResponse;
  35. import javax.servlet.http.HttpServlet;
  36. import javax.servlet.http.HttpServletRequest;
  37. import javax.servlet.http.HttpServletResponse;
  38. /** Tests to make sure that servlets with a context path are handled right. */
  39. public class ContextPathTest extends TestCase {
  40. @Inject @Named("foo")
  41. private TestServlet fooServlet;
  42. @Inject @Named("bar")
  43. private TestServlet barServlet;
  44. private IMocksControl globalControl;
  45. private Injector injector;
  46. private ServletContext servletContext;
  47. private FilterConfig filterConfig;
  48. private GuiceFilter guiceFilter;
  49. @Override
  50. public final void setUp() throws Exception {
  51. injector = Guice.createInjector(new ServletModule() {
  52. @Override
  53. protected void configureServlets() {
  54. bind(TestServlet.class).annotatedWith(Names.named("foo"))
  55. .to(TestServlet.class).in(Scopes.SINGLETON);
  56. bind(TestServlet.class).annotatedWith(Names.named("bar"))
  57. .to(TestServlet.class).in(Scopes.SINGLETON);
  58. serve("/foo/*").with(Key.get(TestServlet.class, Names.named("foo")));
  59. serve("/bar/*").with(Key.get(TestServlet.class, Names.named("bar")));
  60. // TODO: add a filter(..) call and validate it is correct
  61. }
  62. });
  63. injector.injectMembers(this);
  64. assertNotNull(fooServlet);
  65. assertNotNull(barServlet);
  66. assertNotSame(fooServlet, barServlet);
  67. globalControl = createControl();
  68. servletContext = globalControl.createMock(ServletContext.class);
  69. filterConfig = globalControl.createMock(FilterConfig.class);
  70. expect(servletContext.getAttribute(GuiceServletContextListener.INJECTOR_NAME))
  71. .andReturn(injector).anyTimes();
  72. expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
  73. globalControl.replay();
  74. guiceFilter = new GuiceFilter();
  75. guiceFilter.init(filterConfig);
  76. }
  77. @Override
  78. public final void tearDown() {
  79. assertNotNull(fooServlet);
  80. assertNotNull(barServlet);
  81. fooServlet = null;
  82. barServlet = null;
  83. guiceFilter.destroy();
  84. guiceFilter = null;
  85. injector = null;
  86. filterConfig = null;
  87. servletContext = null;
  88. globalControl.verify();
  89. }
  90. public void testSimple() throws Exception {
  91. IMocksControl testControl = createControl();
  92. TestFilterChain testFilterChain = new TestFilterChain();
  93. HttpServletRequest req = testControl.createMock(HttpServletRequest.class);
  94. HttpServletResponse res = testControl.createMock(HttpServletResponse.class);
  95. expect(req.getMethod()).andReturn("GET").anyTimes();
  96. expect(req.getRequestURI()).andReturn("/bar/foo").anyTimes();
  97. expect(req.getServletPath()).andReturn("/bar/foo").anyTimes();
  98. expect(req.getContextPath()).andReturn("").anyTimes();
  99. testControl.replay();
  100. guiceFilter.doFilter(req, res, testFilterChain);
  101. assertFalse(testFilterChain.isTriggered());
  102. assertFalse(fooServlet.isTriggered());
  103. assertTrue(barServlet.isTriggered());
  104. testControl.verify();
  105. }
  106. //
  107. // each of the following "runTest" calls takes three path parameters:
  108. //
  109. // The value of "getRequestURI()"
  110. // The value of "getServletPath()"
  111. // The value of "getContextPath()"
  112. //
  113. // these values have been captured using a filter in Apache Tomcat 6.0.32
  114. // and are used for real-world values that a servlet container would send into
  115. // the GuiceFilter.
  116. //
  117. // the remaining three booleans are:
  118. //
  119. // True if the request gets passed down the filter chain
  120. // True if the request hits the "foo" servlet
  121. // True if the request hits the "bar" sevlet
  122. //
  123. // After adjusting the request URI for the web app deployment location, all
  124. // calls
  125. // should always produce the same result.
  126. //
  127. // ROOT Web app, using Tomcat default servlet
  128. public void testRootDefault() throws Exception {
  129. // fetching /. Should go up the filter chain (only mappings on /foo/* and /bar/*).
  130. runTest("/", "/", "", true, false, false);
  131. // fetching /bar/. Should hit the bar servlet
  132. runTest("/bar/", "/bar/", "", false, false, true);
  133. // fetching /foo/xxx. Should hit the foo servlet
  134. runTest("/foo/xxx", "/foo/xxx", "", false, true, false);
  135. // fetching /xxx. Should go up the chain
  136. runTest("/xxx", "/xxx", "", true, false, false);
  137. }
  138. // ROOT Web app, using explicit backing servlet mounted at /*
  139. public void testRootExplicit() throws Exception {
  140. // fetching /. Should go up the filter chain (only mappings on /foo/* and /bar/*).
  141. runTest("/", "", "", true, false, false);
  142. // fetching /bar/. Should hit the bar servlet
  143. runTest("/bar/", "", "", false, false, true);
  144. // fetching /foo/xxx. Should hit the foo servlet
  145. runTest("/foo/xxx", "", "", false, true, false);
  146. // fetching /xxx. Should go up the chain
  147. runTest("/xxx", "", "", true, false, false);
  148. }
  149. // ROOT Web app, using two backing servlets, mounted at /bar/* and /foo/*
  150. public void testRootSpecific() throws Exception {
  151. // fetching /. Should go up the filter chain (only mappings on /foo/* and /bar/*).
  152. runTest("/", "/", "", true, false, false);
  153. // fetching /bar/. Should hit the bar servlet
  154. runTest("/bar/", "/bar", "", false, false, true);
  155. // fetching /foo/xxx. Should hit the foo servlet
  156. runTest("/foo/xxx", "/foo", "", false, true, false);
  157. // fetching /xxx. Should go up the chain
  158. runTest("/xxx", "/xxx", "", true, false, false);
  159. }
  160. // Web app located at /webtest, using Tomcat default servlet
  161. public void testWebtestDefault() throws Exception {
  162. // fetching /. Should go up the filter chain (only mappings on /foo/* and /bar/*).
  163. runTest("/webtest/", "/", "/webtest", true, false, false);
  164. // fetching /bar/. Should hit the bar servlet
  165. runTest("/webtest/bar/", "/bar/", "/webtest", false, false, true);
  166. // fetching /foo/xxx. Should hit the foo servlet
  167. runTest("/webtest/foo/xxx", "/foo/xxx", "/webtest", false, true, false);
  168. // fetching /xxx. Should go up the chain
  169. runTest("/webtest/xxx", "/xxx", "/webtest", true, false, false);
  170. }
  171. // Web app located at /webtest, using explicit backing servlet mounted at /*
  172. public void testWebtestExplicit() throws Exception {
  173. // fetching /. Should go up the filter chain (only mappings on /foo/* and /bar/*).
  174. runTest("/webtest/", "", "/webtest", true, false, false);
  175. // fetching /bar/. Should hit the bar servlet
  176. runTest("/webtest/bar/", "", "/webtest", false, false, true);
  177. // fetching /foo/xxx. Should hit the foo servlet
  178. runTest("/webtest/foo/xxx", "", "/webtest", false, true, false);
  179. // fetching /xxx. Should go up the chain
  180. runTest("/webtest/xxx", "", "/webtest", true, false, false);
  181. }
  182. // Web app located at /webtest, using two backing servlets, mounted at /bar/*
  183. // and /foo/*
  184. public void testWebtestSpecific() throws Exception {
  185. // fetching /. Should go up the filter chain (only mappings on /foo/* and
  186. // /bar/*).
  187. runTest("/webtest/", "/", "/webtest", true, false, false);
  188. // fetching /bar/. Should hit the bar servlet
  189. runTest("/webtest/bar/", "/bar", "/webtest", false, false, true);
  190. // fetching /foo/xxx. Should hit the foo servlet
  191. runTest("/webtest/foo/xxx", "/foo", "/webtest", false, true, false);
  192. // fetching /xxx. Should go up the chain
  193. runTest("/webtest/xxx", "/xxx", "/webtest", true, false, false);
  194. }
  195. private void runTest(final String requestURI, final String servletPath, final String contextPath,
  196. final boolean filterResult, final boolean fooResult, final boolean barResult)
  197. throws Exception {
  198. IMocksControl testControl = createControl();
  199. barServlet.clear();
  200. fooServlet.clear();
  201. TestFilterChain testFilterChain = new TestFilterChain();
  202. HttpServletRequest req = testControl.createMock(HttpServletRequest.class);
  203. HttpServletResponse res = testControl.createMock(HttpServletResponse.class);
  204. expect(req.getMethod()).andReturn("GET").anyTimes();
  205. expect(req.getRequestURI()).andReturn(requestURI).anyTimes();
  206. expect(req.getServletPath()).andReturn(servletPath).anyTimes();
  207. expect(req.getContextPath()).andReturn(contextPath).anyTimes();
  208. testControl.replay();
  209. guiceFilter.doFilter(req, res, testFilterChain);
  210. assertEquals(filterResult, testFilterChain.isTriggered());
  211. assertEquals(fooResult, fooServlet.isTriggered());
  212. assertEquals(barResult, barServlet.isTriggered());
  213. testControl.verify();
  214. }
  215. public static class TestServlet extends HttpServlet {
  216. private boolean triggered = false;
  217. @Override
  218. public void doGet(HttpServletRequest req, HttpServletResponse resp) {
  219. triggered = true;
  220. }
  221. public boolean isTriggered() {
  222. return triggered;
  223. }
  224. public void clear() {
  225. triggered = false;
  226. }
  227. }
  228. public static class TestFilterChain implements FilterChain {
  229. private boolean triggered = false;
  230. public void doFilter(ServletRequest request, ServletResponse response) throws IOException,
  231. ServletException {
  232. triggered = true;
  233. }
  234. public boolean isTriggered() {
  235. return triggered;
  236. }
  237. public void clear() {
  238. triggered = false;
  239. }
  240. }
  241. }