use of mockwebserver3.MockResponse in project okhttp by square.
the class HttpLoggingInterceptorTest method basicPost.
@Test
public void basicPost() throws IOException {
setLevel(Level.BASIC);
server.enqueue(new MockResponse());
client.newCall(request().post(RequestBody.create("Hi?", PLAIN)).build()).execute();
applicationLogs.assertLogEqual("--> POST " + url + " (3-byte body)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)").assertNoMoreLogs();
networkLogs.assertLogEqual("--> POST " + url + " http/1.1 (3-byte body)").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, 0-byte body\\)").assertNoMoreLogs();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class HttpLoggingInterceptorTest method http2.
@Test
public void http2() throws Exception {
platform.assumeNotBouncyCastle();
server.useHttps(handshakeCertificates.sslSocketFactory(), false);
url = server.url("/");
setLevel(Level.BASIC);
server.enqueue(new MockResponse());
Response response = client.newCall(request().build()).execute();
assumeTrue(response.protocol().equals(Protocol.HTTP_2));
applicationLogs.assertLogEqual("--> GET " + url).assertLogMatch("<-- 200 " + url + " \\(\\d+ms, 0-byte body\\)").assertNoMoreLogs();
networkLogs.assertLogEqual("--> GET " + url + " h2").assertLogMatch("<-- 200 " + url + " \\(\\d+ms, 0-byte body\\)").assertNoMoreLogs();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class HttpLoggingInterceptorTest method basicChunkedResponseBody.
@Test
public void basicChunkedResponseBody() throws IOException {
setLevel(Level.BASIC);
server.enqueue(new MockResponse().setChunkedBody("Hello!", 2).setHeader("Content-Type", PLAIN));
Response response = client.newCall(request().build()).execute();
response.body().close();
applicationLogs.assertLogEqual("--> GET " + url).assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)").assertNoMoreLogs();
networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms, unknown-length body\\)").assertNoMoreLogs();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class HttpLoggingInterceptorTest method bodyResponseUnknownEncoded.
@Test
public void bodyResponseUnknownEncoded() throws IOException {
setLevel(Level.BODY);
server.enqueue(new MockResponse().setHeader("Content-Encoding", "br").setHeader("Content-Type", PLAIN).setBody(new Buffer().write(ByteString.decodeBase64("iwmASGVsbG8sIEhlbGxvLCBIZWxsbwoD"))));
Response response = client.newCall(request().build()).execute();
response.body().close();
networkLogs.assertLogEqual("--> GET " + url + " http/1.1").assertLogEqual("Host: " + host).assertLogEqual("Connection: Keep-Alive").assertLogEqual("Accept-Encoding: gzip").assertLogMatch("User-Agent: okhttp/.+").assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Encoding: br").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogMatch("Content-Length: \\d+").assertLogEqual("<-- END HTTP (encoded body omitted)").assertNoMoreLogs();
applicationLogs.assertLogEqual("--> GET " + url).assertLogEqual("--> END GET").assertLogMatch("<-- 200 OK " + url + " \\(\\d+ms\\)").assertLogEqual("Content-Encoding: br").assertLogEqual("Content-Type: text/plain; charset=utf-8").assertLogMatch("Content-Length: \\d+").assertLogEqual("<-- END HTTP (encoded body omitted)").assertNoMoreLogs();
}
use of mockwebserver3.MockResponse in project okhttp by square.
the class CertificatePinnerChainValidationTest method pinIntermediatePresentInChain.
/**
* The pinner should accept an intermediate from the server's chain.
*/
@Test
public void pinIntermediatePresentInChain() throws Exception {
// Fails on 11.0.1 https://github.com/square/okhttp/issues/4703
HeldCertificate rootCa = new HeldCertificate.Builder().serialNumber(1L).certificateAuthority(1).commonName("root").build();
HeldCertificate intermediateCa = new HeldCertificate.Builder().signedBy(rootCa).certificateAuthority(0).serialNumber(2L).commonName("intermediate_ca").build();
HeldCertificate certificate = new HeldCertificate.Builder().signedBy(intermediateCa).serialNumber(3L).commonName(server.getHostName()).build();
CertificatePinner certificatePinner = new CertificatePinner.Builder().add(server.getHostName(), CertificatePinner.pin(intermediateCa.certificate())).build();
HandshakeCertificates handshakeCertificates = new HandshakeCertificates.Builder().addTrustedCertificate(rootCa.certificate()).build();
OkHttpClient client = clientTestRule.newClientBuilder().sslSocketFactory(handshakeCertificates.sslSocketFactory(), handshakeCertificates.trustManager()).hostnameVerifier(new RecordingHostnameVerifier()).certificatePinner(certificatePinner).build();
HandshakeCertificates serverHandshakeCertificates = new HandshakeCertificates.Builder().heldCertificate(certificate, intermediateCa.certificate()).build();
server.useHttps(serverHandshakeCertificates.sslSocketFactory(), false);
// The request should complete successfully.
server.enqueue(new MockResponse().setBody("abc").setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
Call call1 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response1 = call1.execute();
assertThat(response1.body().string()).isEqualTo("abc");
response1.close();
// Force a fresh connection for the next request.
client.connectionPool().evictAll();
// Confirm that a second request also succeeds. This should detect caching problems.
server.enqueue(new MockResponse().setBody("def").setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
Call call2 = client.newCall(new Request.Builder().url(server.url("/")).build());
Response response2 = call2.execute();
assertThat(response2.body().string()).isEqualTo("def");
response2.close();
}
Aggregations