本文整理汇总了Java中okhttp3.mockwebserver.RecordedRequest类的典型用法代码示例。如果您正苦于以下问题:Java RecordedRequest类的具体用法?Java RecordedRequest怎么用?Java RecordedRequest使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
RecordedRequest类属于okhttp3.mockwebserver包,在下文中一共展示了RecordedRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: authenticate
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void authenticate() throws Exception {
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED)
.addHeader("www-authenticate: Basic realm=\"protected area\"")
.setBody("Please authenticate."));
server.enqueue(new MockResponse()
.setBody("Successful auth!"));
String credential = Credentials.basic("username", "password");
client = client.newBuilder()
.authenticator(new RecordingOkAuthenticator(credential))
.build();
Call call = client.newCall(new Request.Builder()
.url(server.url("/"))
.build());
Response response = call.execute();
assertEquals("Successful auth!", response.body().string());
RecordedRequest denied = server.takeRequest();
assertNull(denied.getHeader("Authorization"));
RecordedRequest accepted = server.takeRequest();
assertEquals("GET / HTTP/1.1", accepted.getRequestLine());
assertEquals(credential, accepted.getHeader("Authorization"));
}
示例2: noTransparentGzipFor304NotModified
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
/**
* We had a bug where we attempted to gunzip responses that didn't have a body. This only came up
* with 304s since that response code can include headers (like "Content-Encoding") without any
* content to go along with it. https://github.com/square/okhttp/issues/358
*/
@Test public void noTransparentGzipFor304NotModified() throws Exception {
server.enqueue(new MockResponse()
.clearHeaders()
.setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED)
.addHeader("Content-Encoding: gzip"));
server.enqueue(new MockResponse().setBody("b"));
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, connection1.getResponseCode());
assertContent("", connection1);
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
assertEquals(HttpURLConnection.HTTP_OK, connection2.getResponseCode());
assertContent("b", connection2);
RecordedRequest requestA = server.takeRequest();
assertEquals(0, requestA.getSequenceNumber());
RecordedRequest requestB = server.takeRequest();
assertEquals(1, requestB.getSequenceNumber());
}
示例3: redirectToCachedResult
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void redirectToCachedResult() throws Exception {
server.enqueue(new MockResponse()
.addHeader("Cache-Control: max-age=60")
.setBody("ABC"));
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_PERM)
.addHeader("Location: /foo"));
server.enqueue(new MockResponse()
.setBody("DEF"));
assertEquals("ABC", readAscii(openConnection(server.url("/foo").url())));
RecordedRequest request1 = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request1.getRequestLine());
assertEquals(0, request1.getSequenceNumber());
assertEquals("ABC", readAscii(openConnection(server.url("/bar").url())));
RecordedRequest request2 = server.takeRequest();
assertEquals("GET /bar HTTP/1.1", request2.getRequestLine());
assertEquals(1, request2.getSequenceNumber());
// an unrelated request should reuse the pooled connection
assertEquals("DEF", readAscii(openConnection(server.url("/baz").url())));
RecordedRequest request3 = server.takeRequest();
assertEquals("GET /baz HTTP/1.1", request3.getRequestLine());
assertEquals(2, request3.getSequenceNumber());
}
示例4: testSendingCookiesFromStore
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void testSendingCookiesFromStore() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse());
server.start();
HttpUrl serverUrl = urlWithIpAddress(server, "/");
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
HttpCookie cookieA = new HttpCookie("a", "android");
cookieA.setDomain(serverUrl.host());
cookieA.setPath("/");
cookieManager.getCookieStore().add(serverUrl.uri(), cookieA);
HttpCookie cookieB = new HttpCookie("b", "banana");
cookieB.setDomain(serverUrl.host());
cookieB.setPath("/");
cookieManager.getCookieStore().add(serverUrl.uri(), cookieB);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
get(serverUrl);
RecordedRequest request = server.takeRequest();
assertEquals("a=android; b=banana", request.getHeader("Cookie"));
}
示例5: put
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void put() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
Request request = new Request.Builder()
.url(server.url("/"))
.put(RequestBody.create(MediaType.parse("text/plain"), "def"))
.build();
executeSynchronously(request)
.assertCode(200)
.assertBody("abc");
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("PUT", recordedRequest.getMethod());
assertEquals("def", recordedRequest.getBody().readUtf8());
assertEquals("3", recordedRequest.getHeader("Content-Length"));
assertEquals("text/plain; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
示例6: main
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
public static void main(String... args) throws IOException, InterruptedException {
MockWebServer server = new MockWebServer();
server.start();
server.enqueue(new MockResponse());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(server.url("/"))
.addConverterFactory(new JsonStringConverterFactory(GsonConverterFactory.create()))
.build();
Service service = retrofit.create(Service.class);
Call<ResponseBody> call = service.example(new Filter("123"));
Response<ResponseBody> response = call.execute();
// TODO handle user response...
// Print the request path that the server saw to show the JSON query param:
RecordedRequest recordedRequest = server.takeRequest();
System.out.println(recordedRequest.getPath());
server.shutdown();
}
示例7: redirectToCachedResult
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void redirectToCachedResult() throws Exception {
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("ABC"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM)
.addHeader("Location: /foo"));
server.enqueue(new MockResponse().setBody("DEF"));
assertEquals("ABC", readAscii(urlFactory.open(server.url("/foo").url())));
RecordedRequest request1 = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request1.getRequestLine());
assertEquals(0, request1.getSequenceNumber());
assertEquals("ABC", readAscii(urlFactory.open(server.url("/bar").url())));
RecordedRequest request2 = server.takeRequest();
assertEquals("GET /bar HTTP/1.1", request2.getRequestLine());
assertEquals(1, request2.getSequenceNumber());
// an unrelated request should reuse the pooled connection
assertEquals("DEF", readAscii(urlFactory.open(server.url("/baz").url())));
RecordedRequest request3 = server.takeRequest();
assertEquals("GET /baz HTTP/1.1", request3.getRequestLine());
assertEquals(2, request3.getSequenceNumber());
}
示例8: doUpload
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
private void doUpload(TransferKind uploadKind, WriteKind writeKind) throws Exception {
int n = 512 * 1024;
server.setBodyLimit(0);
server.enqueue(new MockResponse());
HttpURLConnection conn = urlFactory.open(server.url("/").url());
conn.setDoOutput(true);
conn.setRequestMethod("POST");
if (uploadKind == TransferKind.CHUNKED) {
conn.setChunkedStreamingMode(-1);
} else {
conn.setFixedLengthStreamingMode(n);
}
OutputStream out = conn.getOutputStream();
if (writeKind == WriteKind.BYTE_BY_BYTE) {
for (int i = 0; i < n; ++i) {
out.write('x');
}
} else {
byte[] buf = new byte[writeKind == WriteKind.SMALL_BUFFERS ? 256 : 64 * 1024];
Arrays.fill(buf, (byte) 'x');
for (int i = 0; i < n; i += buf.length) {
out.write(buf, 0, Math.min(buf.length, n - i));
}
}
out.close();
assertEquals(200, conn.getResponseCode());
RecordedRequest request = server.takeRequest();
assertEquals(n, request.getBodySize());
if (uploadKind == TransferKind.CHUNKED) {
assertTrue(request.getChunkSizes().size() > 0);
} else {
assertTrue(request.getChunkSizes().isEmpty());
}
}
示例9: testResponseRedirectedWithPost
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
private void testResponseRedirectedWithPost(int redirectCode, TransferKind transferKind)
throws Exception {
server.enqueue(new MockResponse().setResponseCode(redirectCode)
.addHeader("Location: /page2")
.setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("Page 2"));
connection = urlFactory.open(server.url("/page1").url());
connection.setDoOutput(true);
transferKind.setForRequest(connection, 4);
byte[] requestBody = {'A', 'B', 'C', 'D'};
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestBody);
outputStream.close();
assertEquals("Page 2", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
assertTrue(connection.getDoOutput());
RecordedRequest page1 = server.takeRequest();
assertEquals("POST /page1 HTTP/1.1", page1.getRequestLine());
assertEquals("ABCD", page1.getBody().readUtf8());
RecordedRequest page2 = server.takeRequest();
assertEquals("GET /page2 HTTP/1.1", page2.getRequestLine());
}
示例10: receiveAndSendMultipleCookies
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void receiveAndSendMultipleCookies() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse()
.addHeader("Set-Cookie", "a=android")
.addHeader("Set-Cookie", "b=banana"));
server.enqueue(new MockResponse());
server.start();
CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
client = client.newBuilder()
.cookieJar(new JavaNetCookieJar(cookieManager))
.build();
get(urlWithIpAddress(server, "/"));
RecordedRequest request1 = server.takeRequest();
assertNull(request1.getHeader("Cookie"));
get(urlWithIpAddress(server, "/"));
RecordedRequest request2 = server.takeRequest();
assertEquals("a=android; b=banana", request2.getHeader("Cookie"));
}
示例11: deleteWithRequestBody
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void deleteWithRequestBody() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
Request request = new Request.Builder()
.url(server.url("/"))
.method("DELETE", RequestBody.create(MediaType.parse("text/plain"), "def"))
.build();
executeSynchronously(request)
.assertCode(200)
.assertBody("abc");
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("DELETE", recordedRequest.getMethod());
assertEquals("def", recordedRequest.getBody().readUtf8());
}
示例12: requestCookies
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void requestCookies() throws Exception {
server.enqueue(new MockResponse());
RecordingCookieJar cookieJar = new RecordingCookieJar();
cookieJar.enqueueRequestCookies(
new Cookie.Builder().name("a").value("b").domain(server.getHostName()).build(),
new Cookie.Builder().name("c").value("d").domain(server.getHostName()).build());
client = client.newBuilder()
.cookieJar(cookieJar)
.build();
executeSynchronously("/").assertCode(200);
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("a=b; c=d", recordedRequest.getHeader("Cookie"));
}
示例13: shouldStartFlow
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test
public void shouldStartFlow() throws Exception {
server.jsonResponse(MockServer.START_FLOW_VALID, 201);
StartFlowResponse startFlowResponse = apiClient
.startFlow(ENROLLMENT_TICKET)
.execute();
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest, hasMethodAndPath("POST", "/api/start-flow"));
assertThat(recordedRequest, hasHeader("Content-Type", "application/json; charset=utf-8"));
assertThat(recordedRequest, hasHeader("Authorization", "Ticket id=\"ENROLLMENT_TICKET\""));
Map<String, Object> body = bodyFromRequest(recordedRequest);
assertThat(body, hasEntry("state_transport", (Object) "polling"));
assertThat(startFlowResponse.getTransactionToken(), is(equalTo("THE_TRANSACTION_TOKEN")));
assertThat(startFlowResponse.getDeviceAccount(), is(notNullValue()));
assertThat(startFlowResponse.getDeviceAccount().getId(), is(equalTo("THE_ENROLLMENT_ID")));
assertThat(startFlowResponse.getDeviceAccount().getStatus(), is(equalTo("confirmation_pending")));
assertThat(startFlowResponse.getDeviceAccount().getOtpSecret(), is(equalTo("THE_OTP_SECRET")));
assertThat(startFlowResponse.getDeviceAccount().getRecoveryCode(), is(equalTo("THE_RECOVERY_CODE")));
}
示例14: requestBodyRetransmittedOnClientRequestTimeout
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void requestBodyRetransmittedOnClientRequestTimeout() throws Exception {
enqueueRequestTimeoutResponses();
Response response = client.newCall(new Request.Builder()
.url(server.url("/"))
.post(RequestBody.create(MediaType.parse("text/plain"), "Hello"))
.build()).execute();
assertEquals("Body", response.body().string());
RecordedRequest request1 = server.takeRequest();
assertEquals("Hello", request1.getBody().readUtf8());
RecordedRequest request2 = server.takeRequest();
assertEquals("Hello", request2.getBody().readUtf8());
}
示例15: propfindRedirectsToPropfindAndMaintainsRequestBody
import okhttp3.mockwebserver.RecordedRequest; //导入依赖的package包/类
@Test public void propfindRedirectsToPropfindAndMaintainsRequestBody() throws Exception {
// given
server.enqueue(new MockResponse()
.setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: /page2")
.setBody("This page has moved!"));
server.enqueue(new MockResponse().setBody("Page 2"));
// when
Response response = client.newCall(new Request.Builder()
.url(server.url("/page1"))
.method("PROPFIND", RequestBody.create(MediaType.parse("text/plain"), "Request Body"))
.build()).execute();
// then
assertEquals("Page 2", response.body().string());
RecordedRequest page1 = server.takeRequest();
assertEquals("PROPFIND /page1 HTTP/1.1", page1.getRequestLine());
assertEquals("Request Body", page1.getBody().readUtf8());
RecordedRequest page2 = server.takeRequest();
assertEquals("PROPFIND /page2 HTTP/1.1", page2.getRequestLine());
assertEquals("Request Body", page2.getBody().readUtf8());
}