-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtraHandlersIT.java
More file actions
114 lines (97 loc) · 3.41 KB
/
Copy pathExtraHandlersIT.java
File metadata and controls
114 lines (97 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.retailsvc.http;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.Map;
import org.junit.jupiter.api.Test;
class ExtraHandlersIT extends ServerBaseTest {
@Test
void aliveExtraReturns204AndBypassesValidation() throws Exception {
try (var s =
newBuilder()
.spec(spec)
.handlers(stubAllHandlers(Map.of()))
.port(0)
.extraRoute("/alive", Handlers.aliveHandler())
.build();
var client = httpClient()) {
var req =
HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + s.listenPort() + "/alive"))
.GET()
.build();
var resp = client.send(req, BodyHandlers.ofString());
assertThat(resp.statusCode()).isEqualTo(204);
assertThat(resp.body()).isEmpty();
}
}
@Test
void resourceHandlerServesClasspathResource() throws Exception {
try (var s =
newBuilder()
.spec(spec)
.handlers(stubAllHandlers(Map.of()))
.port(0)
.extraRoute("/openapi.yaml", Handlers.resourceHandler("/openapi.yaml"))
.build();
var client = httpClient()) {
var req =
HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + s.listenPort() + "/openapi.yaml"))
.GET()
.build();
var resp = client.send(req, BodyHandlers.ofString());
assertThat(resp.statusCode()).isEqualTo(200);
assertThat(resp.headers().firstValue("Content-Type")).contains("application/yaml");
assertThat(resp.body()).isNotEmpty();
}
}
@Test
void textHtmlResponseIsSerializedByDefaultMapper() throws Exception {
RequestHandler html =
req -> Response.of(200, "<h1>hi</h1>").withContentType("text/html; charset=UTF-8");
try (var s =
newBuilder()
.spec(spec)
.handlers(stubAllHandlers(Map.of()))
.port(0)
.extraRoute("/page", html)
.build();
var client = httpClient()) {
var req =
HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + s.listenPort() + "/page"))
.GET()
.build();
var resp = client.send(req, BodyHandlers.ofString());
assertThat(resp.statusCode()).isEqualTo(200);
assertThat(resp.headers().firstValue("Content-Type"))
.hasValueSatisfying(v -> assertThat(v).startsWith("text/html"));
assertThat(resp.body()).isEqualTo("<h1>hi</h1>");
}
}
@Test
void extraHandlerExceptionFlowsThroughExceptionHandler() throws Exception {
RequestHandler boom =
req -> {
throw new RuntimeException("boom");
};
try (var s =
newBuilder()
.spec(spec)
.handlers(stubAllHandlers(Map.of()))
.port(0)
.extraRoute("/boom", boom)
.build();
var client = httpClient()) {
var req =
HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + s.listenPort() + "/boom"))
.GET()
.build();
var resp = client.send(req, BodyHandlers.ofString());
assertThat(resp.statusCode()).isEqualTo(500);
}
}
}