|
| 1 | +package com.retailsvc.http; |
| 2 | + |
| 3 | +import static java.net.http.HttpRequest.BodyPublishers.ofString; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | + |
| 6 | +import com.retailsvc.http.start.FormEchoHandler; |
| 7 | +import com.retailsvc.http.start.TextEchoHandler; |
| 8 | +import com.sun.net.httpserver.HttpHandler; |
| 9 | +import java.net.URI; |
| 10 | +import java.net.http.HttpRequest; |
| 11 | +import java.net.http.HttpResponse.BodyHandlers; |
| 12 | +import java.util.Map; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +class NonJsonBodyIT extends ServerBaseTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + void formUrlEncodedBodyParsedAndCoerced() throws Exception { |
| 19 | + Map<String, HttpHandler> handlers = Map.of("form-echo", new FormEchoHandler()); |
| 20 | + try (var s = newServer(handlers); |
| 21 | + var client = httpClient()) { |
| 22 | + var req = postForm(s, "/form-echo", "name=foo&age=30"); |
| 23 | + var resp = client.send(req, BodyHandlers.ofString()); |
| 24 | + assertThat(resp.statusCode()).isEqualTo(200); |
| 25 | + assertThat(resp.body()).contains("name=foo").contains("age=30"); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void formArrayProperty() throws Exception { |
| 31 | + Map<String, HttpHandler> handlers = Map.of("form-echo", new FormEchoHandler()); |
| 32 | + try (var s = newServer(handlers); |
| 33 | + var client = httpClient()) { |
| 34 | + var req = postForm(s, "/form-echo", "tags=a&tags=b"); |
| 35 | + var resp = client.send(req, BodyHandlers.ofString()); |
| 36 | + assertThat(resp.statusCode()).isEqualTo(200); |
| 37 | + assertThat(resp.body()).contains("tags=[a, b]"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void formCoercionFailureReturns400() throws Exception { |
| 43 | + Map<String, HttpHandler> handlers = Map.of("form-echo", new FormEchoHandler()); |
| 44 | + try (var s = newServer(handlers); |
| 45 | + var client = httpClient()) { |
| 46 | + var req = postForm(s, "/form-echo", "age=abc"); |
| 47 | + var resp = client.send(req, BodyHandlers.ofString()); |
| 48 | + assertThat(resp.statusCode()).isEqualTo(400); |
| 49 | + assertThat(resp.body()).contains("/age"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void textPlainBodyParsedAsString() throws Exception { |
| 55 | + Map<String, HttpHandler> handlers = Map.of("text-echo", new TextEchoHandler()); |
| 56 | + try (var s = newServer(handlers); |
| 57 | + var client = httpClient()) { |
| 58 | + var req = postWithContentType(s, "/text-echo", "hello", "text/plain; charset=utf-8"); |
| 59 | + var resp = client.send(req, BodyHandlers.ofString()); |
| 60 | + assertThat(resp.statusCode()).isEqualTo(200); |
| 61 | + assertThat(resp.body()).isEqualTo("hello"); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void formBodyAgainstJsonOnlyOperationReturns400() throws Exception { |
| 67 | + try (var s = newServer(Map.of()); |
| 68 | + var client = httpClient()) { |
| 69 | + var req = postForm(s, "/data", "name=foo"); |
| 70 | + var resp = client.send(req, BodyHandlers.ofString()); |
| 71 | + assertThat(resp.statusCode()).isEqualTo(400); |
| 72 | + assertThat(resp.body()).contains("content-type"); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static HttpRequest postForm(OpenApiServer s, String path, String body) { |
| 77 | + return postWithContentType(s, path, body, "application/x-www-form-urlencoded"); |
| 78 | + } |
| 79 | + |
| 80 | + private static HttpRequest postWithContentType( |
| 81 | + OpenApiServer s, String path, String body, String contentType) { |
| 82 | + return HttpRequest.newBuilder() |
| 83 | + .uri(URI.create("http://localhost:" + s.listenPort() + "/api/v1" + path)) |
| 84 | + .header("Content-Type", contentType) |
| 85 | + .POST(ofString(body)) |
| 86 | + .build(); |
| 87 | + } |
| 88 | +} |
0 commit comments