Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -123,7 +123,14 @@ public void onResponse(@Nonnull Call call, @Nonnull Response response) {
return;
}

// Update ETag cache
// The documentation indicates that the body will not be null for a response passed to the
// onResponse callback.
String responseBody = Objects.requireNonNull(response.body()).string();
logger.debug("Received FDv2 polling response");

List<FDv2Event> events = FDv2Event.parseEventsArray(responseBody);

// Only update the ETag cache after the payload has been parsed.
String newEtag = response.header("ETag");
synchronized (etags) {
if (newEtag != null) {
Expand All @@ -133,13 +140,6 @@ public void onResponse(@Nonnull Call call, @Nonnull Response response) {
}
}

// The documentation indicates that the body will not be null for a response passed to the
// onResponse callback.
String responseBody = Objects.requireNonNull(response.body()).string();
logger.debug("Received FDv2 polling response");

List<FDv2Event> events = FDv2Event.parseEventsArray(responseBody);

// Create and return the response
FDv2PayloadResponse pollingResponse = FDv2PayloadResponse.success(events, response.headers(), response.code());
future.complete(pollingResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,33 @@ public void invalidJsonThrowsException() throws Exception {
}
}

@Test
public void invalidJsonDoesNotCacheEtag() throws Exception {
Handler invalidResponse = Handlers.all(
Handlers.header("ETag", "invalid-etag"),
Handlers.bodyJson("{ invalid json }")
);
Handler validResponse = Handlers.bodyJson(VALID_EVENTS_JSON);

try (HttpServer server = HttpServer.start(Handlers.sequential(invalidResponse, validResponse))) {
try (DefaultFDv2Requestor requestor = makeRequestor(server)) {
try {
requestor.Poll(Selector.EMPTY).get(5, TimeUnit.SECONDS);
fail("Expected ExecutionException");
} catch (ExecutionException e) {
assertThat(e.getCause(), notNullValue());
}

assertNull(server.getRecorder().requireRequest().getHeader("If-None-Match"));

FDv2Requestor.FDv2PayloadResponse response =
requestor.Poll(Selector.EMPTY).get(5, TimeUnit.SECONDS);
assertEquals(3, response.getEvents().size());
assertNull(server.getRecorder().requireRequest().getHeader("If-None-Match"));
}
}
}

@Test
public void missingEventsPropertyThrowsException() throws Exception {
Handler resp = Handlers.bodyJson("{}");
Expand Down