use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.
the class HttpArtifactCacheTest method testFetchOK.
@Test
public void testFetchOK() throws Exception {
Path output = Paths.get("output/file");
final String data = "test";
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
final List<Response> responseList = Lists.newArrayList();
argsBuilder.setProjectFilesystem(filesystem);
argsBuilder.setFetchClient(withMakeRequest((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER + path).build();
Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
responseList.add(response);
return new OkHttpResponseWrapper(response);
}));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
assertEquals(result.cacheError().orElse(""), CacheResultType.HIT, result.getType());
assertEquals(Optional.of(data), filesystem.readFileIfItExists(output));
assertEquals(result.artifactSizeBytes(), Optional.of(filesystem.getFileSize(output)));
assertTrue("response wasn't fully read!", responseList.get(0).body().source().exhausted());
cache.close();
}
use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.
the class HttpArtifactCacheTest method testFetchNotFound.
@Test
public void testFetchNotFound() throws Exception {
final List<Response> responseList = Lists.newArrayList();
argsBuilder.setFetchClient(withMakeRequest((path, requestBuilder) -> {
Response response = new Response.Builder().code(HttpURLConnection.HTTP_NOT_FOUND).body(ResponseBody.create(MediaType.parse("application/octet-stream"), "extraneous")).protocol(Protocol.HTTP_1_1).request(requestBuilder.url(SERVER + path).build()).build();
responseList.add(response);
return new OkHttpResponseWrapper(response);
}));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
CacheResult result = cache.fetch(new RuleKey("00000000000000000000000000000000"), LazyPath.ofInstance(Paths.get("output/file")));
assertEquals(result.getType(), CacheResultType.MISS);
assertTrue("response wasn't fully read!", responseList.get(0).body().source().exhausted());
cache.close();
}
use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.
the class HttpArtifactCacheTest method testStoreMultipleKeys.
@Test
public void testStoreMultipleKeys() throws Exception {
final RuleKey ruleKey1 = new RuleKey("00000000000000000000000000000000");
final RuleKey ruleKey2 = new RuleKey("11111111111111111111111111111111");
final String data = "data";
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
Path output = Paths.get("output/file");
filesystem.writeContentsToPath(data, output);
final Set<RuleKey> stored = Sets.newHashSet();
argsBuilder.setProjectFilesystem(filesystem);
argsBuilder.setStoreClient(withMakeRequest(((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER).build();
Buffer buf = new Buffer();
request.body().writeTo(buf);
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(buf.readByteArray()))) {
int keys = in.readInt();
for (int i = 0; i < keys; i++) {
stored.add(new RuleKey(in.readUTF()));
}
}
Response response = new Response.Builder().body(createDummyBody()).code(HttpURLConnection.HTTP_ACCEPTED).protocol(Protocol.HTTP_1_1).request(request).build();
return new OkHttpResponseWrapper(response);
})));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
cache.storeImpl(ArtifactInfo.builder().addRuleKeys(ruleKey1, ruleKey2).build(), output, createFinishedEventBuilder());
assertThat(stored, Matchers.containsInAnyOrder(ruleKey1, ruleKey2));
cache.close();
}
use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.
the class HttpArtifactCacheTest method testFetchUrl.
@Test
public void testFetchUrl() throws Exception {
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final String expectedUri = "/artifacts/key/00000000000000000000000000000000";
argsBuilder.setFetchClient(withMakeRequest((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER + path).build();
assertEquals(expectedUri, request.url().encodedPath());
return new OkHttpResponseWrapper(new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap(new byte[0]), "data")).build());
}));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
cache.fetch(ruleKey, LazyPath.ofInstance(Paths.get("output/file")));
cache.close();
}
use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.
the class HttpArtifactCacheTest method testFetchExtraPayload.
@Test
public void testFetchExtraPayload() throws Exception {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final List<Response> responseList = Lists.newArrayList();
argsBuilder.setFetchClient(withMakeRequest((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER + path).build();
Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap("more data than length".getBytes(Charsets.UTF_8)), "small")).build();
responseList.add(response);
return new OkHttpResponseWrapper(response);
}));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
Path output = Paths.get("output/file");
CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
assertEquals(CacheResultType.ERROR, result.getType());
assertEquals(Optional.empty(), filesystem.readFileIfItExists(output));
assertTrue("response wasn't fully read!", responseList.get(0).body().source().exhausted());
cache.close();
}
Aggregations