ApacheHttpClientTagAdapter extends HttpTagAndSpanNamingAdapter<HttpRequest, HttpResponse> { @Override public @Nullable String getHeaderSingleValue(@Nullable HttpRequest request, @NotNull String headerKey) { if (request == null) { return null; } Header matchingHeader = request.getFirstHeader(headerKey); if (matchingHeader == null) { return null; } return matchingHeader.getValue(); }  @SuppressWarnings("unchecked") static ApacheHttpClientTagAdapter getDefaultInstance(); @Override @Nullable String getRequestPath(@Nullable HttpRequest request); @Override @Nullable Integer getResponseHttpStatus(@Nullable HttpResponse response); @Override @Nullable String getRequestHttpMethod(@Nullable HttpRequest request); @Override @Nullable String getRequestUriPathTemplate(@Nullable HttpRequest request, @Nullable HttpResponse response); @Override @Nullable String getRequestUrl(@Nullable HttpRequest request); @Override @Nullable String getHeaderSingleValue(@Nullable HttpRequest request, @NotNull String headerKey); @Override @Nullable List<String> getHeaderMultipleValue(@Nullable HttpRequest request, @NotNull String headerKey); @Override @Nullable String getSpanHandlerTagValue(@Nullable HttpRequest request, @Nullable HttpResponse response);  }
@Test public void getHeaderSingleValue_works_as_expected_for_header_that_exists() { String headerKey = "headerKey-" + UUID.randomUUID().toString(); String expectedHeaderValue = "headerValue-" + UUID.randomUUID().toString(); Header matchingHeaderMock = mock(Header.class); doReturn(matchingHeaderMock).when(requestMock).getFirstHeader(headerKey); doReturn(expectedHeaderValue).when(matchingHeaderMock).getValue(); String result = implSpy.getHeaderSingleValue(requestMock, headerKey); assertThat(result).isEqualTo(expectedHeaderValue); verify(requestMock).getFirstHeader(headerKey); verify(matchingHeaderMock).getValue(); }
@Test public void getHeaderSingleValue_returns_null_if_no_matching_header_found() { String headerKey = "headerKey-" + UUID.randomUUID().toString(); doReturn(null).when(requestMock).getFirstHeader(anyString()); String result = implSpy.getHeaderSingleValue(requestMock, headerKey); assertThat(result).isNull(); verify(requestMock).getFirstHeader(headerKey); }
@Test public void getHeaderSingleValue_returns_null_if_passed_null_request() { String result = implSpy.getHeaderSingleValue(null, "foo"); assertThat(result).isNull(); }