|
| 1 | +package com.retailsvc.http; |
| 2 | + |
| 3 | +import static java.net.http.HttpClient.Version.HTTP_1_1; |
| 4 | +import static java.net.http.HttpResponse.BodyHandlers.ofString; |
| 5 | +import static java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor; |
| 6 | +import static org.assertj.core.api.Assertions.assertThat; |
| 7 | + |
| 8 | +import java.net.URI; |
| 9 | +import java.net.http.HttpClient; |
| 10 | +import java.net.http.HttpRequest; |
| 11 | +import java.net.http.HttpRequest.BodyPublishers; |
| 12 | +import java.util.Map; |
| 13 | +import org.junit.jupiter.api.Disabled; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +// TODO Task 9: remove @Disabled once Builder.handlers() accepts Map<String, RequestHandler> |
| 17 | +@Disabled("Enabled in Task 9 once handlers() takes RequestHandler") |
| 18 | +class RequestResponseGatewayTest extends ServerBaseTest { |
| 19 | + |
| 20 | + /** |
| 21 | + * Casts a {@code Map<String, RequestHandler>} to the raw {@code Map} type so callers compile |
| 22 | + * against the existing {@code Builder.handlers(Map<String, HttpHandler>)} signature. This cast is |
| 23 | + * safe to write here because the class is {@link Disabled} — it never runs until Task 9 replaces |
| 24 | + * the stub with a real {@code handlers(Map<String, RequestHandler>)} overload. |
| 25 | + */ |
| 26 | + @SuppressWarnings("unchecked") |
| 27 | + private static Map asRawHandlers(Map<String, RequestHandler> handlers) { |
| 28 | + return handlers; |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + void respondJsonWritesBodyAndContentType() throws Exception { |
| 33 | + RequestHandler echo = req -> req.respond(200).json(Map.of("op", req.operationId())); |
| 34 | + server = |
| 35 | + OpenApiServer.builder() |
| 36 | + .spec(spec) |
| 37 | + .handlers(asRawHandlers(Map.of("getRoot", echo, "postData", echo))) |
| 38 | + .port(0) |
| 39 | + .build(); |
| 40 | + HttpClient client = |
| 41 | + HttpClient.newBuilder() |
| 42 | + .executor(newVirtualThreadPerTaskExecutor()) |
| 43 | + .version(HTTP_1_1) |
| 44 | + .build(); |
| 45 | + var resp = |
| 46 | + client.send( |
| 47 | + HttpRequest.newBuilder() |
| 48 | + .uri(URI.create("http://localhost:%d/api/v1/data".formatted(server.listenPort()))) |
| 49 | + .header("Content-Type", "application/json") |
| 50 | + .POST(BodyPublishers.ofString("{\"n\":1}")) |
| 51 | + .build(), |
| 52 | + ofString()); |
| 53 | + assertThat(resp.statusCode()).isEqualTo(200); |
| 54 | + assertThat(resp.headers().firstValue("Content-Type")).contains("application/json"); |
| 55 | + assertThat(resp.body()).contains("\"op\":\"postData\""); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void respondEmptyUses204Style() throws Exception { |
| 60 | + RequestHandler ok = req -> req.respond(204).empty(); |
| 61 | + server = |
| 62 | + OpenApiServer.builder() |
| 63 | + .spec(spec) |
| 64 | + .handlers(asRawHandlers(Map.of("getRoot", ok, "postData", ok))) |
| 65 | + .port(0) |
| 66 | + .build(); |
| 67 | + var resp = |
| 68 | + HttpClient.newHttpClient() |
| 69 | + .send( |
| 70 | + HttpRequest.newBuilder() |
| 71 | + .uri(URI.create("http://localhost:%d/api/v1/".formatted(server.listenPort()))) |
| 72 | + .GET() |
| 73 | + .build(), |
| 74 | + ofString()); |
| 75 | + assertThat(resp.statusCode()).isEqualTo(204); |
| 76 | + assertThat(resp.body()).isEmpty(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void respondStreamUsesChunkedEncoding() throws Exception { |
| 81 | + RequestHandler streamer = |
| 82 | + req -> { |
| 83 | + try (var out = req.respond(200).contentType("text/plain").stream()) { |
| 84 | + out.write("hello ".getBytes()); |
| 85 | + out.write("world".getBytes()); |
| 86 | + } |
| 87 | + }; |
| 88 | + server = |
| 89 | + OpenApiServer.builder() |
| 90 | + .spec(spec) |
| 91 | + .handlers(asRawHandlers(Map.of("getRoot", streamer, "postData", streamer))) |
| 92 | + .port(0) |
| 93 | + .build(); |
| 94 | + var resp = |
| 95 | + HttpClient.newHttpClient() |
| 96 | + .send( |
| 97 | + HttpRequest.newBuilder() |
| 98 | + .uri(URI.create("http://localhost:%d/api/v1/".formatted(server.listenPort()))) |
| 99 | + .GET() |
| 100 | + .build(), |
| 101 | + ofString()); |
| 102 | + assertThat(resp.statusCode()).isEqualTo(200); |
| 103 | + assertThat(resp.body()).isEqualTo("hello world"); |
| 104 | + } |
| 105 | +} |
0 commit comments