@Test(expected = IllegalStateException.class) public void noPathWithinHandlerMappingAttribute() throws Exception { this.handler.handleRequest(this.request, this.response); }
private void testInvalidPath(String requestPath, ResourceHttpRequestHandler handler) throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, requestPath); this.response = new MockHttpServletResponse(); handler.handleRequest(this.request, this.response); assertEquals(HttpStatus.NOT_FOUND.value(), this.response.getStatus()); }
@Test(expected = HttpRequestMethodNotSupportedException.class) public void unsupportedHttpMethod() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.request.setMethod("POST"); this.handler.handleRequest(this.request, this.response); }
private void testResolvePathWithTraversal(Resource location, String requestPath) throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, requestPath); this.response = new MockHttpServletResponse(); this.handler.handleRequest(this.request, this.response); if (!location.createRelative(requestPath).exists() && !requestPath.contains(":")) { fail(requestPath + " doesn't actually exist as a relative path"); } assertEquals(HttpStatus.NOT_FOUND.value(), this.response.getStatus()); }
private void resourceNotFound(HttpMethod httpMethod) throws Exception { this.request.setMethod(httpMethod.name()); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "not-there.css"); this.handler.handleRequest(this.request, this.response); assertEquals(HttpStatus.NOT_FOUND.value(), this.response.getStatus()); }
@Test public void directoryInJarFile() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "underscorejs/"); this.handler.handleRequest(this.request, this.response); assertEquals(404, this.response.getStatus()); }
@Test public void directory() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "js/"); this.handler.handleRequest(this.request, this.response); assertEquals(404, this.response.getStatus()); }
@Test public void missingResourcePath() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, ""); this.handler.handleRequest(this.request, this.response); assertEquals(404, this.response.getStatus()); }
@Test // SPR-14005 public void doOverwriteExistingCacheControlHeaders() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.response.setHeader("Cache-Control", "no-store"); this.handler.handleRequest(this.request, this.response); assertEquals("max-age=3600", this.response.getHeader("Cache-Control")); }
@Test public void ignoreInvalidEscapeSequence() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/%foo%/bar.txt"); this.response = new MockHttpServletResponse(); this.handler.handleRequest(this.request, this.response); assertEquals(404, this.response.getStatus()); }
@Test public void getResourceFromSubDirectoryOfAlternatePath() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "js/baz.js"); this.handler.handleRequest(this.request, this.response); assertEquals("text/javascript", this.response.getContentType()); assertEquals("function foo() { console.log(\"hello world\"); }", this.response.getContentAsString()); }
@Test public void notModified() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.request.addHeader("If-Modified-Since", resourceLastModified("test/foo.css")); this.handler.handleRequest(this.request, this.response); assertEquals(HttpServletResponse.SC_NOT_MODIFIED, this.response.getStatus()); }
@Test public void getResourceFromSubDirectory() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "js/foo.js"); this.handler.handleRequest(this.request, this.response); assertEquals("text/javascript", this.response.getContentType()); assertEquals("function foo() { console.log(\"hello world\"); }", this.response.getContentAsString()); }
@Test public void getResourceHttpOptions() throws Exception { this.request.setMethod("OPTIONS"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.handler.handleRequest(this.request, this.response); assertEquals(200, this.response.getStatus()); assertEquals("GET,HEAD,OPTIONS", this.response.getHeader("Allow")); }
@Test public void getVersionedResource() throws Exception { VersionResourceResolver versionResolver = new VersionResourceResolver() .addFixedVersionStrategy("versionString", "/**"); this.handler.setResourceResolvers(Arrays.asList(versionResolver, new PathResourceResolver())); this.handler.afterPropertiesSet(); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "versionString/foo.css"); this.handler.handleRequest(this.request, this.response); assertEquals("\"versionString\"", this.response.getHeader("ETag")); assertEquals("bytes", this.response.getHeader("Accept-Ranges")); assertEquals(1, this.response.getHeaders("Accept-Ranges").size()); }
@Test public void mapPathToLocation() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("GET"); request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "/testStylesheet.css"); ResourceHttpRequestHandler handler = getHandler("/resources/**"); handler.handleRequest(request, this.response); assertEquals("test stylesheet content", this.response.getContentAsString()); }
@Test public void modified() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.request.addHeader("If-Modified-Since", resourceLastModified("test/foo.css") / 1000 * 1000 - 1); this.handler.handleRequest(this.request, this.response); assertEquals(HttpServletResponse.SC_OK, this.response.getStatus()); assertEquals("h1 { color:red; }", this.response.getContentAsString()); }
@Test public void getResourceNoCache() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css"); this.handler.setCacheSeconds(0); this.handler.handleRequest(this.request, this.response); assertEquals("no-store", this.response.getHeader("Cache-Control")); assertTrue(this.response.containsHeader("Last-Modified")); assertEquals(resourceLastModified("test/foo.css") / 1000, this.response.getDateHeader("Last-Modified") / 1000); assertEquals("bytes", this.response.getHeader("Accept-Ranges")); assertEquals(1, this.response.getHeaders("Accept-Ranges").size()); }
@Test public void getResourceWithHtmlMediaType() throws Exception { this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html"); this.handler.handleRequest(this.request, this.response); assertEquals("text/html", this.response.getContentType()); assertEquals("max-age=3600", this.response.getHeader("Cache-Control")); assertTrue(this.response.containsHeader("Last-Modified")); assertEquals(resourceLastModified("test/foo.html") / 1000, this.response.getDateHeader("Last-Modified") / 1000); assertEquals("bytes", this.response.getHeader("Accept-Ranges")); assertEquals(1, this.response.getHeaders("Accept-Ranges").size()); }
@Test public void partialContentByteRangeNoEnd() throws Exception { this.request.addHeader("Range", "bytes=9-"); this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.txt"); this.handler.handleRequest(this.request, this.response); assertEquals(206, this.response.getStatus()); assertEquals("text/plain", this.response.getContentType()); assertEquals(1, this.response.getContentLength()); assertEquals("bytes 9-9/10", this.response.getHeader("Content-Range")); assertEquals(".", this.response.getContentAsString()); assertEquals("bytes", this.response.getHeader("Accept-Ranges")); assertEquals(1, this.response.getHeaders("Accept-Ranges").size()); }