@Override public ClientHttpResponse intercept( HttpRequest request, byte[] body, ClientHttpRequestExecution execution ) throws IOException { // We need to wrap the request with HttpRequestWrapperWithModifiableHeaders so that tracing info can be // propagated on the headers. HttpRequestWrapperWithModifiableHeaders wrapperRequest = new HttpRequestWrapperWithModifiableHeaders(request); if (surroundCallsWithSubspan) { return createNewSpanAndExecuteRequest(wrapperRequest, body, execution); } return propagateTracingHeadersAndExecuteRequest(wrapperRequest, body, execution); }
@Override @SuppressWarnings("deprecation") public ListenableFuture<ClientHttpResponse> intercept( HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution ) throws IOException { // We need to wrap the request with HttpRequestWrapperWithModifiableHeaders so that tracing info can be // propagated on the headers. HttpRequestWrapperWithModifiableHeaders wrapperRequest = new HttpRequestWrapperWithModifiableHeaders(request); if (surroundCallsWithSubspan) { return createAsyncSubSpanAndExecute(wrapperRequest, body, execution); } return propagateTracingHeadersAndExecute(wrapperRequest, body, execution); }
@Override public ClientHttpResponse intercept( HttpRequest request, byte[] body, ClientHttpRequestExecution execution ) throws IOException { // We need to wrap the request with HttpRequestWrapperWithModifiableHeaders so that tracing info can be // propagated on the headers. HttpRequestWrapperWithModifiableHeaders wrapperRequest = new HttpRequestWrapperWithModifiableHeaders(request); if (surroundCallsWithSubspan) { return createNewSpanAndExecuteRequest(wrapperRequest, body, execution); } return propagateTracingHeadersAndExecuteRequest(wrapperRequest, body, execution); }
@Override @SuppressWarnings("deprecation") public ListenableFuture<ClientHttpResponse> intercept( HttpRequest request, byte[] body, AsyncClientHttpRequestExecution execution ) throws IOException { // We need to wrap the request with HttpRequestWrapperWithModifiableHeaders so that tracing info can be // propagated on the headers. HttpRequestWrapperWithModifiableHeaders wrapperRequest = new HttpRequestWrapperWithModifiableHeaders(request); if (surroundCallsWithSubspan) { return createAsyncSubSpanAndExecute(wrapperRequest, body, execution); } return propagateTracingHeadersAndExecute(wrapperRequest, body, execution); }
@Test public void verify_non_header_wrapper_methods_pass_through_to_original_request() { // given doReturn(new HttpHeaders()).when(requestMock).getHeaders(); HttpRequestWrapperWithModifiableHeaders wrapper = new HttpRequestWrapperWithModifiableHeaders(requestMock); // when HttpRequest wrappedRequest = wrapper.getRequest(); URI wrapperUri = wrapper.getURI(); HttpMethod wrapperMethod = wrapper.getMethod(); // then assertThat(wrappedRequest).isSameAs(requestMock); assertThat(wrapperUri).isSameAs(uri); assertThat(wrapperMethod).isSameAs(wrapperMethod); verify(requestMock).getURI(); verify(requestMock).getMethod(); }
@Test public void constructor_sets_modifiableHeaders_to_a_mutable_copy_of_given_request_headers() { // given HttpHeaders immutableHeaders = generateImmutableHeaders( MapBuilder.builder("foo", singletonList(UUID.randomUUID().toString())) .put("bar", Arrays.asList(UUID.randomUUID().toString(), UUID.randomUUID().toString())) .build() ); doReturn(immutableHeaders).when(requestMock).getHeaders(); verifyImmutableHeaders(immutableHeaders); // when HttpRequestWrapperWithModifiableHeaders wrapper = new HttpRequestWrapperWithModifiableHeaders(requestMock); verify(requestMock).getHeaders(); // The constructor should have called requestMock.getHeaders() // then HttpHeaders wrapperHeaders = wrapper.getHeaders(); assertThat(wrapperHeaders).isSameAs(wrapper.modifiableHeaders); // The call to wrapper.getHeaders() should not have called requestMock.getHeaders() verifyNoMoreInteractions(requestMock); // Instead we should get back some headers that are equal to requestMock's headers, but mutable. assertThat(wrapperHeaders).isEqualTo(immutableHeaders); verifyMutableHeaders(wrapperHeaders); }
@Test public void createAsyncSubSpanAndExecute_trigger_null_subspanFinisher_in_catch_block_branch_for_code_coverage() { // given Tracer.getInstance().startRequestWithRootSpan("someRootSpan"); TracingState tracingStateBeforeInterceptorCall = TracingState.getCurrentThreadTracingState(); WingtipsAsyncClientHttpRequestInterceptor interceptorSpy = spy(new WingtipsAsyncClientHttpRequestInterceptor( true, tagAndNamingStrategy, tagAndNamingAdapterMock )); RuntimeException explodingSubspanNameMethodEx = new RuntimeException("Intentional exception thrown by getSubspanSpanName()"); doThrow(explodingSubspanNameMethodEx).when(interceptorSpy).getSubspanSpanName( any(HttpRequest.class), any(HttpTagAndSpanNamingStrategy.class), any(HttpTagAndSpanNamingAdapter.class) ); HttpRequestWrapperWithModifiableHeaders wrapperRequest = new HttpRequestWrapperWithModifiableHeaders(requestMock); byte[] body = new byte[]{42}; // when Throwable ex = catchThrowable( () -> interceptorSpy.createAsyncSubSpanAndExecute(wrapperRequest, body, executionMock) ); // then assertThat(ex).isSameAs(explodingSubspanNameMethodEx); verify(interceptorSpy).getSubspanSpanName(wrapperRequest, tagAndNamingStrategy, tagAndNamingAdapterMock); // TracingState should have been reset even though an exception occurred in some unexpected place. assertThat(normalizeTracingState(TracingState.getCurrentThreadTracingState())) .isEqualTo(normalizeTracingState(tracingStateBeforeInterceptorCall)); }