use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class HttpOverHttp2Test method userSuppliedContentLengthHeader.
@Test
public void userSuppliedContentLengthHeader() throws Exception {
final byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8);
server.enqueue(new MockResponse().setBody("ABCDE"));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).post(new RequestBody() {
@Override
public MediaType contentType() {
return MediaType.parse("text/plain; charset=utf-8");
}
@Override
public long contentLength() throws IOException {
return postBytes.length;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write(postBytes);
}
}).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
RecordedRequest request = server.takeRequest();
assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
assertArrayEquals(postBytes, request.getBody().readByteArray());
assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length")));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class HttpOverHttp2Test method get.
@Test
public void get() throws Exception {
server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet"));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
assertEquals(200, response.code());
assertEquals("Sweet", response.message());
RecordedRequest request = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
assertEquals("https", request.getHeader(":scheme"));
assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class HttpOverHttp2Test method serverSendsPushPromise_GET.
@Test
public void serverSendsPushPromise_GET() throws Exception {
PushPromise pushPromise = new PushPromise("GET", "/foo/bar", Headers.of("foo", "bar"), new MockResponse().setBody("bar").setStatus("HTTP/1.1 200 Sweet"));
server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet").withPush(pushPromise));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
assertEquals(200, response.code());
assertEquals("Sweet", response.message());
RecordedRequest request = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
assertEquals("https", request.getHeader(":scheme"));
assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
RecordedRequest pushedRequest = server.takeRequest();
assertEquals("GET /foo/bar HTTP/1.1", pushedRequest.getRequestLine());
assertEquals("bar", pushedRequest.getHeader("foo"));
}
use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.
the class HttpOverHttp2Test method serverSendsPushPromise_HEAD.
@Test
public void serverSendsPushPromise_HEAD() throws Exception {
PushPromise pushPromise = new PushPromise("HEAD", "/foo/bar", Headers.of("foo", "bar"), new MockResponse().setStatus("HTTP/1.1 204 Sweet"));
server.enqueue(new MockResponse().setBody("ABCDE").setStatus("HTTP/1.1 200 Sweet").withPush(pushPromise));
Call call = client.newCall(new Request.Builder().url(server.url("/foo")).build());
Response response = call.execute();
assertEquals("ABCDE", response.body().string());
assertEquals(200, response.code());
assertEquals("Sweet", response.message());
RecordedRequest request = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request.getRequestLine());
assertEquals("https", request.getHeader(":scheme"));
assertEquals(server.getHostName() + ":" + server.getPort(), request.getHeader(":authority"));
RecordedRequest pushedRequest = server.takeRequest();
assertEquals("HEAD /foo/bar HTTP/1.1", pushedRequest.getRequestLine());
assertEquals("bar", pushedRequest.getHeader("foo"));
}
use of okhttp3.mockwebserver.RecordedRequest 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"));
}
Aggregations