Skip to content

Commit 566af0a

Browse files
committed
Adds weight to static content feature
1 parent 1bfffcb commit 566af0a

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

webserver/static-content/src/main/java/io/helidon/webserver/staticcontent/StaticContentFeature.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2024, 2025 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -153,6 +153,11 @@ public String type() {
153153
return STATIC_CONTENT_ID;
154154
}
155155

156+
@Override
157+
public double weight() {
158+
return config.weight();
159+
}
160+
156161
@Override
157162
public void setup(ServerFeatureContext featureContext) {
158163
if (!enabled) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.helidon.webserver.tests.staticcontent;
18+
19+
import io.helidon.webclient.http1.Http1Client;
20+
import io.helidon.webserver.WebServer;
21+
import io.helidon.webserver.http.HttpRouting;
22+
import io.helidon.webserver.staticcontent.StaticContentFeature;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.hamcrest.CoreMatchers.is;
27+
import static org.hamcrest.MatcherAssert.assertThat;
28+
29+
class StaticContentFeatureTest {
30+
private static final String STATIC_CONTENT = "/staticcontent";
31+
private static final String ENTITY = "entity";
32+
33+
@Test
34+
void testDefaultWeight() {
35+
test(95, ENTITY);
36+
}
37+
38+
@Test
39+
void testHigherWeight() {
40+
test(101, "Welcome");
41+
}
42+
43+
void test(double weight, String expected) {
44+
WebServer server = WebServer.builder()
45+
.addFeature(StaticContentFeature.builder()
46+
.addClasspath(cl -> cl.location("static")
47+
.context(STATIC_CONTENT)
48+
.welcome("welcome.txt"))
49+
.weight(weight)
50+
.build())
51+
.addRouting(HttpRouting.builder()
52+
.get(STATIC_CONTENT, (req, res) -> res.send(ENTITY)))
53+
.port(0)
54+
.build()
55+
.start();
56+
57+
Http1Client client = Http1Client.builder()
58+
.baseUri("http://localhost:" + server.port())
59+
.build();
60+
61+
String response = client.get(STATIC_CONTENT).requestEntity(String.class);
62+
assertThat(response, is(expected));
63+
64+
server.stop();
65+
}
66+
}

0 commit comments

Comments
 (0)