use of com.tonyodev.fetch2.Request in project okhttp by square.
the class HttpOverHttp2Test method settingsLimitsMaxConcurrentStreams.
/**
* Push a setting that permits up to 2 concurrent streams, then make 3 concurrent requests and
* confirm that the third concurrent request prepared a new connection.
*/
@Test
public void settingsLimitsMaxConcurrentStreams() throws Exception {
Settings settings = new Settings();
settings.set(Settings.MAX_CONCURRENT_STREAMS, 2);
// Read & write a full request to confirm settings are accepted.
server.enqueue(new MockResponse().withSettings(settings));
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("", response.body().string());
server.enqueue(new MockResponse().setBody("ABC"));
server.enqueue(new MockResponse().setBody("DEF"));
server.enqueue(new MockResponse().setBody("GHI"));
Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response1 = call1.execute();
Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response2 = call2.execute();
Call call3 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response3 = call3.execute();
assertEquals("ABC", response1.body().string());
assertEquals("DEF", response2.body().string());
assertEquals("GHI", response3.body().string());
// Settings connection.
assertEquals(0, server.takeRequest().getSequenceNumber());
// Reuse settings connection.
assertEquals(1, server.takeRequest().getSequenceNumber());
// Reuse settings connection.
assertEquals(2, server.takeRequest().getSequenceNumber());
// New connection!
assertEquals(0, server.takeRequest().getSequenceNumber());
}
use of com.tonyodev.fetch2.Request in project okhttp by square.
the class HttpOverHttp2Test method domainFronting.
/** https://github.com/square/okhttp/issues/3103 */
@Test
public void domainFronting() throws Exception {
client = client.newBuilder().addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().header("Host", "privateobject.com").build();
return chain.proceed(request);
}
}).build();
server.enqueue(new MockResponse());
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("", response.body().string());
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("privateobject.com", recordedRequest.getHeader(":authority"));
}
use of com.tonyodev.fetch2.Request in project okhttp by square.
the class HttpOverHttp2Test method sendRequestCookies.
@Test
public void sendRequestCookies() throws Exception {
RecordingCookieJar cookieJar = new RecordingCookieJar();
Cookie requestCookie = new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build();
cookieJar.enqueueRequestCookies(requestCookie);
client = client.newBuilder().cookieJar(cookieJar).build();
server.enqueue(new MockResponse());
Call call = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response = call.execute();
assertEquals("", response.body().string());
RecordedRequest request = server.takeRequest();
assertEquals("a=b", request.getHeader("Cookie"));
}
use of com.tonyodev.fetch2.Request in project realm-java by realm.
the class HttpUtils method waitAuthServerReady.
// Checking the server
private static boolean waitAuthServerReady() throws InterruptedException {
int retryTimes = 20;
// Dummy invalid request, which will trigger a 400 (BAD REQUEST), but indicate the auth
// server is responsive
Request request = new Request.Builder().url(Constants.AUTH_SERVER_URL).build();
while (retryTimes != 0) {
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
return true;
}
RealmLog.error("Error response from auth server: %s", response.toString());
} catch (IOException e) {
// TODO As long as the auth server hasn't started yet, OKHttp cannot parse the response
// correctly. At this point it is unknown weather is a bug in OKHttp or an
// unknown host is reported. This can cause a lot of "false" errors in the log.
RealmLog.error(e);
Thread.sleep(500);
} finally {
if (response != null) {
response.close();
}
}
retryTimes--;
}
return false;
}
use of com.tonyodev.fetch2.Request in project okhttp by square.
the class OkHttpURLConnection method buildCall.
private Call buildCall() throws IOException {
if (call != null) {
return call;
}
connected = true;
if (doOutput) {
if (method.equals("GET")) {
// they are requesting a stream to write to. This implies a POST method
method = "POST";
} else if (!HttpMethod.permitsRequestBody(method)) {
throw new ProtocolException(method + " does not support writing");
}
}
if (requestHeaders.get("User-Agent") == null) {
requestHeaders.add("User-Agent", defaultUserAgent());
}
OutputStreamRequestBody requestBody = null;
if (HttpMethod.permitsRequestBody(method)) {
// Add a content type for the request body, if one isn't already present.
String contentType = requestHeaders.get("Content-Type");
if (contentType == null) {
contentType = "application/x-www-form-urlencoded";
requestHeaders.add("Content-Type", contentType);
}
boolean stream = fixedContentLength != -1L || chunkLength > 0;
long contentLength = -1L;
String contentLengthString = requestHeaders.get("Content-Length");
if (fixedContentLength != -1L) {
contentLength = fixedContentLength;
} else if (contentLengthString != null) {
contentLength = Long.parseLong(contentLengthString);
}
requestBody = stream ? new StreamedRequestBody(contentLength) : new BufferedRequestBody(contentLength);
requestBody.timeout().timeout(client.writeTimeoutMillis(), TimeUnit.MILLISECONDS);
}
Request request = new Request.Builder().url(Internal.instance.getHttpUrlChecked(getURL().toString())).headers(requestHeaders.build()).method(method, requestBody).build();
if (urlFilter != null) {
urlFilter.checkURLPermitted(request.url().url());
}
OkHttpClient.Builder clientBuilder = client.newBuilder();
clientBuilder.interceptors().clear();
clientBuilder.interceptors().add(UnexpectedException.INTERCEPTOR);
clientBuilder.networkInterceptors().clear();
clientBuilder.networkInterceptors().add(networkInterceptor);
// Use a separate dispatcher so that limits aren't impacted. But use the same executor service!
clientBuilder.dispatcher(new Dispatcher(client.dispatcher().executorService()));
// If we're currently not using caches, make sure the engine's client doesn't have one.
if (!getUseCaches()) {
clientBuilder.cache(null);
}
return call = clientBuilder.build().newCall(request);
}
Aggregations