Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MP path based static content should use index.html (4.x) #4737

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion etc/copyright-exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
.MD
.p12
.txt
.sql
jaxb.index
/MANIFEST.MF
/README
Expand All @@ -35,6 +34,7 @@ jaxb.index
.p12
.bin
.vm
.sql
src/main/proto/
src/test/resources/keystore/
etc/javadoc/
Expand All @@ -55,3 +55,5 @@ target/
META-INF/services/
persistence_3_0.xjb
persistence_3_0.xsd
# excluded as this is a test file and we need to validate its content
src/test/resources/static/classpath/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021 Oracle and/or its affiliates.
* Copyright (c) 2018, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -288,9 +288,10 @@ private void registerPathStaticContent(Config config) {
StaticContentSupport.FileSystemBuilder pBuilder = StaticContentSupport.builder(config.get("location")
.as(Path.class)
.get());
config.get("welcome")
pBuilder.welcomeFileName(config.get("welcome")
.asString()
.ifPresent(pBuilder::welcomeFileName);
.orElse("index.html"));

StaticContentSupport staticContent = pBuilder.build();

if (context.exists()) {
Expand Down
59 changes: 59 additions & 0 deletions tests/integration/mp-gh-4654/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2022 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.helidon.tests.integration</groupId>
<artifactId>helidon-tests-integration</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-tests-integration-mp-gh-4654</artifactId>
<name>Helidon Tests Integration MP GH 4654</name>
<description>Reproducer for Github issue #4654 - static content default index</description>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.server</groupId>
<artifactId>helidon-microprofile-server</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.tests.integration.gh4654;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

import io.helidon.config.mp.MpConfigSources;
import io.helidon.microprofile.server.Server;
import io.helidon.webserver.testsupport.TemporaryFolder;
import io.helidon.webserver.testsupport.TemporaryFolderExtension;

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@ExtendWith(TemporaryFolderExtension.class)
class Gh4654StaticContentTest {
private static Client client;
private TemporaryFolder folder;
private Server server;
private WebTarget target;

@BeforeAll
static void setupAll() {
client = ClientBuilder.newClient();
}

@AfterAll
static void cleanupAll() {
client.close();
client = null;
}

@BeforeEach
void setup() throws IOException {
// cannot use @HelidonTest, as the tmp folder extension requires to be run beforeEach

// root
Path root = folder.root().toPath();
Files.writeString(root.resolve("index.html"), "Root Index HTML");
Files.writeString(root.resolve("foo.txt"), "Foo TXT");
// css
Path cssDir = folder.newFolder("css").toPath();
Files.writeString(cssDir.resolve("a.css"), "A CSS");
// bar
Path other = folder.newFolder("other").toPath();
Files.writeString(other.resolve("index.html"), "Other Index");

ConfigProviderResolver cpr = ConfigProviderResolver.instance();
Config config = cpr.getBuilder()
.withSources(MpConfigSources.create(Map.of(
"server.host", "localhost",
"server.port", "0",
"server.static.path.location", folder.root().getAbsolutePath(),
"server.static.path.context", "/static",
"server.static.classpath.location", "/static",
"server.static.classpath.context", "/static"
)))
.build();
cpr.registerConfig(config, null);

server = Server.create()
.start();

target = client.target("http://localhost:" + server.port() + "/static");

}

@AfterEach
void cleanup() {
server.stop();
target = null;
}

@ParameterizedTest(name = "\"{0}\" - {2}")
@CsvSource({
"/,Root Index HTML,path should serve index.html",
"/index.html,Root Index HTML,path should serve index.html",
"/foo.txt,Foo TXT,path should serve foo.txt",
"/css/a.css,A CSS,path should serve css/a.css",
"/other,Other Index,path should serve other/index.html",
"/other/index.html,Other Index,path should serve other/index.html",
"/classpath,classpath index,classpath should serve index.html",
"/classpath/index.html,classpath index,classpath should serve index.html"
})
void testExists(String path, String expectedContent, String name) {
Response response = target.path(path)
.request()
.get();

assertThat(response.getStatus(), is(200));
assertThat(response.readEntity(String.class), is(expectedContent));
}

@ParameterizedTest(name = "{0}")
@CsvSource({
"/not-there.txt",
"/css/not-there.txt",
"/classpath/not-there.txt"
})
void test404(String path) {
Response response = target.path(path)
.request()
.get();

assertThat(response.getStatus(), is(404));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
classpath index
1 change: 1 addition & 0 deletions tests/integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<module>mp-gh-3246</module>
<module>mp-gh-3974</module>
<module>mp-gh-4123</module>
<module>mp-gh-4654</module>
<module>kafka</module>
<module>jms</module>
<module>config</module>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@
* Tests {@link io.helidon.webserver.staticcontent.FileSystemContentHandler}.
*/
@ExtendWith(TemporaryFolderExtension.class)
public class FileSystemContentHandlerTest {
class FileSystemContentHandlerTest {

private TemporaryFolder folder;

@BeforeEach
public void createContent() throws IOException {
// root
Path root = folder.root().toPath();
Files.write(root.resolve("index.html"), "Index HTML".getBytes(StandardCharsets.UTF_8));
Files.write(root.resolve("foo.txt"), "Foo TXT".getBytes(StandardCharsets.UTF_8));
Files.writeString(root.resolve("index.html"), "Index HTML");
Files.writeString(root.resolve("foo.txt"), "Foo TXT");
// css
Path cssDir = folder.newFolder("css").toPath();
Files.write(cssDir.resolve("a.css"), "A CSS".getBytes(StandardCharsets.UTF_8));
Files.write(cssDir.resolve("b.css"), "B CSS".getBytes(StandardCharsets.UTF_8));
Files.writeString(cssDir.resolve("a.css"), "A CSS");
Files.writeString(cssDir.resolve("b.css"), "B CSS");
// bar
Path other = folder.newFolder("other").toPath();
Files.write(other.resolve("index.html"), "Index HTML".getBytes(StandardCharsets.UTF_8));
Files.writeString(other.resolve("index.html"), "Index HTML");
}

static String responseToString(TestResponse response)
Expand All @@ -71,8 +71,7 @@ static String responseToString(TestResponse response)
}

@Test
public void serveFile() throws Exception {
try {
void serveFile() throws Exception {
Routing routing = Routing.builder()
.register("/some", StaticContentSupport.create(folder.root().toPath()))
.build();
Expand Down Expand Up @@ -105,13 +104,10 @@ public void serveFile() throws Exception {
.path("/some/css/")
.get();
assertThat(response.status(), is(Http.Status.NOT_FOUND_404));
} catch(Throwable ex){
ex.printStackTrace();
}
}

@Test
public void serveIndex() throws Exception {
void serveIndex() throws Exception {
Routing routing = Routing.builder()
.register(StaticContentSupport.builder(folder.root().toPath())
.welcomeFileName("index.html")
Expand Down