From b8edf21cbe4c05787db1d062d3b08c20dc23063a Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Thu, 24 Aug 2023 14:57:30 +0100 Subject: [PATCH] Fix Jetty with multiple static resources Currently Jetty responds with a 404 if there are multiple static-resources. I believe we need a Default servlet PER resource, and not a single one trying to handle it all. --- .../JettyStaticResourceResolutionSpec.groovy | 37 +++++++++++++++++++ .../src/test/resources/nest-test/index.html | 1 + .../resources/nest-test/nested/index.html | 5 +++ .../TomcatStaticResourceResolutionSpec.groovy | 37 +++++++++++++++++++ .../src/test/resources/nest-test/index.html | 1 + .../resources/nest-test/nested/index.html | 5 +++ ...ndertowStaticResourceResolutionSpec.groovy | 37 +++++++++++++++++++ .../src/test/resources/nest-test/index.html | 1 + .../resources/nest-test/nested/index.html | 5 +++ 9 files changed, 129 insertions(+) create mode 100644 http-server-jetty/src/test/resources/nest-test/index.html create mode 100644 http-server-jetty/src/test/resources/nest-test/nested/index.html create mode 100644 http-server-tomcat/src/test/resources/nest-test/index.html create mode 100644 http-server-tomcat/src/test/resources/nest-test/nested/index.html create mode 100644 http-server-undertow/src/test/resources/nest-test/index.html create mode 100644 http-server-undertow/src/test/resources/nest-test/nested/index.html diff --git a/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyStaticResourceResolutionSpec.groovy b/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyStaticResourceResolutionSpec.groovy index e0504769a..df6505ece 100644 --- a/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyStaticResourceResolutionSpec.groovy +++ b/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyStaticResourceResolutionSpec.groovy @@ -253,4 +253,41 @@ class JettyStaticResourceResolutionSpec extends Specification implements TestPro embeddedServer?.stop() embeddedServer?.close() } + + void "test resources with multiple nested mapping automatically resolves index.html"() { + given: + EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [ + 'micronaut.router.static-resources.nest-test.paths': ['classpath:nest-test'], + 'micronaut.router.static-resources.nest-test.mapping': '/nest-test/**', + 'micronaut.router.static-resources.nest.paths': ['classpath:nest-test/nested'], + 'micronaut.router.static-resources.nest.mapping': '/nest/**' + ]) + def client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL()).toBlocking() + + when: + def nestTestResponse = client.exchange(HttpRequest.GET("/nest-test"), String) + def nestTestText = JettyStaticResourceResolutionSpec.classLoader.getResource("nest-test/index.html").text + + def nestResponse = client.exchange(HttpRequest.GET("/nest"), String) + def nestText = JettyStaticResourceResolutionSpec.classLoader.getResource("nest-test/nested/index.html").text + + then: + with(nestTestResponse) { + code() == HttpStatus.OK.code + header(CONTENT_TYPE) == "text/html" + Integer.parseInt(header(CONTENT_LENGTH)) > 0 + body() == nestTestText + } + + with(nestResponse) { + code() == HttpStatus.OK.code + header(CONTENT_TYPE) == "text/html" + Integer.parseInt(header(CONTENT_LENGTH)) > 0 + body() == nestText + } + + cleanup: + embeddedServer.stop() + embeddedServer.close() + } } diff --git a/http-server-jetty/src/test/resources/nest-test/index.html b/http-server-jetty/src/test/resources/nest-test/index.html new file mode 100644 index 000000000..d44613946 --- /dev/null +++ b/http-server-jetty/src/test/resources/nest-test/index.html @@ -0,0 +1 @@ +

Test

diff --git a/http-server-jetty/src/test/resources/nest-test/nested/index.html b/http-server-jetty/src/test/resources/nest-test/nested/index.html new file mode 100644 index 000000000..87b44891e --- /dev/null +++ b/http-server-jetty/src/test/resources/nest-test/nested/index.html @@ -0,0 +1,5 @@ + + +

Nest

