|
| 1 | +package dev.eidentification.bankid.internal.http; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import dev.eidentification.bankid.UnitTest; |
| 5 | +import dev.eidentification.bankid.client.response.ErrorResponse; |
| 6 | +import dev.eidentification.bankid.client.response.SignResponse; |
| 7 | +import dev.eidentification.bankid.exceptions.BankIdApiErrorException; |
| 8 | +import dev.eidentification.bankid.exceptions.BankIdApiUnexpectedResponseException; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.net.http.HttpClient; |
| 13 | +import java.net.http.HttpHeaders; |
| 14 | +import java.net.http.HttpResponse; |
| 15 | +import java.nio.ByteBuffer; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | +import java.util.concurrent.CompletionException; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.TimeoutException; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.fail; |
| 26 | + |
| 27 | +class JsonBodyHandlerUnitTest extends UnitTest { |
| 28 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 29 | + |
| 30 | + @Test |
| 31 | + void givenStatus200AndValidJson_whenApply_thenParsesResponse() { |
| 32 | + final JsonBodyHandler<SignResponse, ErrorResponse> handler = |
| 33 | + new JsonBodyHandler<>(SignResponse.class, ErrorResponse.class, objectMapper); |
| 34 | + |
| 35 | + final HttpResponse.ResponseInfo info = responseInfo(200, "application/json; charset=utf-8"); |
| 36 | + |
| 37 | + final String json = """ |
| 38 | + { |
| 39 | + "orderRef":"131daac9-16c6-4618-beb0-365768f37288", |
| 40 | + "autoStartToken":"7c40b5c9-fa74-49cf-b98c-bfe651f9a7c6" |
| 41 | + }"""; |
| 42 | + |
| 43 | + final HttpResponse.BodySubscriber<SignResponse> subscriber = handler.apply(info); |
| 44 | + final SignResponse result = complete(feed(subscriber, json)); |
| 45 | + |
| 46 | + Assertions.assertNotNull(result); |
| 47 | + Assertions.assertEquals("131daac9-16c6-4618-beb0-365768f37288", result.getOrderRef()); |
| 48 | + Assertions.assertEquals("7c40b5c9-fa74-49cf-b98c-bfe651f9a7c6", result.getAutoStartToken()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void givenStatus200AndInvalidJson_whenApply_thenThrowsUnexpectedResponse() { |
| 53 | + final JsonBodyHandler<SignResponse, ErrorResponse> handler = |
| 54 | + new JsonBodyHandler<>(SignResponse.class, ErrorResponse.class, objectMapper); |
| 55 | + final HttpResponse.ResponseInfo info = responseInfo(200, "application/json"); |
| 56 | + final String invalidJson = "not-a-json"; |
| 57 | + |
| 58 | + final HttpResponse.BodySubscriber<SignResponse> subscriber = handler.apply(info); |
| 59 | + |
| 60 | + final BankIdApiUnexpectedResponseException ex = Assertions.assertThrows( |
| 61 | + BankIdApiUnexpectedResponseException.class, |
| 62 | + () -> complete(feed(subscriber, invalidJson)) |
| 63 | + ); |
| 64 | + Assertions.assertTrue(ex.getMessage() != null && !ex.getMessage().isBlank()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void givenNon200AndMissingContentType_whenApply_thenThrowsUnexpectedResponse() { |
| 69 | + final JsonBodyHandler<SignResponse, ErrorResponse> handler = |
| 70 | + new JsonBodyHandler<>(SignResponse.class, ErrorResponse.class, objectMapper); |
| 71 | + final HttpResponse.ResponseInfo info = responseInfo(500, null); |
| 72 | + final String body = "<html>Server error</html>"; |
| 73 | + |
| 74 | + final HttpResponse.BodySubscriber<SignResponse> subscriber = handler.apply(info); |
| 75 | + |
| 76 | + Assertions.assertThrows(BankIdApiUnexpectedResponseException.class, () -> complete(feed(subscriber, body))); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void givenNon200AndNonJsonContentType_whenApply_thenThrowsUnexpectedResponse() { |
| 81 | + final JsonBodyHandler<SignResponse, ErrorResponse> handler = |
| 82 | + new JsonBodyHandler<>(SignResponse.class, ErrorResponse.class, objectMapper); |
| 83 | + final HttpResponse.ResponseInfo info = responseInfo(404, "text/plain"); |
| 84 | + final String body = "Not found"; |
| 85 | + |
| 86 | + final HttpResponse.BodySubscriber<SignResponse> subscriber = handler.apply(info); |
| 87 | + |
| 88 | + Assertions.assertThrows(BankIdApiUnexpectedResponseException.class, () -> complete(feed(subscriber, body))); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void givenNon200AndJsonContentTypeWithValidJson_whenApply_thenThrowsApiError() { |
| 93 | + final JsonBodyHandler<SignResponse, ErrorResponse> handler = |
| 94 | + new JsonBodyHandler<>(SignResponse.class, ErrorResponse.class, objectMapper); |
| 95 | + final HttpResponse.ResponseInfo info = responseInfo(400, "application/json"); |
| 96 | + final String errorJson = "{\"errorCode\":\"BAD_REQUEST\",\"details\":\"Invalid input\"}"; |
| 97 | + |
| 98 | + final HttpResponse.BodySubscriber<SignResponse> subscriber = handler.apply(info); |
| 99 | + |
| 100 | + Assertions.assertThrows(BankIdApiErrorException.class, () -> complete(feed(subscriber, errorJson))); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void givenNon200AndJsonContentTypeWithInvalidJson_whenApply_thenThrowsUnexpectedResponse() { |
| 105 | + final JsonBodyHandler<SignResponse, ErrorResponse> handler = |
| 106 | + new JsonBodyHandler<>(SignResponse.class, ErrorResponse.class, objectMapper); |
| 107 | + final HttpResponse.ResponseInfo info = responseInfo(500, "Application/JSON; charset=UTF-8"); // case-insensitive |
| 108 | + final String invalidJson = "{ this is broken }"; |
| 109 | + |
| 110 | + final HttpResponse.BodySubscriber<SignResponse> subscriber = handler.apply(info); |
| 111 | + |
| 112 | + Assertions.assertThrows(BankIdApiUnexpectedResponseException.class, () -> complete(feed(subscriber, invalidJson))); |
| 113 | + } |
| 114 | + |
| 115 | + private static HttpResponse.ResponseInfo responseInfo(final int status, final String contentType) { |
| 116 | + final Map<String, List<String>> headerMap = |
| 117 | + contentType == null |
| 118 | + ? Map.of() |
| 119 | + : Map.of("Content-Type", List.of(contentType)); |
| 120 | + |
| 121 | + final HttpHeaders headers = HttpHeaders.of(headerMap, (k, v) -> true); |
| 122 | + |
| 123 | + return new HttpResponse.ResponseInfo() { |
| 124 | + @Override |
| 125 | + public int statusCode() { |
| 126 | + return status; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public HttpHeaders headers() { |
| 131 | + return headers; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public HttpClient.Version version() { |
| 136 | + return HttpClient.Version.HTTP_1_1; |
| 137 | + } |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + private static <T> T complete(final CompletableFuture<T> future) { |
| 142 | + try { |
| 143 | + return future.get(2, TimeUnit.SECONDS); |
| 144 | + } catch (final TimeoutException e) { |
| 145 | + fail("Timed out waiting for body subscriber to complete"); |
| 146 | + return null; // unreachable |
| 147 | + } catch (final ExecutionException e) { |
| 148 | + if (e.getCause() instanceof RuntimeException re) { |
| 149 | + throw re; |
| 150 | + } |
| 151 | + throw new CompletionException(e.getCause()); |
| 152 | + } catch (final InterruptedException e) { |
| 153 | + Thread.currentThread().interrupt(); |
| 154 | + throw new RuntimeException(e); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static <T> CompletableFuture<T> feed(final HttpResponse.BodySubscriber<T> subscriber, final String body) { |
| 159 | + subscriber.onSubscribe(new NoopSubscription()); |
| 160 | + subscriber.onNext(List.of(ByteBuffer.wrap(body.getBytes(StandardCharsets.UTF_8)))); |
| 161 | + subscriber.onComplete(); |
| 162 | + return subscriber.getBody().toCompletableFuture(); |
| 163 | + } |
| 164 | + |
| 165 | + private static class NoopSubscription implements java.util.concurrent.Flow.Subscription { |
| 166 | + @Override |
| 167 | + public void request(final long n) { |
| 168 | + // no-op |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void cancel() { |
| 173 | + // no-op |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments