Skip to content

Commit 6f29b49

Browse files
committed
test: Integration coverage for extra handlers and builder
1 parent d36982a commit 6f29b49

1 file changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.retailsvc.http;
2+
3+
import static com.retailsvc.http.Handlers.defaultExceptionHandler;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
import static org.junit.jupiter.api.Assertions.fail;
6+
7+
import com.sun.net.httpserver.HttpHandler;
8+
import java.io.IOException;
9+
import java.net.URI;
10+
import java.net.http.HttpRequest;
11+
import java.net.http.HttpResponse.BodyHandlers;
12+
import java.util.Map;
13+
import org.junit.jupiter.api.Test;
14+
15+
class ExtraHandlersIT extends ServerBaseTest {
16+
17+
@Test
18+
void aliveExtraReturns204AndBypassesValidation() throws Exception {
19+
try (var s =
20+
OpenApiServer.builder()
21+
.spec(spec)
22+
.jsonMapper(jsonMapper())
23+
.handlers(Map.of())
24+
.exceptionHandler(defaultExceptionHandler())
25+
.port(0)
26+
.addHandler("/alive", Handlers.aliveHandler())
27+
.build();
28+
var client = httpClient()) {
29+
30+
var req =
31+
HttpRequest.newBuilder()
32+
.uri(URI.create("http://localhost:" + s.listenPort() + "/alive"))
33+
.GET()
34+
.build();
35+
var resp = client.send(req, BodyHandlers.ofString());
36+
37+
assertThat(resp.statusCode()).isEqualTo(204);
38+
assertThat(resp.body()).isEmpty();
39+
}
40+
}
41+
42+
@Test
43+
void specHandlerServesClasspathResource() throws Exception {
44+
try (var s =
45+
OpenApiServer.builder()
46+
.spec(spec)
47+
.jsonMapper(jsonMapper())
48+
.handlers(Map.of())
49+
.exceptionHandler(defaultExceptionHandler())
50+
.port(0)
51+
.addHandler("/openapi.yaml", Handlers.specHandler("/openapi.yaml"))
52+
.build();
53+
var client = httpClient()) {
54+
55+
var req =
56+
HttpRequest.newBuilder()
57+
.uri(URI.create("http://localhost:" + s.listenPort() + "/openapi.yaml"))
58+
.GET()
59+
.build();
60+
var resp = client.send(req, BodyHandlers.ofString());
61+
62+
assertThat(resp.statusCode()).isEqualTo(200);
63+
assertThat(resp.headers().firstValue("Content-Type")).contains("application/yaml");
64+
assertThat(resp.body()).isNotEmpty();
65+
}
66+
}
67+
68+
@Test
69+
void extraHandlerExceptionFlowsThroughExceptionHandler() throws Exception {
70+
HttpHandler boom =
71+
ex -> {
72+
throw new RuntimeException("boom");
73+
};
74+
75+
try (var s =
76+
OpenApiServer.builder()
77+
.spec(spec)
78+
.jsonMapper(jsonMapper())
79+
.handlers(Map.of())
80+
.exceptionHandler(defaultExceptionHandler())
81+
.port(0)
82+
.addHandler("/boom", boom)
83+
.build();
84+
var client = httpClient()) {
85+
86+
var req =
87+
HttpRequest.newBuilder()
88+
.uri(URI.create("http://localhost:" + s.listenPort() + "/boom"))
89+
.GET()
90+
.build();
91+
var resp = client.send(req, BodyHandlers.ofString());
92+
93+
assertThat(resp.statusCode()).isEqualTo(500);
94+
}
95+
}
96+
97+
@Test
98+
void existingPublicConstructorStillWorks() {
99+
try {
100+
try (var s = new OpenApiServer(spec, jsonMapper(), Map.of(), defaultExceptionHandler(), 0)) {
101+
assertThat(s.listenPort()).isGreaterThan(0);
102+
}
103+
} catch (IOException io) {
104+
fail(io);
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)