|
| 1 | +package com.retailsvc.http.internal; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | +import static org.mockito.ArgumentMatchers.eq; |
| 6 | +import static org.mockito.ArgumentMatchers.longThat; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import com.sun.net.httpserver.Headers; |
| 12 | +import com.sun.net.httpserver.HttpExchange; |
| 13 | +import java.io.ByteArrayOutputStream; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStream; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +class ClasspathResourceHandlerTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + void getServesBytesVerbatim() throws IOException { |
| 22 | + byte[] expected = readResource("/sample.txt"); |
| 23 | + HttpExchange ex = newExchange("GET"); |
| 24 | + ByteArrayOutputStream body = new ByteArrayOutputStream(); |
| 25 | + when(ex.getResponseBody()).thenReturn(body); |
| 26 | + |
| 27 | + new ClasspathResourceHandler("/sample.txt").handle(ex); |
| 28 | + |
| 29 | + verify(ex).sendResponseHeaders(eq(200), eq((long) expected.length)); |
| 30 | + assertThat(body.toByteArray()).isEqualTo(expected); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void headSendsContentLengthHeaderWithoutBody() throws IOException { |
| 35 | + byte[] expected = readResource("/sample.txt"); |
| 36 | + HttpExchange ex = newExchange("HEAD"); |
| 37 | + Headers responseHeaders = new Headers(); |
| 38 | + when(ex.getResponseHeaders()).thenReturn(responseHeaders); |
| 39 | + |
| 40 | + new ClasspathResourceHandler("/sample.txt").handle(ex); |
| 41 | + |
| 42 | + verify(ex).sendResponseHeaders(eq(200), eq(-1L)); |
| 43 | + assertThat(responseHeaders.getFirst("Content-Length")) |
| 44 | + .isEqualTo(String.valueOf(expected.length)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void infersApplicationJsonForJsonExtension() throws IOException { |
| 49 | + assertThat(contentTypeFor("/openapi.json")).isEqualTo("application/json"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void infersApplicationYamlForYamlExtension() throws IOException { |
| 54 | + assertThat(contentTypeFor("/openapi.yaml")).isEqualTo("application/yaml"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void infersTextPlainForTxtExtension() throws IOException { |
| 59 | + assertThat(contentTypeFor("/sample.txt")).isEqualTo("text/plain; charset=utf-8"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void fallsBackToOctetStreamForUnknownExtension() throws IOException { |
| 64 | + assertThat(contentTypeFor("/sample.bin")).isEqualTo("application/octet-stream"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void missingResourceThrowsIllegalArgumentExceptionWithPathInMessage() { |
| 69 | + assertThatThrownBy(() -> new ClasspathResourceHandler("/does-not-exist.json")) |
| 70 | + .isInstanceOf(IllegalArgumentException.class) |
| 71 | + .hasMessageContaining("/does-not-exist.json"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void resourceIsLoadedEagerlyAtConstruction() { |
| 76 | + // If the resource were loaded lazily, construction would succeed and the handle() |
| 77 | + // call would fail. Construction itself must fail. |
| 78 | + assertThatThrownBy(() -> new ClasspathResourceHandler("/missing.txt")) |
| 79 | + .isInstanceOf(IllegalArgumentException.class); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void contentLengthIsSetForGetRequests() throws IOException { |
| 84 | + HttpExchange ex = newExchange("GET"); |
| 85 | + when(ex.getResponseBody()).thenReturn(new ByteArrayOutputStream()); |
| 86 | + |
| 87 | + new ClasspathResourceHandler("/sample.txt").handle(ex); |
| 88 | + |
| 89 | + verify(ex).sendResponseHeaders(eq(200), longThat(n -> n > 0)); |
| 90 | + } |
| 91 | + |
| 92 | + private static String contentTypeFor(String resource) throws IOException { |
| 93 | + HttpExchange ex = newExchange("GET"); |
| 94 | + Headers headers = new Headers(); |
| 95 | + when(ex.getResponseHeaders()).thenReturn(headers); |
| 96 | + when(ex.getResponseBody()).thenReturn(new ByteArrayOutputStream()); |
| 97 | + new ClasspathResourceHandler(resource).handle(ex); |
| 98 | + return headers.getFirst("Content-Type"); |
| 99 | + } |
| 100 | + |
| 101 | + private static HttpExchange newExchange(String method) { |
| 102 | + HttpExchange ex = mock(HttpExchange.class); |
| 103 | + when(ex.getRequestMethod()).thenReturn(method); |
| 104 | + when(ex.getResponseHeaders()).thenReturn(new Headers()); |
| 105 | + return ex; |
| 106 | + } |
| 107 | + |
| 108 | + private static byte[] readResource(String path) throws IOException { |
| 109 | + try (InputStream in = ClasspathResourceHandlerTest.class.getResourceAsStream(path)) { |
| 110 | + if (in == null) { |
| 111 | + throw new IOException("missing fixture: " + path); |
| 112 | + } |
| 113 | + return in.readAllBytes(); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments