use of okhttp3.Response in project buck by facebook.
the class HttpArtifactCacheTest method errorTextReplaced.
@Test
public void errorTextReplaced() throws InterruptedException {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
final String cacheName = "http cache";
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final RuleKey otherRuleKey = new RuleKey("11111111111111111111111111111111");
final String data = "data";
final AtomicBoolean consoleEventReceived = new AtomicBoolean(false);
argsBuilder.setCacheName(cacheName).setProjectFilesystem(filesystem).setBuckEventBus(new BuckEventBus(new IncrementingFakeClock(), new BuildId()) {
@Override
public void post(BuckEvent event) {
if (event instanceof ConsoleEvent) {
consoleEventReceived.set(true);
ConsoleEvent consoleEvent = (ConsoleEvent) event;
assertThat(consoleEvent.getMessage(), Matchers.containsString(cacheName));
assertThat(consoleEvent.getMessage(), Matchers.containsString("incorrect key name"));
}
}
}).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(otherRuleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
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(consoleEventReceived.get());
cache.close();
}
use of okhttp3.Response in project buck by facebook.
the class HttpArtifactCacheTest method testStore.
@Test
public void testStore() throws Exception {
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final String data = "data";
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
Path output = Paths.get("output/file");
filesystem.writeContentsToPath(data, output);
final AtomicBoolean hasCalled = new AtomicBoolean(false);
argsBuilder.setProjectFilesystem(filesystem);
argsBuilder.setStoreClient(withMakeRequest(((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER).build();
hasCalled.set(true);
Buffer buf = new Buffer();
request.body().writeTo(buf);
byte[] actualData = buf.readByteArray();
byte[] expectedData;
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(out)) {
dataOut.write(HttpArtifactCacheBinaryProtocol.createKeysHeader(ImmutableSet.of(ruleKey)));
byte[] metadata = HttpArtifactCacheBinaryProtocol.createMetadataHeader(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)));
dataOut.writeInt(metadata.length);
dataOut.write(metadata);
dataOut.write(data.getBytes(Charsets.UTF_8));
expectedData = out.toByteArray();
}
assertArrayEquals(expectedData, actualData);
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(ruleKey).build(), output, createFinishedEventBuilder());
assertTrue(hasCalled.get());
cache.close();
}
use of okhttp3.Response in project buck by facebook.
the class HttpArtifactCacheTest method testFetchWrongKey.
@Test
public void testFetchWrongKey() throws Exception {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final RuleKey otherRuleKey = new RuleKey("11111111111111111111111111111111");
final String data = "data";
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(otherRuleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
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));
cache.close();
}
use of okhttp3.Response in project Parse-SDK-Android by ParsePlatform.
the class ParseOkHttpClient method executeInternal.
@Override
/* package */
ParseHttpResponse executeInternal(ParseHttpRequest parseRequest) throws IOException {
Request okHttpRequest = getRequest(parseRequest);
Call okHttpCall = okHttpClient.newCall(okHttpRequest);
Response okHttpResponse = okHttpCall.execute();
return getResponse(okHttpResponse);
}
use of okhttp3.Response in project Parse-SDK-Android by ParsePlatform.
the class ParseHttpClientTest method doSingleParseHttpClientExecuteWithResponse.
private void doSingleParseHttpClientExecuteWithResponse(int responseCode, String responseStatus, String responseContent, ParseHttpClient client) throws Exception {
MockWebServer server = new MockWebServer();
// Make mock response
int responseContentLength = responseContent.length();
MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + responseCode + " " + responseStatus).setBody(responseContent);
// Start mock server
server.enqueue(mockResponse);
server.start();
// Make ParseHttpRequest
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("User-Agent", "Parse Android SDK");
String requestUrl = server.url("/").toString();
JSONObject json = new JSONObject();
json.put("key", "value");
String requestContent = json.toString();
int requestContentLength = requestContent.length();
String requestContentType = "application/json";
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody(requestContent, requestContentType)).setHeaders(requestHeaders).build();
// Execute request
ParseHttpResponse parseResponse = client.execute(parseRequest);
RecordedRequest recordedApacheRequest = server.takeRequest();
// Verify request method
assertEquals(ParseHttpRequest.Method.POST.toString(), recordedApacheRequest.getMethod());
// Verify request headers, since http library automatically adds some headers, we only need to
// verify all parseRequest headers are in recordedRequest headers.
Headers recordedApacheHeaders = recordedApacheRequest.getHeaders();
Set<String> recordedApacheHeadersNames = recordedApacheHeaders.names();
for (String name : parseRequest.getAllHeaders().keySet()) {
assertTrue(recordedApacheHeadersNames.contains(name));
assertEquals(parseRequest.getAllHeaders().get(name), recordedApacheHeaders.get(name));
}
// Verify request body
assertEquals(requestContentLength, recordedApacheRequest.getBodySize());
assertArrayEquals(requestContent.getBytes(), recordedApacheRequest.getBody().readByteArray());
// Verify response status code
assertEquals(responseCode, parseResponse.getStatusCode());
// Verify response status
assertEquals(responseStatus, parseResponse.getReasonPhrase());
// Verify all response header entries' keys and values are not null.
for (Map.Entry<String, String> entry : parseResponse.getAllHeaders().entrySet()) {
assertNotNull(entry.getKey());
assertNotNull(entry.getValue());
}
// Verify response body
byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
assertArrayEquals(responseContent.getBytes(), content);
// Verify response body size
assertEquals(responseContentLength, content.length);
// Shutdown mock server
server.shutdown();
}
Aggregations