use of okhttp3.Response in project Parse-SDK-Android by ParsePlatform.
the class ParseHttpClientTest method doSingleParseHttpClientExecuteWithGzipResponse.
private void doSingleParseHttpClientExecuteWithGzipResponse(int responseCode, String responseStatus, final String responseContent, ParseHttpClient client) throws Exception {
MockWebServer server = new MockWebServer();
// Make mock response
Buffer buffer = new Buffer();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
gzipOut.write(responseContent.getBytes());
gzipOut.close();
buffer.write(byteOut.toByteArray());
MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + responseCode + " " + responseStatus).setBody(buffer).setHeader("Content-Encoding", "gzip");
// Start mock server
server.enqueue(mockResponse);
server.start();
// We do not need to add Accept-Encoding header manually, httpClient library should do that.
String requestUrl = server.url("/").toString();
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.GET).build();
// Execute request
ParseHttpResponse parseResponse = client.execute(parseRequest);
RecordedRequest recordedRequest = server.takeRequest();
// Verify request method
assertEquals(ParseHttpRequest.Method.GET.toString(), recordedRequest.getMethod());
// Verify request headers
Headers recordedHeaders = recordedRequest.getHeaders();
assertEquals("gzip", recordedHeaders.get("Accept-Encoding"));
// Verify we do not have Content-Encoding header
assertNull(parseResponse.getHeader("Content-Encoding"));
// Verify response body
byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
assertArrayEquals(responseContent.getBytes(), content);
// Shutdown mock server
server.shutdown();
}
use of okhttp3.Response in project Parse-SDK-Android by ParsePlatform.
the class ParseOkHttpClientTest method testGetOkHttpRequestType.
//region testTransferRequest/Response
@Test
public void testGetOkHttpRequestType() throws IOException {
ParseOkHttpClient parseClient = new ParseOkHttpClient(10000, null);
ParseHttpRequest.Builder builder = new ParseHttpRequest.Builder();
builder.setUrl("http://www.parse.com");
// Get
ParseHttpRequest parseRequest = builder.setMethod(ParseHttpRequest.Method.GET).setBody(null).build();
Request okHttpRequest = parseClient.getRequest(parseRequest);
assertEquals(ParseHttpRequest.Method.GET.toString(), okHttpRequest.method());
// Post
parseRequest = builder.setMethod(ParseHttpRequest.Method.POST).setBody(new ParseByteArrayHttpBody("test", "application/json")).build();
okHttpRequest = parseClient.getRequest(parseRequest);
assertEquals(ParseHttpRequest.Method.POST.toString(), okHttpRequest.method());
// Delete
parseRequest = builder.setMethod(ParseHttpRequest.Method.DELETE).setBody(null).build();
okHttpRequest = parseClient.getRequest(parseRequest);
assertEquals(ParseHttpRequest.Method.DELETE.toString(), okHttpRequest.method());
// Put
parseRequest = builder.setMethod(ParseHttpRequest.Method.PUT).setBody(new ParseByteArrayHttpBody("test", "application/json")).build();
okHttpRequest = parseClient.getRequest(parseRequest);
assertEquals(ParseHttpRequest.Method.PUT.toString(), okHttpRequest.method());
}
use of okhttp3.Response in project Parse-SDK-Android by ParsePlatform.
the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse.
@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
// Make mock response
Buffer buffer = new Buffer();
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
gzipOut.write("content".getBytes());
gzipOut.close();
buffer.write(byteOut.toByteArray());
MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer).setHeader("Content-Encoding", "gzip");
// Start mock server
server.enqueue(mockResponse);
server.start();
ParseHttpClient client = new ParseOkHttpClient(10000, null);
final Semaphore done = new Semaphore(0);
// Add plain interceptor to disable decompress response stream
client.addExternalInterceptor(new ParseNetworkInterceptor() {
@Override
public ParseHttpResponse intercept(Chain chain) throws IOException {
done.release();
ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
// Make sure the response we get from the interceptor is the raw gzip stream
byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
assertArrayEquals(byteOut.toByteArray(), content);
// We need to set a new stream since we have read it
return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray())).build();
}
});
// We do not need to add Accept-Encoding header manually, httpClient library should do that.
String requestUrl = server.url("/").toString();
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.GET).build();
// Execute request
ParseHttpResponse parseResponse = client.execute(parseRequest);
// Make sure the response we get is ungziped by OkHttp library
byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
assertArrayEquals("content".getBytes(), content);
// Make sure interceptor is called
assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));
server.shutdown();
}
use of okhttp3.Response in project BigImageViewer by Piasy.
the class GlideProgressSupport method createInterceptor.
private static Interceptor createInterceptor(final ResponseProgressListener listener) {
return new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
return response.newBuilder().body(new OkHttpProgressResponseBody(request.url(), response.body(), listener)).build();
}
};
}
use of okhttp3.Response in project sonarqube by SonarSource.
the class HttpHeadersTest method verify_headers_of_js_provided_by_plugins.
@Test
public void verify_headers_of_js_provided_by_plugins() throws Exception {
Response response = call(orchestrator.getServer().getUrl() + "/static/uiextensionsplugin/extension.js");
verifySecurityHeaders(response);
verifyContentType(response, "application/javascript");
}
Aggregations