forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMvcTest.java
More file actions
437 lines (356 loc) · 12.7 KB
/
MvcTest.java
File metadata and controls
437 lines (356 loc) · 12.7 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package io.jooby;
import examples.InstanceRouter;
import examples.JAXRS;
import examples.LoopDispatch;
import examples.Message;
import examples.MvcBody;
import examples.MyValueRouter;
import examples.NoTopLevelPath;
import examples.NullInjection;
import examples.ProducesConsumes;
import examples.Provisioning;
import examples.TopDispatch;
import io.jooby.json.JacksonModule;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import javax.annotation.Nonnull;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.concurrent.Executors;
import static io.jooby.MediaType.xml;
import static okhttp3.RequestBody.create;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class MvcTest {
@ServerTest
public void routerInstance(ServerTestRunner runner) {
runner.define(app -> {
app.mvc(new InstanceRouter());
}).ready(client -> {
client.get("/", rsp -> {
assertEquals("Got it!", rsp.body().string());
});
client.post("/", rsp -> {
assertEquals("Got it!", rsp.body().string());
});
client.get("/subpath", rsp -> {
assertEquals("OK", rsp.body().string());
});
client.get("/void", rsp -> {
assertEquals("", rsp.body().string());
assertEquals(204, rsp.code());
});
client.get("/voidwriter", rsp -> {
assertEquals("writer", rsp.body().string().trim());
assertEquals(200, rsp.code());
});
});
}
@ServerTest
public void routerImporting(ServerTestRunner runner) {
runner.define(app -> {
Jooby sub = new Jooby();
sub.mvc(new InstanceRouter());
app.use("/sub", sub);
}).ready(client -> {
client.get("/sub", rsp -> {
assertEquals("Got it!", rsp.body().string());
});
client.post("/sub", rsp -> {
assertEquals("Got it!", rsp.body().string());
});
client.get("/sub/subpath", rsp -> {
assertEquals("OK", rsp.body().string());
});
client.get("/sub/void", rsp -> {
assertEquals("", rsp.body().string());
assertEquals(204, rsp.code());
});
client.get("/sub/voidwriter", rsp -> {
assertEquals("writer", rsp.body().string().trim());
assertEquals(200, rsp.code());
});
});
}
@ServerTest
public void producesAndConsumes(ServerTestRunner runner) {
runner.define(app -> {
app.encoder(io.jooby.MediaType.json, (@Nonnull Context ctx, @Nonnull Object value) ->
("{" + value.toString() + "}").getBytes(StandardCharsets.UTF_8)
);
app.encoder(io.jooby.MediaType.xml, (@Nonnull Context ctx, @Nonnull Object value) ->
("<" + value.toString() + ">").getBytes(StandardCharsets.UTF_8)
);
app.decoder(io.jooby.MediaType.json, new MessageDecoder() {
@Nonnull @Override public Message decode(@Nonnull Context ctx, @Nonnull Type type)
throws Exception {
return new Message("{" + ctx.body().value() + "}");
}
});
app.decoder(xml, new MessageDecoder() {
@Nonnull @Override public Message decode(@Nonnull Context ctx, @Nonnull Type type)
throws Exception {
return new Message("<" + ctx.body().value() + ">");
}
});
app.mvc(new ProducesConsumes());
}).ready(client -> {
client.header("Accept", "application/json");
client.get("/produces", rsp -> {
assertEquals("{MVC}", rsp.body().string());
});
client.header("Accept", "application/xml");
client.get("/produces", rsp -> {
assertEquals("<MVC>", rsp.body().string());
});
client.header("Accept", "text/html");
client.get("/produces", rsp -> {
assertEquals(406, rsp.code());
});
client.header("Content-Type", "application/json");
client.get("/consumes", rsp -> {
assertEquals("{}", rsp.body().string());
});
client.header("Content-Type", "application/xml");
client.get("/consumes", rsp -> {
assertEquals("<>", rsp.body().string());
});
client.header("Content-Type", "text/plain");
client.get("/consumes", rsp -> {
assertEquals(415, rsp.code());
});
client.get("/consumes", rsp -> {
assertEquals(415, rsp.code());
});
});
}
@ServerTest
public void jaxrs(ServerTestRunner runner) {
runner.define(app -> {
app.mvc(new JAXRS());
}).ready(client -> {
client.get("/jaxrs", rsp -> {
assertEquals("Got it!", rsp.body().string());
});
});
}
@ServerTest
public void noTopLevelPath(ServerTestRunner runner) {
runner.define(app -> {
app.mvc(new NoTopLevelPath());
}).ready(client -> {
client.get("/", rsp -> {
assertEquals("root", rsp.body().string());
});
client.get("/subpath", rsp -> {
assertEquals("subpath", rsp.body().string());
});
});
}
@ServerTest
public void provisioning(ServerTestRunner runner) {
runner.define(app -> {
app.mvc(new Provisioning());
}).ready(client -> {
client.get("/args/ctx", rsp -> {
assertEquals("/args/ctx", rsp.body().string());
});
client.header("Cookie", "jooby.flash=success=OK").get("/args/flash", rsp -> {
assertEquals("{success=OK}OK", rsp.body().string());
});
client.get("/args/sendStatusCode", rsp -> {
assertEquals("", rsp.body().string());
assertEquals(201, rsp.code());
});
client.get("/args/file/foo.txt", rsp -> {
assertEquals("foo.txt", rsp.body().string());
});
client.get("/args/int/678", rsp -> {
assertEquals("678", rsp.body().string());
});
client.get("/args/foo/678/9/3.14/6.66/true", rsp -> {
assertEquals("GET/foo/678/9/3.14/6.66/true", rsp.body().string());
});
client.get("/args/long/678", rsp -> {
assertEquals("678", rsp.body().string());
});
client.get("/args/float/67.8", rsp -> {
assertEquals("67.8", rsp.body().string());
});
client.get("/args/double/67.8", rsp -> {
assertEquals("67.8", rsp.body().string());
});
client.get("/args/bool/false", rsp -> {
assertEquals("false", rsp.body().string());
});
client.get("/args/str/*", rsp -> {
assertEquals("*", rsp.body().string());
});
client.get("/args/list/*", rsp -> {
assertEquals("[*]", rsp.body().string());
});
client.get("/args/custom/3.14", rsp -> {
assertEquals("3.14", rsp.body().string());
});
client.get("/args/search?q=*", rsp -> {
assertEquals("*", rsp.body().string());
});
client.get("/args/querystring?q=*", rsp -> {
assertEquals("{q=*}", rsp.body().string());
});
client.get("/args/search-opt", rsp -> {
assertEquals("Optional.empty", rsp.body().string());
});
client.header("foo", "bar");
client.get("/args/header", rsp -> {
assertEquals("bar", rsp.body().string());
});
client.post("/args/form", new FormBody.Builder().add("foo", "ab").build(), rsp -> {
assertEquals("ab", rsp.body().string());
});
client.post("/args/formdata", new FormBody.Builder().add("foo", "ab").build(), rsp -> {
assertEquals("{foo=ab}", rsp.body().string());
});
client.post("/args/multipart", new FormBody.Builder().add("foo", "ab").build(), rsp -> {
assertEquals("{foo=ab}", rsp.body().string());
});
client.post("/args/multipart",
new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("foo", "...")
.build(), rsp -> {
assertEquals("{foo=...}", rsp.body().string());
});
client.post("/args/form",
new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("foo", "...")
.build(), rsp -> {
assertEquals("...", rsp.body().string());
});
client.header("Cookie", "foo=bar");
client.get("/args/cookie", rsp -> {
assertEquals("bar", rsp.body().string());
});
});
}
@ServerTest
public void nullinjection(ServerTestRunner runner) {
runner.define(app -> {
app.mvc(new NullInjection());
app.error((ctx, cause, statusCode) -> {
app.getLog().error("{} {}", ctx.getMethod(), ctx.getRequestPath(), cause);
ctx.setResponseCode(statusCode)
.send(cause.getMessage());
});
}).ready(client -> {
client.get("/nonnull", rsp -> {
assertEquals("Missing value: 'v'", rsp.body().string());
});
client.get("/nullok", rsp -> {
assertEquals("null", rsp.body().string());
});
client.get("/nullbean", rsp -> {
assertEquals(
"Unable to provision parameter: 'foo: int', require by: constructor examples.NullInjection.QParam(int, java.lang.Integer)",
rsp.body().string());
});
client.get("/nullbean?foo=foo", rsp -> {
assertEquals(
"Unable to provision parameter: 'foo: int', require by: constructor examples.NullInjection.QParam(int, java.lang.Integer)",
rsp.body().string());
});
client.get("/nullbean?foo=0&baz=baz", rsp -> {
assertEquals(
"Unable to provision parameter: 'baz: int', require by: method examples.NullInjection.QParam.setBaz(int)",
rsp.body().string());
});
});
}
@ServerTest
public void mvcBody(ServerTestRunner runner) {
runner.define(app -> {
app.install(new JacksonModule());
app.mvc(new MvcBody());
app.error((ctx, cause, statusCode) -> {
app.getLog()
.error("{} {} {}", ctx.getMethod(), ctx.getRequestPath(), statusCode.value(), cause);
ctx.setResponseCode(statusCode)
.send(cause.getMessage());
});
}).ready(client -> {
client.header("Content-Type", "application/json");
client.post("/body/json", create("{\"foo\": \"bar\"}", MediaType.get("application/json")),
rsp -> {
assertEquals("{foo=bar}null", rsp.body().string());
});
client.header("Content-Type", "text/plain");
client.post("/body/str", create("...", MediaType.get("text/plain")), rsp -> {
assertEquals("...", rsp.body().string());
});
client.header("Content-Type", "text/plain");
client.post("/body/int", create("8", MediaType.get("text/plain")), rsp -> {
assertEquals("8", rsp.body().string());
});
client.post("/body/int", create("8x", MediaType.get("text/plain")), rsp -> {
assertEquals("Cannot convert value: 'body', to: 'int'", rsp.body().string());
});
client.header("Content-Type", "application/json");
client.post("/body/json", create("{\"foo\"= \"bar\"}", MediaType.get("application/json")),
rsp -> {
assertEquals(400, rsp.code());
});
client.header("Content-Type", "application/json");
client.post("/body/json?type=x",
create("{\"foo\": \"bar\"}", MediaType.get("application/json")), rsp -> {
assertEquals("{foo=bar}x", rsp.body().string());
});
});
}
@ServerTest
public void beanConverter(ServerTestRunner runner) {
runner.define(app -> {
app.converter(new MyValueBeanConverter());
app.mvc(new MyValueRouter());
}).ready(client -> {
client.get("/myvalue?string=query", rsp -> {
assertEquals("query", rsp.body().string());
});
});
}
@ServerTest(executionMode = ExecutionMode.EVENT_LOOP)
public void mvcDispatch(ServerTestRunner runner) {
runner.define(app -> {
app.executor("single", Executors.newSingleThreadExecutor(r ->
new Thread(r, "single")
));
app.mvc(new TopDispatch());
}).ready(client -> {
client.get("/", rsp -> {
String body = rsp.body().string();
assertTrue(body.startsWith("worker"), body);
});
client.get("/method", rsp -> {
assertEquals("single", rsp.body().string());
});
});
}
@ServerTest(executionMode = ExecutionMode.EVENT_LOOP)
public void mvcLoopDispatch(ServerTestRunner runner) {
runner.define(app -> {
app.executor("single", Executors.newSingleThreadExecutor(r ->
new Thread(r, "single")
));
app.mvc(new LoopDispatch());
}).ready(client -> {
client.get("/", rsp -> {
String body = rsp.body().string();
assertTrue(runner.matchesEventLoopThread(body), body);
});
client.get("/method", rsp -> {
assertEquals("single", rsp.body().string());
});
});
}
}