Skip to content

Commit

Permalink
fixed headers crash
Browse files Browse the repository at this point in the history
  • Loading branch information
sanzmauro committed Nov 2, 2023
1 parent 541db29 commit 29a2014
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
21 changes: 19 additions & 2 deletions client/src/main/java/io/split/client/HttpSplitChangeFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.net.URIBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,6 +27,8 @@
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -93,8 +97,7 @@ public SplitChange fetch(long since, FetchOptions options) {
}

response = _client.execute(request);
options.handleResponseHeaders(Arrays.stream(response.getHeaders())
.collect(Collectors.toMap(Header::getName, Header::getValue)));
options.handleResponseHeaders(readResponseHeaders(response.getHeaders()));

int statusCode = response.getCode();

Expand Down Expand Up @@ -127,4 +130,18 @@ public SplitChange fetch(long since, FetchOptions options) {
URI getTarget() {
return _target;
}

private Map<String, String> readResponseHeaders(Header[] headers) {
Map<String, String> toReturn = new HashMap<>();
for (Header header : headers) {
if (toReturn.containsKey(header.getName())) {
toReturn.put(header.getName(), toReturn.get(header.getName()) + "," + header.getValue());
continue;
}

toReturn.put(header.getName(), header.getValue());
}

return toReturn;
}
}
7 changes: 6 additions & 1 deletion client/src/test/java/io/split/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.message.BasicHeader;
import org.mockito.Mockito;

import java.io.IOException;
Expand All @@ -19,7 +21,10 @@ public static CloseableHttpClient mockHttpClient(String jsonName, int httpStatus
ClassicHttpResponse httpResponseMock = Mockito.mock(ClassicHttpResponse.class);
Mockito.when(httpResponseMock.getEntity()).thenReturn(entityMock);
Mockito.when(httpResponseMock.getCode()).thenReturn(httpStatus);
Mockito.when(httpResponseMock.getHeaders()).thenReturn(new Header[0]);
Header[] headers = new Header[2];
headers[0] = new BasicHeader(HttpHeaders.VIA, "HTTP/1.1 m_proxy_rio1");
headers[1] = new BasicHeader(HttpHeaders.VIA, "HTTP/1.1 s_proxy_rio1");
Mockito.when(httpResponseMock.getHeaders()).thenReturn(headers);
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
Mockito.when(httpClientMock.execute(Mockito.anyObject())).thenReturn(classicResponseToCloseableMock(httpResponseMock));

Expand Down

0 comments on commit 29a2014

Please sign in to comment.