-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTest.java
More file actions
218 lines (184 loc) · 6.32 KB
/
Copy pathRequestTest.java
File metadata and controls
218 lines (184 loc) · 6.32 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.retailsvc.http;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.retailsvc.http.internal.DispatchHandler;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.UnaryOperator;
import org.junit.jupiter.api.Test;
class RequestTest {
private static final UnaryOperator<String> NO_HEADERS = name -> null;
private static UnaryOperator<String> headers(String... pairs) {
Map<String, String> map = new java.util.HashMap<>();
for (int i = 0; i < pairs.length; i += 2) {
map.put(pairs[i].toLowerCase(), pairs[i + 1]);
}
return name -> map.get(name.toLowerCase());
}
@Test
void readsBoundContext() throws Exception {
Request req =
new Request(
new byte[] {1, 2, 3},
Map.of("k", "v"),
null,
"get-x",
Map.of("id", "42"),
null,
NO_HEADERS);
AtomicReference<byte[]> seenBytes = new AtomicReference<>();
AtomicReference<Object> seenParsed = new AtomicReference<>();
AtomicReference<String> seenOpId = new AtomicReference<>();
AtomicReference<Map<String, String>> seenPathParams = new AtomicReference<>();
ScopedValue.where(DispatchHandler.CURRENT, req)
.call(
() -> {
Request r = DispatchHandler.CURRENT.get();
seenBytes.set(r.bytes());
seenParsed.set(r.parsed());
seenOpId.set(r.operationId());
seenPathParams.set(r.pathParams());
return null;
});
assertThat(seenBytes.get()).containsExactly(1, 2, 3);
assertThat(seenParsed.get()).isEqualTo(Map.of("k", "v"));
assertThat(seenOpId.get()).isEqualTo("get-x");
assertThat(seenPathParams.get()).containsEntry("id", "42");
}
@Test
void asPojoDeserialisesViaTypedMapper() {
Jackson2JsonTypeMapper mapper = new Jackson2JsonTypeMapper(new ObjectMapper());
byte[] body = "{\"id\":\"x-1\",\"qty\":7}".getBytes(StandardCharsets.UTF_8);
Request req =
new Request(
body,
Map.of("id", "x-1", "qty", 7),
mapper,
"op",
Map.of(),
null,
headers("Content-Type", "application/json"));
Item item = req.asPojo(Item.class);
assertThat(item.id).isEqualTo("x-1");
assertThat(item.qty).isEqualTo(7);
}
@Test
void asPojoFastPathWhenParsedAlreadyMatchesType() {
Map<String, Object> alreadyParsed = Map.of("k", "v");
Request req =
new Request("x".getBytes(), alreadyParsed, null, "op", Map.of(), null, NO_HEADERS);
Map<?, ?> result = req.asPojo(Map.class);
assertThat(result).isSameAs(alreadyParsed);
}
@Test
void asPojoThrowsWhenBodyEmpty() {
Request req = new Request(new byte[0], null, null, "op", Map.of(), null, NO_HEADERS);
assertThatThrownBy(() -> req.asPojo(Item.class))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("no body");
}
@Test
void asPojoThrowsWhenMapperNotTyped() {
TypeMapper plain =
new TypeMapper() {
@Override
public Object readFrom(byte[] b, String h) {
return new String(b, StandardCharsets.UTF_8);
}
@Override
public byte[] writeTo(Object v) {
return v.toString().getBytes(StandardCharsets.UTF_8);
}
};
Request req =
new Request(
"hello".getBytes(),
"hello",
plain,
"op",
Map.of(),
null,
headers("Content-Type", "text/plain"));
assertThatThrownBy(() -> req.asPojo(Item.class))
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining("TypedTypeMapper");
}
static final class Item {
public String id;
public int qty;
}
@Test
void pathParamReturnsValueOrNull() {
Request req = new Request(new byte[0], null, null, "op", Map.of("id", "42"), null, NO_HEADERS);
assertThat(req.pathParam("id")).isEqualTo("42");
assertThat(req.pathParam("missing")).isNull();
}
@Test
void exposesQueryParams() {
Request req =
new Request(
new byte[0],
null,
null,
"op",
Map.of(),
"name=Alice%20Smith&active=true&active=false",
NO_HEADERS);
assertThat(req.rawQuery()).isEqualTo("name=Alice%20Smith&active=true&active=false");
assertThat(req.queryParam("name")).contains("Alice Smith");
assertThat(req.queryParam("active")).contains("true");
assertThat(req.queryParam("missing")).isEmpty();
assertThat(req.queryParams())
.containsEntry("name", "Alice Smith")
.containsEntry("active", "true");
}
@Test
void queryParamsEmptyWhenNoQuery() {
Request req = new Request(new byte[0], null, null, "op", Map.of(), null, NO_HEADERS);
assertThat(req.rawQuery()).isNull();
assertThat(req.queryParams()).isEmpty();
assertThat(req.queryParam("anything")).isEmpty();
}
@Test
void queryParamBlankIsTreatedAsAbsent() {
Request req =
new Request(new byte[0], null, null, "op", Map.of(), "limit=&offset=%20", NO_HEADERS);
assertThat(req.queryParam("limit")).isEmpty();
assertThat(req.queryParam("offset")).isEmpty();
}
@Test
void contentTypeShortcutsContentTypeHeader() {
Request req =
new Request(
new byte[0],
null,
null,
"op",
Map.of(),
null,
headers("Content-Type", "application/json"));
assertThat(req.contentType()).contains("application/json");
}
@Test
void contentTypeEmptyWhenHeaderAbsent() {
Request req = new Request(new byte[0], null, null, "op", Map.of(), null, NO_HEADERS);
assertThat(req.contentType()).isEmpty();
}
@Test
void headerReturnsOptionalAndBlankIsAbsent() {
Request req =
new Request(
new byte[0],
null,
null,
"op",
Map.of(),
null,
headers("X-Trace", "abc", "X-Empty", " "));
assertThat(req.header("X-Trace")).contains("abc");
assertThat(req.header("X-Empty")).isEmpty();
assertThat(req.header("Missing")).isEmpty();
}
}