-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlersTest.java
More file actions
152 lines (119 loc) · 5 KB
/
Copy pathHandlersTest.java
File metadata and controls
152 lines (119 loc) · 5 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.retailsvc.http;
import static com.retailsvc.http.spec.HttpMethod.GET;
import static com.retailsvc.http.spec.HttpMethod.HEAD;
import static com.retailsvc.http.spec.HttpMethod.POST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.retailsvc.http.internal.BodyWriter;
import com.retailsvc.http.spec.HttpMethod;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.UnaryOperator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
class HandlersTest {
private static final UnaryOperator<String> NO_HEADERS = name -> null;
private static Request request(HttpMethod method) {
return new Request(new byte[0], null, null, null, Map.of(), null, NO_HEADERS, Map.of(), method);
}
@Test
void aliveHandlerReturns204OnGet() {
Response resp = Handlers.aliveHandler().handle(request(GET));
assertThat(resp.status()).isEqualTo(204);
assertThat(resp.body()).isNull();
}
@Test
void aliveHandlerReturns204OnHead() {
Response resp = Handlers.aliveHandler().handle(request(HEAD));
assertThat(resp.status()).isEqualTo(204);
}
@Test
void aliveHandlerReturns405OnPost() {
Response resp = Handlers.aliveHandler().handle(request(POST));
assertThat(resp.status()).isEqualTo(405);
assertThat(resp.headers()).containsEntry("Allow", "GET, HEAD");
}
@Test
void resourceHandlerStreamsYamlBytesWithInferredContentType() throws IOException {
Response resp = Handlers.resourceHandler("/openapi.yaml").handle(request(GET));
assertThat(resp.status()).isEqualTo(200);
assertThat(resp.contentType()).isEqualTo("application/yaml");
assertThat(write(resp)).isEqualTo(readClasspath("/openapi.yaml"));
}
@Test
void resourceHandlerInfersJsonContentType() {
Response resp = Handlers.resourceHandler("/openapi.json").handle(request(GET));
assertThat(resp.contentType()).isEqualTo("application/json");
}
@Test
void resourceHandlerThrowsAtConstructionForMissingClasspathResource() {
assertThatThrownBy(() -> Handlers.resourceHandler("/does-not-exist.yaml"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("/does-not-exist.yaml");
}
@Test
void resourceHandlerReturns405OnPost() {
Response resp = Handlers.resourceHandler("/openapi.yaml").handle(request(POST));
assertThat(resp.status()).isEqualTo(405);
assertThat(resp.headers()).containsEntry("Allow", "GET, HEAD");
}
@Test
void resourceHandlerHeadReturnsContentLengthWithoutBody() {
Response resp = Handlers.resourceHandler("/openapi.yaml").handle(request(HEAD));
assertThat(resp.status()).isEqualTo(200);
assertThat(resp.body()).isNull();
assertThat(resp.headers()).containsKey("Content-Length");
assertThat(Integer.parseInt(resp.headers().get("Content-Length"))).isGreaterThan(0);
}
@Test
void resourceHandlerOpensClasspathStreamLazilyPerRequest() throws IOException {
RequestHandler handler = Handlers.resourceHandler("/openapi.yaml");
byte[] first = write(handler.handle(request(GET)));
byte[] second = write(handler.handle(request(GET)));
assertThat(first).isEqualTo(second).isNotEmpty();
}
@Test
void resourceHandlerServesFilesystemFile(@TempDir Path tmp) throws IOException {
Path file = tmp.resolve("page.html");
Files.writeString(file, "<h1>hi</h1>");
Response resp = Handlers.resourceHandler(file).handle(request(GET));
assertThat(resp.status()).isEqualTo(200);
assertThat(resp.contentType()).isEqualTo("text/html; charset=utf-8");
assertThat(new String(write(resp))).isEqualTo("<h1>hi</h1>");
}
@Test
void resourceHandlerHeadOnFilesystemFileReportsContentLength(@TempDir Path tmp)
throws IOException {
Path file = tmp.resolve("data.txt");
Files.writeString(file, "hello");
Response resp = Handlers.resourceHandler(file).handle(request(HEAD));
assertThat(resp.status()).isEqualTo(200);
assertThat(resp.body()).isNull();
assertThat(resp.headers()).containsEntry("Content-Length", "5");
}
@Test
void resourceHandlerThrowsAtConstructionForMissingFile(@TempDir Path tmp) {
Path missing = tmp.resolve("nope.txt");
assertThatThrownBy(() -> Handlers.resourceHandler(missing))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("nope.txt");
}
private static byte[] write(Response resp) throws IOException {
assertThat(resp.body()).isInstanceOf(BodyWriter.class);
ByteArrayOutputStream out = new ByteArrayOutputStream();
((BodyWriter) resp.body()).writeTo(out);
return out.toByteArray();
}
private static byte[] readClasspath(String path) throws IOException {
try (InputStream in = HandlersTest.class.getResourceAsStream(path)) {
if (in == null) {
throw new IOException("missing fixture: " + path);
}
return in.readAllBytes();
}
}
}