+ + diff --git a/http-server-tomcat/src/test/groovy/io/micronaut/servlet/tomcat/TomcatStaticResourceResolutionSpec.groovy b/http-server-tomcat/src/test/groovy/io/micronaut/servlet/tomcat/TomcatStaticResourceResolutionSpec.groovy index 01f2e7d9c..d99243eaa 100644 --- a/http-server-tomcat/src/test/groovy/io/micronaut/servlet/tomcat/TomcatStaticResourceResolutionSpec.groovy +++ b/http-server-tomcat/src/test/groovy/io/micronaut/servlet/tomcat/TomcatStaticResourceResolutionSpec.groovy @@ -251,4 +251,41 @@ class TomcatStaticResourceResolutionSpec extends Specification implements TestPr embeddedServer?.stop() embeddedServer?.close() } + + void "test resources with multiple nested mapping automatically resolves index.html"() { + given: + EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [ + 'micronaut.router.static-resources.nest-test.paths': ['classpath:nest-test'], + 'micronaut.router.static-resources.nest-test.mapping': '/nest-test/**', + 'micronaut.router.static-resources.nest.paths': ['classpath:nest-test/nested'], + 'micronaut.router.static-resources.nest.mapping': '/nest/**' + ]) + def client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL()).toBlocking() + + when: + def nestTestResponse = client.exchange(HttpRequest.GET("/nest-test"), String) + def nestTestText = TomcatStaticResourceResolutionSpec.classLoader.getResource("nest-test/index.html").text + + def nestResponse = client.exchange(HttpRequest.GET("/nest"), String) + def nestText = TomcatStaticResourceResolutionSpec.classLoader.getResource("nest-test/nested/index.html").text + + then: + with(nestTestResponse) { + code() == HttpStatus.OK.code + header(CONTENT_TYPE) == "text/html" + Integer.parseInt(header(CONTENT_LENGTH)) > 0 + body() == nestTestText + } + + with(nestResponse) { + code() == HttpStatus.OK.code + header(CONTENT_TYPE) == "text/html" + Integer.parseInt(header(CONTENT_LENGTH)) > 0 + body() == nestText + } + + cleanup: + embeddedServer.stop() + embeddedServer.close() + } } diff --git a/http-server-tomcat/src/test/resources/nest-test/index.html b/http-server-tomcat/src/test/resources/nest-test/index.html new file mode 100644 index 000000000..d44613946 --- /dev/null +++ b/http-server-tomcat/src/test/resources/nest-test/index.html @@ -0,0 +1 @@ +

Test

diff --git a/http-server-tomcat/src/test/resources/nest-test/nested/index.html b/http-server-tomcat/src/test/resources/nest-test/nested/index.html new file mode 100644 index 000000000..87b44891e --- /dev/null +++ b/http-server-tomcat/src/test/resources/nest-test/nested/index.html @@ -0,0 +1,5 @@ + + +

Nest

+ + diff --git a/http-server-undertow/src/test/groovy/io/micronaut/servlet/undertow/UndertowStaticResourceResolutionSpec.groovy b/http-server-undertow/src/test/groovy/io/micronaut/servlet/undertow/UndertowStaticResourceResolutionSpec.groovy index 4fa9647e6..c91cc783c 100644 --- a/http-server-undertow/src/test/groovy/io/micronaut/servlet/undertow/UndertowStaticResourceResolutionSpec.groovy +++ b/http-server-undertow/src/test/groovy/io/micronaut/servlet/undertow/UndertowStaticResourceResolutionSpec.groovy @@ -249,4 +249,41 @@ class UndertowStaticResourceResolutionSpec extends Specification implements Test embeddedServer?.stop() embeddedServer?.close() } + + void "test resources with multiple nested mapping automatically resolves index.html"() { + given: + EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [ + 'micronaut.router.static-resources.nest-test.paths': ['classpath:nest-test'], + 'micronaut.router.static-resources.nest-test.mapping': '/nest-test/**', + 'micronaut.router.static-resources.nest.paths': ['classpath:nest-test/nested'], + 'micronaut.router.static-resources.nest.mapping': '/nest/**' + ]) + def client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL()).toBlocking() + + when: + def nestTestResponse = client.exchange(HttpRequest.GET("/nest-test"), String) + def nestTestText = UndertowStaticResourceResolutionSpec.classLoader.getResource("nest-test/index.html").text + + def nestResponse = client.exchange(HttpRequest.GET("/nest"), String) + def nestText = UndertowStaticResourceResolutionSpec.classLoader.getResource("nest-test/nested/index.html").text + + then: + with(nestTestResponse) { + code() == HttpStatus.OK.code + header(CONTENT_TYPE) == "text/html" + Integer.parseInt(header(CONTENT_LENGTH)) > 0 + body() == nestTestText + } + + with(nestResponse) { + code() == HttpStatus.OK.code + header(CONTENT_TYPE) == "text/html" + Integer.parseInt(header(CONTENT_LENGTH)) > 0 + body() == nestText + } + + cleanup: + embeddedServer.stop() + embeddedServer.close() + } } diff --git a/http-server-undertow/src/test/resources/nest-test/index.html b/http-server-undertow/src/test/resources/nest-test/index.html new file mode 100644 index 000000000..d44613946 --- /dev/null +++ b/http-server-undertow/src/test/resources/nest-test/index.html @@ -0,0 +1 @@ +

Test

diff --git a/http-server-undertow/src/test/resources/nest-test/nested/index.html b/http-server-undertow/src/test/resources/nest-test/nested/index.html new file mode 100644 index 000000000..87b44891e --- /dev/null +++ b/http-server-undertow/src/test/resources/nest-test/nested/index.html @@ -0,0 +1,5 @@ + + +

Nest

+ +