/** * Constructor that lets you choose whether downstream calls will be surrounded with a subspan. The default * {@link HttpTagAndSpanNamingStrategy} and {@link HttpTagAndSpanNamingAdapter} will be used * ({@link ZipkinHttpTagStrategy} and {@link SpringHttpClientTagAdapter}). * * @param surroundCallsWithSubspan pass in true to have downstream calls surrounded with a new span, false to only * propagate the current span's info downstream (no subspan). */ public WingtipsClientHttpRequestInterceptor(boolean surroundCallsWithSubspan) { this( surroundCallsWithSubspan, ZipkinHttpTagStrategy.<HttpRequest, ClientHttpResponse>getDefaultInstance(), SpringHttpClientTagAdapter.getDefaultInstance() ); }
@Before public void beforeMethod() { implSpy = spy(new SpringHttpClientTagAdapter()); requestMock = mock(HttpRequest.class); responseMock = mock(ClientHttpResponse.class); }
@Test public void getRequestUrl_returns_request_URI_toString() { // given URI expectedFullUri = URI.create("/foo/bar/" + UUID.randomUUID().toString() + "?stuff=things"); doReturn(expectedFullUri).when(requestMock).getURI(); // when String result = implSpy.getRequestUrl(requestMock); // then assertThat(result).isEqualTo(expectedFullUri.toString()); }
@Test public void getResponseHttpStatus_returns_null_when_passed_null() { // expect assertThat(implSpy.getResponseHttpStatus(null)).isNull(); }
@Test public void getHeaderSingleValue_returns_result_of_calling_request_headers_getFirst() { // given String expectedHeaderValue = UUID.randomUUID().toString(); String headerKey = UUID.randomUUID().toString(); HttpHeaders headersMock = mock(HttpHeaders.class); doReturn(headersMock).when(requestMock).getHeaders(); doReturn(expectedHeaderValue).when(headersMock).getFirst(headerKey); // when String result = implSpy.getHeaderSingleValue(requestMock, headerKey); // then assertThat(result).isEqualTo(expectedHeaderValue); verify(headersMock).getFirst(headerKey); }
@Test public void getRequestHttpMethod_returns_null_when_passed_null() { // expect assertThat(implSpy.getRequestHttpMethod(null)).isNull(); }
@Test public void getHeaderMultipleValue_returns_null_when_passed_null_request() { // expect assertThat(implSpy.getHeaderMultipleValue(null, "foo")).isNull(); }
@Test public void getRequestPath_returns_request_URI_path() { // given String expectedPath = "/foo/bar/" + UUID.randomUUID().toString(); URI fullUri = URI.create(expectedPath + "?stuff=things"); doReturn(fullUri).when(requestMock).getURI(); assertThat(fullUri.toString()).contains("?"); assertThat(expectedPath).doesNotContain("?"); assertThat(fullUri.getPath()).isEqualTo(expectedPath); // when String result = implSpy.getRequestPath(requestMock); // then assertThat(result).isEqualTo(expectedPath); }
@Test public void getRequestUriPathTemplate_returns_null() { // expect assertThat(implSpy.getRequestUriPathTemplate(requestMock, responseMock)).isNull(); }
@Test public void getResponseHttpStatus_returns_null_when_response_getRawStatusCode_throws_IOException( ) throws IOException { // given doThrow(new IOException("intentional exception")).when(responseMock).getRawStatusCode(); // when Integer result = implSpy.getResponseHttpStatus(responseMock); // then assertThat(result).isNull(); verify(responseMock).getRawStatusCode(); }
@Test public void getHeaderSingleValue_returns_null_when_passed_null_request() { // expect assertThat(implSpy.getHeaderSingleValue(null, "foo")).isNull(); }
@DataProvider(value = { "GET | GET", "HEAD | HEAD", "POST | POST", "PUT | PUT", "PATCH | PATCH", "DELETE | DELETE", "OPTIONS | OPTIONS", "TRACE | TRACE", "null | UNKNOWN_HTTP_METHOD" }, splitBy = "\\|") @Test public void getRequestHttpMethod_works_as_expected(HttpMethod httpMethod, String expectedResult) { // given doReturn(httpMethod).when(requestMock).getMethod(); // when String result = implSpy.getRequestHttpMethod(requestMock); // then assertThat(result).isEqualTo(expectedResult); }
@Test public void getHeaderMultipleValue_returns_result_of_calling_request_headers_getValuesAsList() { // given List<String> expectedHeaderValues = Arrays.asList(UUID.randomUUID().toString(), UUID.randomUUID().toString()); String headerKey = UUID.randomUUID().toString(); HttpHeaders headersMock = mock(HttpHeaders.class); doReturn(headersMock).when(requestMock).getHeaders(); doReturn(expectedHeaderValues).when(headersMock).getValuesAsList(headerKey); // when List<String> result = implSpy.getHeaderMultipleValue(requestMock, headerKey); // then assertThat(result).isEqualTo(expectedHeaderValues); verify(headersMock).getValuesAsList(headerKey); }
@Test public void getRequestPath_returns_null_when_passed_null() { // expect assertThat(implSpy.getRequestPath(null)).isNull(); }
/** * Constructor that lets you choose whether downstream calls will be surrounded with a subspan. The default * {@link HttpTagAndSpanNamingStrategy} and {@link HttpTagAndSpanNamingAdapter} will be used * ({@link ZipkinHttpTagStrategy} and {@link SpringHttpClientTagAdapter}). * * @param surroundCallsWithSubspan pass in true to have downstream calls surrounded with a new span, false to only * propagate the current span's info downstream (no subspan). */ public WingtipsAsyncClientHttpRequestInterceptor(boolean surroundCallsWithSubspan) { this( surroundCallsWithSubspan, ZipkinHttpTagStrategy.<HttpRequest, ClientHttpResponse>getDefaultInstance(), SpringHttpClientTagAdapter.getDefaultInstance() ); }
@DataProvider(value = { "200", "300", "400", "500", "999" }) @Test public void getResponseHttpStatus_returns_value_from_response_getRawStatusCode( int responseMethodValue ) throws IOException { // given doReturn(responseMethodValue).when(responseMock).getRawStatusCode(); // when Integer result = implSpy.getResponseHttpStatus(responseMock); // then assertThat(result).isEqualTo(responseMethodValue); verify(responseMock).getRawStatusCode(); }
@Test public void getRequestUrl_returns_null_when_passed_null() { // expect assertThat(implSpy.getRequestUrl(null)).isNull(); }
/** * Constructor that lets you choose whether downstream calls will be surrounded with a subspan. The default * {@link HttpTagAndSpanNamingStrategy} and {@link HttpTagAndSpanNamingAdapter} will be used * ({@link ZipkinHttpTagStrategy} and {@link SpringHttpClientTagAdapter}). * * @param surroundCallsWithSubspan pass in true to have downstream calls surrounded with a new span, false to only * propagate the current span's info downstream (no subspan). */ public WingtipsClientHttpRequestInterceptor(boolean surroundCallsWithSubspan) { this( surroundCallsWithSubspan, ZipkinHttpTagStrategy.<HttpRequest, ClientHttpResponse>getDefaultInstance(), SpringHttpClientTagAdapter.getDefaultInstance() ); }
/** * Constructor that lets you choose whether downstream calls will be surrounded with a subspan. The default * {@link HttpTagAndSpanNamingStrategy} and {@link HttpTagAndSpanNamingAdapter} will be used * ({@link ZipkinHttpTagStrategy} and {@link SpringHttpClientTagAdapter}). * * @param surroundCallsWithSubspan pass in true to have downstream calls surrounded with a new span, false to only * propagate the current span's info downstream (no subspan). */ public WingtipsAsyncClientHttpRequestInterceptor(boolean surroundCallsWithSubspan) { this( surroundCallsWithSubspan, ZipkinHttpTagStrategy.<HttpRequest, ClientHttpResponse>getDefaultInstance(), SpringHttpClientTagAdapter.getDefaultInstance() ); }
@Test public void getDefaultInstance_returns_DEFAULT_INSTANCE() { // expect assertThat(SpringHttpClientTagAdapter.getDefaultInstance()) .isSameAs(SpringHttpClientTagAdapter.DEFAULT_INSTANCE); }