@Test public void getFormattedElement() { RoutingContext mockContext = Mockito.mock(RoutingContext.class); AccessLogParam param = new AccessLogParam().setRoutingContext(mockContext); HttpServerRequest mockRequest = Mockito.mock(HttpServerRequest.class); VertxHttpHeaders headers = new VertxHttpHeaders(); String testValue = "testValue"; headers.add(HEADER_IDENTIFIER, testValue); Mockito.when(mockContext.request()).thenReturn(mockRequest); Mockito.when(mockRequest.headers()).thenReturn(headers); String result = ELEMENT.getFormattedElement(param); assertEquals(testValue, result); assertEquals(ELEMENT.getIdentifier(), HEADER_IDENTIFIER); }
@Test public void getFormattedElementIfNotFound() { RoutingContext mockContext = Mockito.mock(RoutingContext.class); AccessLogParam param = new AccessLogParam().setRoutingContext(mockContext); HttpServerRequest mockRequest = Mockito.mock(HttpServerRequest.class); VertxHttpHeaders headers = new VertxHttpHeaders(); String testValue = "testValue"; headers.add("anotherHeader", testValue); Mockito.when(mockContext.request()).thenReturn(mockRequest); Mockito.when(mockRequest.headers()).thenReturn(headers); String result = ELEMENT.getFormattedElement(param); assertEquals("-", result); }
@Test public void getFormattedElement() { AccessLogParam param = new AccessLogParam(); RoutingContext mockContext = Mockito.mock(RoutingContext.class); HttpServerResponse mockResponse = Mockito.mock(HttpServerResponse.class); VertxHttpHeaders headers = new VertxHttpHeaders(); String headerValue = "headerValue"; param.setRoutingContext(mockContext); headers.add(IDENTIFIER, headerValue); Mockito.when(mockContext.response()).thenReturn(mockResponse); Mockito.when(mockResponse.headers()).thenReturn(headers); String result = ELEMENT.getFormattedElement(param); assertEquals(headerValue, result); assertEquals(ELEMENT.getIdentifier(), IDENTIFIER); }
@Test public void getFormattedElementOnNotFound() { AccessLogParam param = new AccessLogParam(); RoutingContext mockContext = Mockito.mock(RoutingContext.class); HttpServerResponse mockResponse = Mockito.mock(HttpServerResponse.class); VertxHttpHeaders headers = new VertxHttpHeaders(); String headerValue = "headerValue"; param.setRoutingContext(mockContext); headers.add("anotherHeader", headerValue); Mockito.when(mockContext.response()).thenReturn(mockResponse); Mockito.when(mockResponse.headers()).thenReturn(headers); String result = ELEMENT.getFormattedElement(param); assertEquals("-", result); }
@Test public void test400(final TestContext testContext) throws Exception { final HttpServerRequest serverRequest = mock(HttpServerRequest.class); when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/hello/400"); when(serverRequest.path()).thenReturn("/api/hello/400"); when(serverRequest.uri()).thenReturn("/api/hello/400"); when(serverRequest.isEnded()).thenReturn(true); when(serverRequest.method()).thenReturn(HttpMethod.GET); final HttpServerResponse response = mock(HttpServerResponse.class); when(response.putHeader(anyString(), anyString())).thenReturn(response); when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response); when(response.headers()).thenReturn(new VertxHttpHeaders()); final Async async = testContext.async(); when(response.setStatusCode(Matchers.any(Integer.class))).then(invocation -> { try { return response; } finally { async.complete(); } }); when(serverRequest.response()).thenReturn(response); router.accept(serverRequest); async.awaitSuccess(); verify(response, times(1)).setStatusCode(400); }
@Test public void test404(final TestContext testContext) throws Exception { final Router router = Router.router(rule.vertx()); final JaxRsRouter jaxRsRouter = new JaxRsRouter(); final SpringJaxRsHandler handler = new SpringJaxRsHandler(MyApp.class); jaxRsRouter.register(MyApp.class, router, handler, handler); final HttpServerRequest serverRequest = mock(HttpServerRequest.class); when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/nothello"); when(serverRequest.path()).thenReturn("/api/nothello"); when(serverRequest.uri()).thenReturn("/api/nothello"); when(serverRequest.isEnded()).thenReturn(true); when(serverRequest.method()).thenReturn(HttpMethod.GET); final HttpServerResponse response = mock(HttpServerResponse.class); when(response.putHeader(anyString(), anyString())).thenReturn(response); when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response); when(response.headers()).thenReturn(new VertxHttpHeaders()); final Async async = testContext.async(); Mockito.doAnswer(invocation -> { async.complete(); return null; }).when(response).end(anyString()); when(serverRequest.response()).thenReturn(response); router.accept(serverRequest); async.await(); verify(response, times(1)).setStatusCode(404); verify(response, times(1)).end(anyString()); }
@Test public void test404Internal(final TestContext testContext) throws Exception { final Router router = Router.router(rule.vertx()); final JaxRsRouter jaxRsRouter = new JaxRsRouter(); final SpringJaxRsHandler handler = new SpringJaxRsHandler(MyApp.class); jaxRsRouter.register(MyApp.class, router, handler, handler); final HttpServerRequest serverRequest = mock(HttpServerRequest.class); when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/hello/404"); when(serverRequest.path()).thenReturn("/api/hello/404"); when(serverRequest.uri()).thenReturn("/api/hello/404"); when(serverRequest.isEnded()).thenReturn(true); when(serverRequest.method()).thenReturn(HttpMethod.GET); final HttpServerResponse response = mock(HttpServerResponse.class); when(response.putHeader(anyString(), anyString())).thenReturn(response); when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response); when(response.headers()).thenReturn(new VertxHttpHeaders()); final Async async = testContext.async(); when(response.setStatusCode(Matchers.any(Integer.class))).then(invocation -> { try { return response; } finally { async.complete(); } }); when(serverRequest.response()).thenReturn(response); router.accept(serverRequest); async.await(); verify(response, times(1)).setStatusCode(404); }
@Test public void test500(final TestContext testContext) throws Exception { final Router router = Router.router(rule.vertx()); final JaxRsRouter jaxRsRouter = new JaxRsRouter(); final SpringJaxRsHandler handler = new SpringJaxRsHandler(MyApp.class); jaxRsRouter.register(MyApp.class, router, handler, handler); final HttpServerRequest serverRequest = mock(HttpServerRequest.class); when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/hello/cough"); when(serverRequest.path()).thenReturn("/api/hello/cough"); when(serverRequest.uri()).thenReturn("/api/hello/cough"); when(serverRequest.isEnded()).thenReturn(true); when(serverRequest.method()).thenReturn(HttpMethod.GET); final HttpServerResponse response = mock(HttpServerResponse.class); when(response.putHeader(anyString(), anyString())).thenReturn(response); when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response); when(response.headers()).thenReturn(new VertxHttpHeaders()); final Async async = testContext.async(); when(response.write(Matchers.any(Buffer.class))).thenReturn(response); doAnswer(i -> { async.complete(); return null; }).when(response).end(); when(serverRequest.response()).thenReturn(response); router.accept(serverRequest); async.awaitSuccess(); final ArgumentCaptor<Buffer> captor = ArgumentCaptor.forClass(Buffer.class); verify(response, times(1)).setStatusCode(500); verify(response, atLeastOnce()).write(captor.capture()); final String errorMessage = String.join("", captor.getAllValues().stream().map(Buffer::toString).collect(Collectors.toList())); assertTrue(errorMessage.contains("server_error")); }
@Test public void testFailure(final TestContext testContext) throws Exception { final HttpServerRequest serverRequest = mock(HttpServerRequest.class); when(serverRequest.absoluteURI()).thenThrow(new RuntimeException("boom")); when(serverRequest.path()).thenReturn("/api/hello/400"); when(serverRequest.uri()).thenReturn("/api/hello/400"); when(serverRequest.isEnded()).thenReturn(true); when(serverRequest.method()).thenReturn(HttpMethod.GET); final HttpServerResponse response = mock(HttpServerResponse.class); when(response.putHeader(anyString(), anyString())).thenReturn(response); when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response); when(response.headers()).thenReturn(new VertxHttpHeaders()); final Async async = testContext.async(); when(response.setStatusCode(Matchers.any(Integer.class))).then(invocation -> { try { return response; } finally { async.complete(); } }); when(serverRequest.response()).thenReturn(response); router.accept(serverRequest); async.await(); verify(response, times(1)).setStatusCode(500); }
@Test public void testHandler(final TestContext testContext) throws Exception { final Router router = Router.router(rule.vertx()); final JaxRsRouter jaxRsRouter = new JaxRsRouter(); final SpringJaxRsHandler handler = new SpringJaxRsHandler(MyApp.class); jaxRsRouter.register(MyApp.class, router, handler, handler); final HttpServerRequest serverRequest = mock(HttpServerRequest.class); when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/hello"); when(serverRequest.path()).thenReturn("/api/hello"); when(serverRequest.uri()).thenReturn("/api/hello"); when(serverRequest.isEnded()).thenReturn(true); when(serverRequest.method()).thenReturn(HttpMethod.GET); final HttpServerResponse response = mock(HttpServerResponse.class); when(response.putHeader(anyString(), anyString())).thenReturn(response); when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response); when(response.headers()).thenReturn(new VertxHttpHeaders()); final Async async = testContext.async(); when(response.write(Matchers.any(Buffer.class))).then(invocation -> { try { return response; } finally { async.complete(); } }); when(serverRequest.response()).thenReturn(response); router.accept(serverRequest); async.await(); final ArgumentCaptor<Buffer> captor = ArgumentCaptor.forClass(Buffer.class); verify(response, times(1)).setStatusCode(200); verify(response, times(1)).setChunked(true); verify(response, times(1)).write(captor.capture()); assertTrue(captor.getValue().toString().startsWith("Hello")); }