You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestAbstractApplicationServletStaticFilesLocation.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.vaadin.server;
  2. import static org.easymock.EasyMock.createMock;
  3. import static org.easymock.EasyMock.expect;
  4. import static org.easymock.EasyMock.replay;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7. import javax.servlet.http.HttpServletRequest;
  8. import org.junit.Assert;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. public class TestAbstractApplicationServletStaticFilesLocation {
  12. VaadinServlet servlet;
  13. // private Method getStaticFilesLocationMethod;
  14. @Before
  15. public void setUp() throws Exception {
  16. servlet = new VaadinServlet();
  17. servlet.init(new MockServletConfig());
  18. }
  19. @Test
  20. public void testWidgetSetLocation() throws Exception {
  21. String location;
  22. /* SERVLETS */
  23. // http://dummy.host:8080/contextpath/servlet
  24. // should return . (relative url resolving to /contextpath)
  25. location = testLocation("http://dummy.host:8080", "/contextpath",
  26. "/servlet", "");
  27. Assert.assertEquals(".", location);
  28. // http://dummy.host:8080/contextpath/servlet/
  29. // should return ./.. (relative url resolving to /contextpath)
  30. location = testLocation("http://dummy.host:8080", "/contextpath",
  31. "/servlet", "/");
  32. Assert.assertEquals("./..", location);
  33. // http://dummy.host:8080/servlet
  34. // should return "."
  35. location = testLocation("http://dummy.host:8080", "", "/servlet", "");
  36. Assert.assertEquals(".", location);
  37. // http://dummy.host/contextpath/servlet/extra/stuff
  38. // should return ./../.. (relative url resolving to /contextpath)
  39. location = testLocation("http://dummy.host", "/contextpath",
  40. "/servlet", "/extra/stuff");
  41. Assert.assertEquals("./../..", location);
  42. // http://dummy.host/context/path/servlet/extra/stuff
  43. // should return ./../.. (relative url resolving to /context/path)
  44. location = testLocation("http://dummy.host", "/context/path",
  45. "/servlet", "/extra/stuff");
  46. Assert.assertEquals("./../..", location);
  47. /* Include requests */
  48. // Include request support dropped with support for portlet1
  49. // Might reconsider when JSP integration support is implemented
  50. // location = testIncludedLocation("http://my.portlet.server", "/user",
  51. // "/tmpservletlocation1", "");
  52. // assertEquals("Wrong widgetset location", "/user", location);
  53. }
  54. private String testLocation(String base, String contextPath,
  55. String servletPath, String pathInfo) throws Exception {
  56. HttpServletRequest request = createNonIncludeRequest(base, contextPath,
  57. servletPath, pathInfo);
  58. // Set request into replay mode
  59. replay(request);
  60. String location = servlet.getService().getStaticFileLocation(
  61. servlet.createVaadinRequest(request));
  62. return location;
  63. }
  64. private String testIncludedLocation(String base, String portletContextPath,
  65. String servletPath, String pathInfo) throws Exception {
  66. HttpServletRequest request = createIncludeRequest(base,
  67. portletContextPath, servletPath, pathInfo);
  68. // Set request into replay mode
  69. replay(request);
  70. String location = servlet.getService().getStaticFileLocation(
  71. servlet.createVaadinRequest(request));
  72. return location;
  73. }
  74. private HttpServletRequest createIncludeRequest(String base,
  75. String realContextPath, String realServletPath, String pathInfo)
  76. throws Exception {
  77. HttpServletRequest request = createRequest(base, "", "", pathInfo);
  78. expect(request.getAttribute("javax.servlet.include.context_path"))
  79. .andReturn(realContextPath).anyTimes();
  80. expect(request.getAttribute("javax.servlet.include.servlet_path"))
  81. .andReturn(realServletPath).anyTimes();
  82. return request;
  83. }
  84. private HttpServletRequest createNonIncludeRequest(String base,
  85. String realContextPath, String realServletPath, String pathInfo)
  86. throws Exception {
  87. HttpServletRequest request = createRequest(base, realContextPath,
  88. realServletPath, pathInfo);
  89. expect(request.getAttribute("javax.servlet.include.context_path"))
  90. .andReturn(null).anyTimes();
  91. expect(request.getAttribute("javax.servlet.include.servlet_path"))
  92. .andReturn(null).anyTimes();
  93. return request;
  94. }
  95. /**
  96. * Creates a HttpServletRequest mock using the supplied parameters.
  97. *
  98. * @param base
  99. * The base url, e.g. http://localhost:8080
  100. * @param contextPath
  101. * The context path where the application is deployed, e.g.
  102. * /mycontext
  103. * @param servletPath
  104. * The servlet path to the servlet we are testing, e.g. /myapp
  105. * @param pathInfo
  106. * Any text following the servlet path in the request, not
  107. * including query parameters, e.g. /UIDL/
  108. * @return A mock HttpServletRequest object useful for testing
  109. * @throws MalformedURLException
  110. */
  111. private HttpServletRequest createRequest(String base, String contextPath,
  112. String servletPath, String pathInfo) throws MalformedURLException {
  113. URL url = new URL(base + contextPath + pathInfo);
  114. HttpServletRequest request = createMock(HttpServletRequest.class);
  115. expect(request.isSecure()).andReturn(
  116. url.getProtocol().equalsIgnoreCase("https")).anyTimes();
  117. expect(request.getServerName()).andReturn(url.getHost()).anyTimes();
  118. expect(request.getServerPort()).andReturn(url.getPort()).anyTimes();
  119. expect(request.getRequestURI()).andReturn(url.getPath()).anyTimes();
  120. expect(request.getContextPath()).andReturn(contextPath).anyTimes();
  121. expect(request.getPathInfo()).andReturn(pathInfo).anyTimes();
  122. expect(request.getServletPath()).andReturn(servletPath).anyTimes();
  123. return request;
  124. }
  125. }