Add ning http headers class

This commit is contained in:
Winston Li 2017-08-13 17:09:15 +01:00 committed by Michael Walker
parent 34e558ab63
commit 6c71c2cb97
2 changed files with 61 additions and 2 deletions

View file

@ -0,0 +1,46 @@
package uk.ac.ic.wlgitbridge.io.http.ning;
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
import com.ning.http.client.HttpResponseHeaders;
import java.util.*;
public class NingHttpHeaders extends HttpResponseHeaders {
private final FluentCaseInsensitiveStringsMap map;
private NingHttpHeaders(FluentCaseInsensitiveStringsMap map) {
this.map = map;
}
public static NingHttpHeadersBuilder builder() {
return new NingHttpHeadersBuilder();
}
@Override
public FluentCaseInsensitiveStringsMap getHeaders() {
return map;
}
public static class NingHttpHeadersBuilder {
private final Map<String, Collection<String>> map;
private NingHttpHeadersBuilder() {
map = new HashMap<>();
}
public NingHttpHeadersBuilder addHeader(String key, String... values) {
map.computeIfAbsent(key, __ -> new ArrayList<>())
.addAll(Arrays.asList(values));
return this;
}
public NingHttpHeaders build() {
return new NingHttpHeaders(
new FluentCaseInsensitiveStringsMap(map));
}
}
}

View file

@ -1,13 +1,19 @@
package uk.ac.ic.wlgitbridge.bridge.resource;
import com.ning.http.client.HttpResponseHeaders;
import org.junit.Test;
import uk.ac.ic.wlgitbridge.bridge.db.noop.NoopDbStore;
import uk.ac.ic.wlgitbridge.git.exception.SizeLimitExceededException;
import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpClientFacade;
import uk.ac.ic.wlgitbridge.io.http.ning.NingHttpHeaders;
import uk.ac.ic.wlgitbridge.util.FunctionT;
import java.util.Collections;
import java.util.HashMap;
import java.util.Optional;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class UrlResourceCacheTest {
@ -22,6 +28,13 @@ public class UrlResourceCacheTest {
private final UrlResourceCache cache
= new UrlResourceCache(new NoopDbStore(), http);
private static HttpResponseHeaders withContentLength(long cl) {
return NingHttpHeaders
.builder()
.addHeader("Content-Length", String.valueOf(cl))
.build();
}
@Test
public void getThrowsSizeLimitWhenContentLengthTooBig() throws Exception {
when(http.get(any(), any())).thenAnswer(invoc -> {
@ -35,7 +48,7 @@ public class UrlResourceCacheTest {
cache.get(
PROJ, URL, NEW_PATH,
Collections.emptyMap(), Collections.emptyMap(),
new HashMap<>(), new HashMap<>(),
Optional.of(2L)
);
}