use of okhttp3.Protocol 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 okhttp3.Protocol 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 okhttp3.Protocol 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();
}
use of okhttp3.Protocol in project buck by facebook.
the class ClientSideSlbTest method testAllServersArePinged.
@Test
@SuppressWarnings("unchecked")
public void testAllServersArePinged() throws IOException {
Capture<Runnable> capture = EasyMock.newCapture();
EasyMock.expect(mockScheduler.scheduleWithFixedDelay(EasyMock.capture(capture), EasyMock.anyLong(), EasyMock.anyLong(), EasyMock.anyObject(TimeUnit.class))).andReturn(mockFuture).once();
Call mockCall = createMock(Call.class);
for (URI server : SERVERS) {
EasyMock.expect(mockClient.newCall(EasyMock.anyObject(Request.class))).andReturn(mockCall);
mockCall.enqueue(EasyMock.anyObject(ClientSideSlb.ServerPing.class));
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
Callback callback = (Callback) EasyMock.getCurrentArguments()[0];
ResponseBody body = ResponseBody.create(MediaType.parse("text/plain"), "The Body.");
Response response = new Response.Builder().body(body).code(200).protocol(Protocol.HTTP_1_1).request(new Request.Builder().url(server.toString()).build()).build();
callback.onResponse(mockCall, response);
return null;
}
});
}
mockBus.post(EasyMock.anyObject(LoadBalancerPingEvent.class));
EasyMock.expectLastCall();
EasyMock.expect(mockFuture.cancel(true)).andReturn(true).once();
replayAll();
try (ClientSideSlb slb = new ClientSideSlb(config, mockClient)) {
Runnable healthCheckLoop = capture.getValue();
healthCheckLoop.run();
}
verifyAll();
}
use of okhttp3.Protocol in project buck by facebook.
the class SingleUriServiceTest method testClientIsCalledWithFullUrl.
@Test
public void testClientIsCalledWithFullUrl() throws IOException, InterruptedException {
OkHttpClient mockClient = EasyMock.createMock(OkHttpClient.class);
String path = "my/super/path";
Request.Builder request = new Request.Builder().url(SERVER + path).get();
Call mockCall = EasyMock.createMock(Call.class);
EasyMock.expect(mockClient.newCall(EasyMock.anyObject(Request.class))).andReturn(mockCall);
Response response = new Response.Builder().message("my super response").request(request.build()).protocol(Protocol.HTTP_1_1).code(200).build();
EasyMock.expect(mockCall.execute()).andReturn(response);
EasyMock.replay(mockCall, mockClient);
try (SingleUriService service = new SingleUriService(SERVER, mockClient)) {
service.makeRequest(path, request);
}
}
Aggregations