From ade40d7940bfcb20af9cc64a9e8c5476179e8a73 Mon Sep 17 00:00:00 2001 From: Jackson Price <59149601+jprice8@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:27:27 -0600 Subject: [PATCH] test(codeartifact): share npm transport mock helpers (#9019) Extract the client construction and the mocked HttpClient.send call into two private helpers, clientWithTransport and anySend, used by the six npm transport tests. Each case now states its response or exception and its expected send count without repeating the transport matchers. --- .../NpmExternalConnectionClientTest.java | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/test/java/io/floci/aws/services/codeartifact/NpmExternalConnectionClientTest.java b/src/test/java/io/floci/aws/services/codeartifact/NpmExternalConnectionClientTest.java index 60e06f7e34..bf0b7bdfc7 100644 --- a/src/test/java/io/floci/aws/services/codeartifact/NpmExternalConnectionClientTest.java +++ b/src/test/java/io/floci/aws/services/codeartifact/NpmExternalConnectionClientTest.java @@ -80,44 +80,41 @@ class NpmExternalConnectionClientTest { byte[] content = "{\"name\":\"widget\"}".getBytes(StandardCharsets.UTF_8); when(response.statusCode()).thenReturn(200); when(response.body()).thenReturn(new ByteArrayInputStream(content)); - when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))) + when(anySend(transport)) .thenThrow(new IOException("Connection reset")) .thenReturn(response); - NpmExternalConnectionClient retryClient = - new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1")); + NpmExternalConnectionClient retryClient = clientWithTransport(transport); Optional result = retryClient.fetchPackument("fake", null, "widget"); assertTrue(result.isPresent()); assertArrayEquals(content, result.get().content()); - verify(transport, times(2)).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)); + anySend(verify(transport, times(2))); } @Test void fetchPackumentDoesNotRetryARequestTimeout() throws Exception { HttpClient transport = mock(HttpClient.class); - when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))) + when(anySend(transport)) .thenThrow(new HttpTimeoutException("request timed out")); - NpmExternalConnectionClient retryClient = - new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1")); + NpmExternalConnectionClient retryClient = clientWithTransport(transport); assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty()); - verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)); + anySend(verify(transport)); } @Test void fetchPackumentStopsAfterTwoFailedTransportAttempts() throws Exception { HttpClient transport = mock(HttpClient.class); - when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))) + when(anySend(transport)) .thenThrow(new IOException("Connection reset")); - NpmExternalConnectionClient retryClient = - new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1")); + NpmExternalConnectionClient retryClient = clientWithTransport(transport); assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty()); - verify(transport, times(2)).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)); + anySend(verify(transport, times(2))); } @Test @@ -126,13 +123,12 @@ class NpmExternalConnectionClientTest { HttpResponse response = mock(HttpResponse.class); when(response.statusCode()).thenReturn(404); when(response.body()).thenReturn(new ByteArrayInputStream(new byte[0])); - when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(response); - NpmExternalConnectionClient retryClient = - new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1")); + when(anySend(transport)).thenReturn(response); + NpmExternalConnectionClient retryClient = clientWithTransport(transport); assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty()); - verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)); + anySend(verify(transport)); } @Test @@ -143,28 +139,26 @@ class NpmExternalConnectionClientTest { when(response.statusCode()).thenReturn(200); when(response.body()).thenReturn(body); when(body.transferTo(any())).thenThrow(new IOException("Connection reset while reading")); - when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(response); - NpmExternalConnectionClient retryClient = - new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1")); + when(anySend(transport)).thenReturn(response); + NpmExternalConnectionClient retryClient = clientWithTransport(transport); assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty()); - verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)); + anySend(verify(transport)); verify(body).close(); } @Test void fetchPackumentPreservesInterruptionWithoutRetrying() throws Exception { HttpClient transport = mock(HttpClient.class); - when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))) + when(anySend(transport)) .thenThrow(new InterruptedException("cancelled")); - NpmExternalConnectionClient retryClient = - new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1")); + NpmExternalConnectionClient retryClient = clientWithTransport(transport); try { assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty()); assertTrue(Thread.currentThread().isInterrupted()); - verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)); + anySend(verify(transport)); } finally { Thread.interrupted(); } @@ -299,6 +293,14 @@ class NpmExternalConnectionClientTest { assertFalse(result.isPresent()); } + private static NpmExternalConnectionClient clientWithTransport(HttpClient transport) { + return new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1")); + } + + private static HttpResponse anySend(HttpClient transport) throws IOException, InterruptedException { + return transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)); + } + private static void respondEmpty(HttpExchange exchange, int status) throws IOException { exchange.sendResponseHeaders(status, -1); exchange.close();