Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestAbstractApplicationServletStaticFilesLocation.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 static org.junit.Assert.assertEquals;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import javax.servlet.http.HttpServletRequest;
  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. 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. assertEquals("./..", location);
  33. // http://dummy.host:8080/servlet
  34. // should return "."
  35. location = testLocation("http://dummy.host:8080", "", "/servlet", "");
  36. 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", "/servlet",
  40. "/extra/stuff");
  41. 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. 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()
  61. .getStaticFileLocation(servlet.createVaadinRequest(request));
  62. return location;
  63. }
  64. private HttpServletRequest createNonIncludeRequest(String base,
  65. String realContextPath, String realServletPath, String pathInfo)
  66. throws Exception {
  67. HttpServletRequest request = createRequest(base, realContextPath,
  68. realServletPath, pathInfo);
  69. expect(request.getAttribute("javax.servlet.include.context_path"))
  70. .andReturn(null).anyTimes();
  71. expect(request.getAttribute("javax.servlet.include.servlet_path"))
  72. .andReturn(null).anyTimes();
  73. return request;
  74. }
  75. /**
  76. * Creates a HttpServletRequest mock using the supplied parameters.
  77. *
  78. * @param base
  79. * The base url, e.g. http://localhost:8080
  80. * @param contextPath
  81. * The context path where the application is deployed, e.g.
  82. * /mycontext
  83. * @param servletPath
  84. * The servlet path to the servlet we are testing, e.g. /myapp
  85. * @param pathInfo
  86. * Any text following the servlet path in the request, not
  87. * including query parameters, e.g. /UIDL/
  88. * @return A mock HttpServletRequest object useful for testing
  89. * @throws MalformedURLException
  90. */
  91. private HttpServletRequest createRequest(String base, String contextPath,
  92. String servletPath, String pathInfo) throws MalformedURLException {
  93. URL url = new URL(base + contextPath + pathInfo);
  94. HttpServletRequest request = createMock(HttpServletRequest.class);
  95. expect(request.isSecure())
  96. .andReturn(url.getProtocol().equalsIgnoreCase("https"))
  97. .anyTimes();
  98. expect(request.getServerName()).andReturn(url.getHost()).anyTimes();
  99. expect(request.getServerPort()).andReturn(url.getPort()).anyTimes();
  100. expect(request.getRequestURI()).andReturn(url.getPath()).anyTimes();
  101. expect(request.getContextPath()).andReturn(contextPath).anyTimes();
  102. expect(request.getPathInfo()).andReturn(pathInfo).anyTimes();
  103. expect(request.getServletPath()).andReturn(servletPath).anyTimes();
  104. return request;
  105. }
  106. }