Skip to content

Commit

Permalink
Migrate OData header deduplication (#114)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Schneider <[email protected]>
  • Loading branch information
cschubertcs and Johannes-Schneider authored Nov 3, 2023
1 parent 3e607d8 commit 9d13f9d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ public void setHeader( @Nonnull final String key, @Nullable final String value )
headers.put(key, values);
}

/**
* Replace a header with multiple values in the OData HTTP request.
*
* @param key
* The header name.
* @param values
* The header values.
* @since 4.27.0
*/
public void setHeader( @Nonnull final String key, @Nonnull final Collection<String> values )
{
headers.put(key, new ArrayList<>(values));
}

/**
* Add a header to the OData HTTP request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ public Try<ODataRequestResultGeneric> tryGetNextPage()
request.getProtocol());

// populate headers
request.getHeaders().forEach(( k, values ) -> values.forEach(v -> nextReadRequest.addHeader(k, v)));
request.getHeaders().forEach(nextReadRequest::setHeader);

// execute request
return Try.of(() -> nextReadRequest.execute(httpClient));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.apache.http.HttpVersion.HTTP_1_1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
Expand All @@ -15,10 +16,15 @@
import static org.mockito.Mockito.when;

import java.net.SocketException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -132,4 +138,32 @@ public void getHeaderValuesShouldHandleKeyInsensitivity()
assertThat(result.getHeaderValues("someOtherKey")).containsExactly("someOtherValue");
assertThat(result.getHeaderValues("SOMEotherKEY")).containsExactly("someOtherValue");
}

@Test
@SneakyThrows
public void ensureNoRedundantHeadersForPaginatedRequests()
{
final ODataRequestGeneric oDataRequest =
new ODataRequestRead("generic/service/path", "entity(123)", null, ODataProtocol.V4);

final BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, 200, "OK");
final String json = "{\"value\":[],\"@odata.nextLink\": \"Foo?$count=true&$select=BarID&$skiptoken='ABCD'\"}";
httpResponse.setEntity(new StringEntity(json));

final HttpClient httpClient = mock(HttpClient.class);
when(httpClient.execute(any())).thenReturn(httpResponse);

final ODataRequestResultGeneric testResult =
new ODataRequestResultGeneric(oDataRequest, httpResponse, httpClient);

ODataRequestResultGeneric nextResult = testResult.tryGetNextPage().get();
nextResult = nextResult.tryGetNextPage().get();
nextResult = nextResult.tryGetNextPage().get();
nextResult = nextResult.tryGetNextPage().get();
nextResult = nextResult.tryGetNextPage().get();
nextResult = nextResult.tryGetNextPage().get();

final Map<String, Collection<String>> lastRequestHeaders = nextResult.getODataRequest().getHeaders();
assertThat(lastRequestHeaders).containsExactly(entry("Accept", Collections.singletonList("application/json")));
}
}

0 comments on commit 9d13f9d

Please sign in to comment.