|
| 1 | +package com.retailsvc.http.internal; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +class ProblemDetailRendererTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + void rendersAllFieldsWhenPresent() { |
| 12 | + ProblemDetail pd = |
| 13 | + new ProblemDetail("about:blank", "Bad Request", 400, "expected string", "/x", "type"); |
| 14 | + assertThat(asString(ProblemDetailRenderer.renderJson(pd))) |
| 15 | + .isEqualTo( |
| 16 | + "{\"type\":\"about:blank\",\"title\":\"Bad Request\",\"status\":400," |
| 17 | + + "\"detail\":\"expected string\",\"pointer\":\"/x\",\"keyword\":\"type\"}"); |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + void omitsNullPointerAndKeyword() { |
| 22 | + ProblemDetail pd = |
| 23 | + new ProblemDetail("about:blank", "Unauthorized", 401, "missing token", null, null); |
| 24 | + assertThat(asString(ProblemDetailRenderer.renderJson(pd))) |
| 25 | + .isEqualTo( |
| 26 | + "{\"type\":\"about:blank\",\"title\":\"Unauthorized\",\"status\":401," |
| 27 | + + "\"detail\":\"missing token\"}"); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void omitsNullDetail() { |
| 32 | + ProblemDetail pd = new ProblemDetail("about:blank", "Not Found", 404, null, null, null); |
| 33 | + assertThat(asString(ProblemDetailRenderer.renderJson(pd))) |
| 34 | + .isEqualTo("{\"type\":\"about:blank\",\"title\":\"Not Found\",\"status\":404}"); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void escapesQuoteAndBackslashInDetail() { |
| 39 | + ProblemDetail pd = new ProblemDetail("about:blank", "Bad Request", 400, "a\"b\\c", null, null); |
| 40 | + assertThat(asString(ProblemDetailRenderer.renderJson(pd))) |
| 41 | + .isEqualTo( |
| 42 | + "{\"type\":\"about:blank\",\"title\":\"Bad Request\",\"status\":400," |
| 43 | + + "\"detail\":\"a\\\"b\\\\c\"}"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void escapesNamedControlCharsInDetail() { |
| 48 | + ProblemDetail pd = |
| 49 | + new ProblemDetail("about:blank", "Bad Request", 400, "\b\f\n\r\t", null, null); |
| 50 | + assertThat(asString(ProblemDetailRenderer.renderJson(pd))) |
| 51 | + .isEqualTo( |
| 52 | + "{\"type\":\"about:blank\",\"title\":\"Bad Request\",\"status\":400," |
| 53 | + + "\"detail\":\"\\b\\f\\n\\r\\t\"}"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void escapesUnnamedControlCharsAsHexUnicode() { |
| 58 | + ProblemDetail pd = new ProblemDetail("about:blank", "Bad Request", 400, "", null, null); |
| 59 | + assertThat(asString(ProblemDetailRenderer.renderJson(pd))) |
| 60 | + .isEqualTo( |
| 61 | + "{\"type\":\"about:blank\",\"title\":\"Bad Request\",\"status\":400," |
| 62 | + + "\"detail\":\"\\u0001\\u001f\"}"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void passesThroughNonAsciiCharactersVerbatim() { |
| 67 | + ProblemDetail pd = new ProblemDetail("about:blank", "Bad Request", 400, "café-é", null, null); |
| 68 | + assertThat(asString(ProblemDetailRenderer.renderJson(pd))) |
| 69 | + .isEqualTo( |
| 70 | + "{\"type\":\"about:blank\",\"title\":\"Bad" |
| 71 | + + " Request\",\"status\":400,\"detail\":\"café-é\"}"); |
| 72 | + } |
| 73 | + |
| 74 | + private static String asString(byte[] bytes) { |
| 75 | + return new String(bytes, StandardCharsets.UTF_8); |
| 76 | + } |
| 77 | +} |
0 commit comments