ApacheHttpClientTagAdapter extends HttpTagAndSpanNamingAdapter<HttpRequest, HttpResponse> { @Override public @Nullable List<String> getHeaderMultipleValue(@Nullable HttpRequest request, @NotNull String headerKey) { if (request == null) { return null; } Header[] matchingHeaders = request.getHeaders(headerKey); if (matchingHeaders == null || matchingHeaders.length == 0) { return null; } List<String> returnList = new ArrayList<>(matchingHeaders.length); for (Header header : matchingHeaders) { returnList.add(header.getValue()); } return returnList; }  @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 getHeaderMultipleValue_works_as_expected_for_headers_that_exist() { String headerKey = "headerKey-" + UUID.randomUUID().toString(); List<String> expectedHeaderValues = Arrays.asList( "headerValue1-" + UUID.randomUUID().toString(), "headerValue1-" + UUID.randomUUID().toString() ); Header[] matchingHeaderMocksArray = expectedHeaderValues .stream() .map(v -> { Header headerMock = mock(Header.class); doReturn(v).when(headerMock).getValue(); return headerMock; }) .toArray(Header[]::new); doReturn(matchingHeaderMocksArray).when(requestMock).getHeaders(headerKey); List<String> result = implSpy.getHeaderMultipleValue(requestMock, headerKey); assertThat(result).isEqualTo(expectedHeaderValues); verify(requestMock).getHeaders(headerKey); for (Header headerMock : matchingHeaderMocksArray) { verify(headerMock).getValue(); } }
@DataProvider(value = { "true", "false" }) @Test public void getHeaderMultipleValue_returns_null_when_no_matching_headers_found(boolean nullMatchingHeaders) { String headerKey = "headerKey-" + UUID.randomUUID().toString(); Header[] nullOrEmptyMatchingHeadersArray = (nullMatchingHeaders) ? null : new Header[0]; doReturn(nullOrEmptyMatchingHeadersArray).when(requestMock).getHeaders(headerKey); List<String> result = implSpy.getHeaderMultipleValue(requestMock, headerKey); assertThat(result).isNull(); verify(requestMock).getHeaders(headerKey); }
@Test public void getHeaderMultipleValue_returns_null_when_passed_null_request() { assertThat(implSpy.getHeaderMultipleValue(null, "foo")).isNull(); }