Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Split’s handler response headers. #459

Merged
merged 9 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ public SegmentChange fetch(String segmentName, long since, FetchOptions options)

response = _client.execute(request);

options.handleResponseHeaders(Arrays.stream(response.getHeaders())
.collect(Collectors.toMap(Header::getName, Header::getValue)));

int statusCode = response.getCode();

if (_log.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
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.net.URIBuilder;
import org.slf4j.Logger;
Expand All @@ -23,8 +22,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;

import static com.google.common.base.Preconditions.checkNotNull;

Expand Down Expand Up @@ -92,8 +89,6 @@ public SplitChange fetch(long since, FetchOptions options) {
}

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

int statusCode = response.getCode();

Expand Down
24 changes: 2 additions & 22 deletions client/src/main/java/io/split/engine/common/FetchOptions.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.split.engine.common;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

public class FetchOptions {

Expand All @@ -16,7 +14,6 @@ public Builder(FetchOptions opts) {
_targetCN = opts._targetCN;
_cacheControlHeaders = opts._cacheControlHeaders;
_fastlyDebugHeader = opts._fastlyDebugHeader;
_responseHeadersCallback = opts._responseHeadersCallback;
_flagSetsFilter = opts._flagSetsFilter;
}

Expand All @@ -30,11 +27,6 @@ public Builder fastlyDebugHeader(boolean on) {
return this;
}

public Builder responseHeadersCallback(Function<Map<String, String>, Void> callback) {
_responseHeadersCallback = callback;
return this;
}

public Builder targetChangeNumber(long targetCN) {
_targetCN = targetCN;
return this;
Expand All @@ -46,13 +38,12 @@ public Builder flagSetsFilter(String flagSetsFilter) {
}

public FetchOptions build() {
return new FetchOptions(_cacheControlHeaders, _targetCN, _responseHeadersCallback, _fastlyDebugHeader, _flagSetsFilter);
return new FetchOptions(_cacheControlHeaders, _targetCN, _fastlyDebugHeader, _flagSetsFilter);
}

private long _targetCN = DEFAULT_TARGET_CHANGENUMBER;
private boolean _cacheControlHeaders = false;
private boolean _fastlyDebugHeader = false;
private Function<Map<String, String>, Void> _responseHeadersCallback = null;
private String _flagSetsFilter = "";
}

Expand All @@ -72,21 +63,12 @@ public String flagSetsFilter() {
return _flagSetsFilter;
}

public void handleResponseHeaders(Map<String, String> headers) {
if (Objects.isNull(_responseHeadersCallback) || Objects.isNull(headers)) {
return;
}
_responseHeadersCallback.apply(headers);
}

private FetchOptions(boolean cacheControlHeaders,
long targetCN,
Function<Map<String, String>, Void> responseHeadersCallback,
boolean fastlyDebugHeader,
String flagSetsFilter) {
_cacheControlHeaders = cacheControlHeaders;
_targetCN = targetCN;
_responseHeadersCallback = responseHeadersCallback;
_fastlyDebugHeader = fastlyDebugHeader;
_flagSetsFilter = flagSetsFilter;
}
Expand All @@ -101,20 +83,18 @@ public boolean equals(Object obj) {

return Objects.equals(_cacheControlHeaders, other._cacheControlHeaders)
&& Objects.equals(_fastlyDebugHeader, other._fastlyDebugHeader)
&& Objects.equals(_responseHeadersCallback, other._responseHeadersCallback)
&& Objects.equals(_targetCN, other._targetCN)
&& Objects.equals(_flagSetsFilter, other._flagSetsFilter);
}

@Override
public int hashCode() {
return com.google.common.base.Objects.hashCode(_cacheControlHeaders, _fastlyDebugHeader, _responseHeadersCallback,
return com.google.common.base.Objects.hashCode(_cacheControlHeaders, _fastlyDebugHeader,
_targetCN, _flagSetsFilter);
}

private final boolean _cacheControlHeaders;
private final boolean _fastlyDebugHeader;
private final long _targetCN;
private final Function<Map<String, String>, Void> _responseHeadersCallback;
private final String _flagSetsFilter;
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public void refreshSplits(Long targetChangeNumber) {
FetchOptions opts = new FetchOptions.Builder()
.cacheControlHeaders(true)
.fastlyDebugHeader(_cdnResponseHeadersLogging)
.responseHeadersCallback(_cdnResponseHeadersLogging ? captor::handle : null)
.flagSetsFilter(_sets)
.build();

Expand Down Expand Up @@ -239,7 +238,6 @@ public void refreshSegment(String segmentName, Long targetChangeNumber) {
FetchOptions opts = new FetchOptions.Builder()
.cacheControlHeaders(true)
.fastlyDebugHeader(_cdnResponseHeadersLogging)
.responseHeadersCallback(_cdnResponseHeadersLogging ? captor::handle : null)
.build();

SyncResult regularResult = attemptSegmentSync(segmentName, targetChangeNumber, opts,
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

Expand All @@ -12,40 +11,16 @@ public class FetcherOptionsTest {

@Test
public void optionsPropagatedOk() {
final boolean[] called = {false};
Function<Map<String, String>, Void> func = new Function<Map<String, String>, Void>() {
@Override
public Void apply(Map<String, String> unused) {
called[0] = true;
return null;
}
};

FetchOptions options = new FetchOptions.Builder()
.cacheControlHeaders(true)
.fastlyDebugHeader(true)
.responseHeadersCallback(func)
.targetChangeNumber(123)
.flagSetsFilter("set1,set2")
.build();

assertEquals(options.cacheControlHeadersEnabled(), true);
assertEquals(options.fastlyDebugHeaderEnabled(), true);
assertEquals(options.targetCN(), 123);
options.handleResponseHeaders(new HashMap<>());
assertEquals(called[0], true);
assertEquals("set1,set2", options.flagSetsFilter());
}

@Test
public void nullHandlerDoesNotExplode() {

FetchOptions options = new FetchOptions.Builder()
.cacheControlHeaders(true)
.fastlyDebugHeader(true)
.responseHeadersCallback(null)
.build();

options.handleResponseHeaders(new HashMap<>());
}
}