本文整理汇总了Java中com.google.mockwebserver.MockResponse类的典型用法代码示例。如果您正苦于以下问题:Java MockResponse类的具体用法?Java MockResponse怎么用?Java MockResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MockResponse类属于com.google.mockwebserver包,在下文中一共展示了MockResponse类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: testExtremelyLarge
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Suppress
public void testExtremelyLarge() throws Exception {
// NOTE: suppressed since this takes several minutes to run
final long length = 3 * GB_IN_BYTES;
final InputStream body = new FakeInputStream(length);
enqueueResponse(new MockResponse().setResponseCode(HTTP_OK).setBody(body, length)
.setHeader("Content-type", "text/plain")
.setSocketPolicy(SocketPolicy.DISCONNECT_AT_END));
final Download download = enqueueRequest(getRequest()
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "extreme.bin"));
download.runUntilStatus(DownloadManager.STATUS_SUCCESSFUL, 10 * DateUtils.MINUTE_IN_MILLIS);
assertEquals(length, download.getLongField(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
assertEquals(length, download.getLongField(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
}
开发者ID:wuyexiong,项目名称:android-downloadprovider,代码行数:18,代码来源:PublicApiFunctionalTest.java
示例2: responsesAreCached
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void responsesAreCached() throws IOException {
client.setResponseCache(cache);
server.enqueue(new MockResponse().addHeader("cache-control: max-age=60").setBody("A"));
server.play();
assertContent("A", client.open(server.getUrl("/")), Integer.MAX_VALUE);
assertEquals(1, cache.getRequestCount());
assertEquals(1, cache.getNetworkCount());
assertEquals(0, cache.getHitCount());
assertContent("A", client.open(server.getUrl("/")), Integer.MAX_VALUE);
assertContent("A", client.open(server.getUrl("/")), Integer.MAX_VALUE);
assertEquals(3, cache.getRequestCount());
assertEquals(1, cache.getNetworkCount());
assertEquals(2, cache.getHitCount());
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:17,代码来源:HttpOverSpdyTest.java
示例3: redirectedFromHttpsToHttpFollowingProtocolRedirects
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void redirectedFromHttpsToHttpFollowingProtocolRedirects() throws Exception {
server2 = new MockWebServer();
server2.enqueue(new MockResponse().setBody("This is insecure HTTP!"));
server2.play();
server.useHttps(sslContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
.addHeader("Location: " + server2.getUrl("/"))
.setBody("This page has moved!"));
server.play();
client.setSslSocketFactory(sslContext.getSocketFactory());
client.setHostnameVerifier(new RecordingHostnameVerifier());
client.setFollowProtocolRedirects(true);
HttpsURLConnection connection = (HttpsURLConnection) client.open(server.getUrl("/"));
assertContent("This is insecure HTTP!", connection);
assertNull(connection.getCipherSuite());
assertNull(connection.getLocalCertificates());
assertNull(connection.getServerCertificates());
assertNull(connection.getPeerPrincipal());
assertNull(connection.getLocalPrincipal());
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:23,代码来源:URLConnectionTest.java
示例4: connectionCloseInRequest
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void connectionCloseInRequest() throws IOException, InterruptedException {
server.enqueue(new MockResponse()); // server doesn't honor the connection: close header!
server.enqueue(new MockResponse());
server.play();
HttpURLConnection a = client.open(server.getUrl("/"));
a.setRequestProperty("Connection", "close");
assertEquals(200, a.getResponseCode());
HttpURLConnection b = client.open(server.getUrl("/"));
assertEquals(200, b.getResponseCode());
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals("When connection: close is used, each request should get its own connection", 0,
server.takeRequest().getSequenceNumber());
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:17,代码来源:URLConnectionTest.java
示例5: getHeadersRetainsCached200LevelWarnings
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void getHeadersRetainsCached200LevelWarnings() throws Exception {
server.enqueue(new MockResponse().addHeader("Warning: 299 test danger")
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Cache-Control: max-age=0")
.setBody("A"));
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
server.play();
URLConnection connection1 = openConnection(server.getUrl("/"));
assertEquals("A", readAscii(connection1));
assertEquals("299 test danger", connection1.getHeaderField("Warning"));
URLConnection connection2 = openConnection(server.getUrl("/"));
assertEquals("A", readAscii(connection2));
assertEquals("299 test danger", connection2.getHeaderField("Warning"));
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:17,代码来源:HttpResponseCacheTest.java
示例6: testImagePush
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test
public void testImagePush() throws IOException, InterruptedException {
final String json = Resources.toString(getResource("image/image-push.json"), Charsets.UTF_8);
final Auth auth = new Auth("hannibal", "xxxx", "[email protected]");
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody(json));
server.play();
Docker docker = createDockerForMock(server);
Progress response = docker.imagePush("base", "reg", auth);
assertNotNull(response);
assertEquals(response.getError(), "Invalid...");
RecordedRequest recordedRequest = server.takeRequest();
String body = new String(recordedRequest.getBody());
Auth requestAuth = new Gson().fromJson(body, Auth.class);
assertEquals(auth.getEmail(), requestAuth.getEmail());
}
开发者ID:jboss-fuse,项目名称:fuse-docker,代码行数:17,代码来源:ImageTest.java
示例7: setChunkedStreamingMode
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
/**
* Obnoxiously test that the chunk sizes transmitted exactly equal the
* requested data+chunk header size. Although setChunkedStreamingMode()
* isn't specific about whether the size applies to the data or the
* complete chunk, the RI interprets it as a complete chunk.
*/
@Test public void setChunkedStreamingMode() throws IOException, InterruptedException {
server.enqueue(new MockResponse());
server.play();
HttpURLConnection urlConnection = client.open(server.getUrl("/"));
urlConnection.setChunkedStreamingMode(8);
urlConnection.setDoOutput(true);
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write("ABCDEFGHIJKLMNOPQ".getBytes("US-ASCII"));
assertEquals(200, urlConnection.getResponseCode());
RecordedRequest request = server.takeRequest();
assertEquals("ABCDEFGHIJKLMNOPQ", new String(request.getBody(), "US-ASCII"));
assertEquals(Arrays.asList(3, 3, 3, 3, 3, 2), request.getChunkSizes());
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:22,代码来源:URLConnectionTest.java
示例8: testGetDataString
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test
public void testGetDataString() throws InterruptedException {
// Define WFS mock responses
mockServer.enqueue(new MockResponse().clearHeaders().setBody(
createStubGetCapabilitiesResponse()));
mockServer.enqueue(new MockResponse().clearHeaders().setBody(
createStubGetFeatureResponse()));
classUnderTest.init(createStubContext(), createStubProperties());
Iterator<Map<String, Object>> result = classUnderTest
.getData("bestuurlijkegrenzen:provincies_2012");
assertThat("Result should not be NULL.", result, notNullValue());
Map<String, Object> transformedFeature = result.next();
assertThat("Result should contain at least 1 feature.",
transformedFeature, notNullValue());
assertThat("Feature should contain 3 properties", transformedFeature
.entrySet().size(), is(3));
}
开发者ID:Geodan,项目名称:solr-dataimporthandler-wfs,代码行数:21,代码来源:WFSDataSourceTest.java
示例9: authenticate
import com.google.mockwebserver.MockResponse; //导入依赖的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!"));
server.play();
Authenticator.setDefault(new RecordingAuthenticator());
HttpURLConnection connection = client.open(server.getUrl("/"));
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest denied = server.takeRequest();
assertContainsNoneMatching(denied.getHeaders(), "authorization: Basic .*");
RecordedRequest accepted = server.takeRequest();
assertEquals("GET / HTTP/1.1", accepted.getRequestLine());
assertContains(accepted.getHeaders(),
"authorization: Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS);
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:19,代码来源:HttpOverSpdyTest.java
示例10: clientConfiguredGzipContentEncoding
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void clientConfiguredGzipContentEncoding() throws Exception {
byte[] bodyBytes = gzip("ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes("UTF-8"));
server.enqueue(new MockResponse()
.setBody(bodyBytes)
.addHeader("Content-Encoding: gzip"));
server.play();
URLConnection connection = client.open(server.getUrl("/"));
connection.addRequestProperty("Accept-Encoding", "gzip");
InputStream gunzippedIn = new GZIPInputStream(connection.getInputStream());
assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", readAscii(gunzippedIn, Integer.MAX_VALUE));
assertEquals(bodyBytes.length, connection.getContentLength());
RecordedRequest request = server.takeRequest();
assertContains(request.getHeaders(), "Accept-Encoding: gzip");
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:17,代码来源:URLConnectionTest.java
示例11: redirectToCachedResult
import com.google.mockwebserver.MockResponse; //导入依赖的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"));
server.play();
assertEquals("ABC", readAscii(openConnection(server.getUrl("/foo"))));
RecordedRequest request1 = server.takeRequest();
assertEquals("GET /foo HTTP/1.1", request1.getRequestLine());
assertEquals(0, request1.getSequenceNumber());
assertEquals("ABC", readAscii(openConnection(server.getUrl("/bar"))));
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.getUrl("/baz"))));
RecordedRequest request3 = server.takeRequest();
assertEquals("GET /baz HTTP/1.1", request3.getRequestLine());
assertEquals(2, request3.getSequenceNumber());
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:24,代码来源:HttpResponseCacheTest.java
示例12: testServerPrematureDisconnect
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
private void testServerPrematureDisconnect(TransferKind transferKind) throws IOException {
MockResponse response = new MockResponse();
transferKind.setBody(response, "ABCDE\nFGHIJKLMNOPQRSTUVWXYZ", 16);
server.enqueue(truncateViolently(response, 16));
server.enqueue(new MockResponse().setBody("Request #2"));
server.play();
BufferedReader reader = new BufferedReader(
new InputStreamReader(openConnection(server.getUrl("/")).getInputStream()));
assertEquals("ABCDE", reader.readLine());
try {
reader.readLine();
fail("This implementation silently ignored a truncated HTTP body.");
} catch (IOException expected) {
} finally {
reader.close();
}
assertEquals(1, cache.getWriteAbortCount());
assertEquals(0, cache.getWriteSuccessCount());
URLConnection connection = openConnection(server.getUrl("/"));
assertEquals("Request #2", readAscii(connection));
assertEquals(1, cache.getWriteAbortCount());
assertEquals(1, cache.getWriteSuccessCount());
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:26,代码来源:HttpResponseCacheTest.java
示例13: testEarlyDisconnectDoesntHarmPooling
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
private void testEarlyDisconnectDoesntHarmPooling(TransferKind transferKind) throws Exception {
MockResponse response1 = new MockResponse();
transferKind.setBody(response1, "ABCDEFGHIJK", 1024);
server.enqueue(response1);
MockResponse response2 = new MockResponse();
transferKind.setBody(response2, "LMNOPQRSTUV", 1024);
server.enqueue(response2);
server.play();
URLConnection connection1 = client.open(server.getUrl("/"));
InputStream in1 = connection1.getInputStream();
assertEquals("ABCDE", readAscii(in1, 5));
in1.close();
HttpURLConnection connection2 = client.open(server.getUrl("/"));
InputStream in2 = connection2.getInputStream();
assertEquals("LMNOP", readAscii(in2, 5));
in2.close();
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber()); // Connection is pooled!
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:25,代码来源:URLConnectionTest.java
示例14: testServerClosesOutput
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
private void testServerClosesOutput(SocketPolicy socketPolicy) throws Exception {
server.enqueue(new MockResponse().setBody("This connection won't pool properly")
.setSocketPolicy(socketPolicy));
MockResponse responseAfter = new MockResponse().setBody("This comes after a busted connection");
server.enqueue(responseAfter);
server.enqueue(responseAfter); // Enqueue 2x because the broken connection may be reused.
server.play();
HttpURLConnection connection1 = client.open(server.getUrl("/a"));
connection1.setReadTimeout(100);
assertContent("This connection won't pool properly", connection1);
assertEquals(0, server.takeRequest().getSequenceNumber());
HttpURLConnection connection2 = client.open(server.getUrl("/b"));
connection2.setReadTimeout(100);
assertContent("This comes after a busted connection", connection2);
// Check that a fresh connection was created, either immediately or after attempting reuse.
RecordedRequest requestAfter = server.takeRequest();
if (server.getRequestCount() == 3) {
requestAfter = server.takeRequest(); // The failure consumed a response.
}
// sequence number 0 means the HTTP socket connection was not reused
assertEquals(0, requestAfter.getSequenceNumber());
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:25,代码来源:URLConnectionTest.java
示例15: post
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void post() throws Exception {
server.enqueue(new MockResponse().setBody("abc"));
server.play();
Request request = new Request.Builder(server.getUrl("/"))
.post(Request.Body.create(MediaType.parse("text/plain"), "def"))
.build();
client.enqueue(request, receiver);
receiver.await(request)
.assertCode(200)
.assertBody("abc");
RecordedRequest recordedRequest = server.takeRequest();
assertEquals("def", recordedRequest.getUtf8Body());
assertEquals("3", recordedRequest.getHeader("Content-Length"));
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:18,代码来源:AsyncApiTest.java
示例16: getKeepAlive
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void getKeepAlive() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("ABC"));
server.play();
// The request should work once and then fail
URLConnection connection1 = client.open(server.getUrl(""));
connection1.setReadTimeout(100);
InputStream input = connection1.getInputStream();
assertEquals("ABC", readAscii(input, Integer.MAX_VALUE));
input.close();
server.shutdown();
try {
HttpURLConnection connection2 = client.open(server.getUrl(""));
connection2.setReadTimeout(100);
connection2.getInputStream();
fail();
} catch (ConnectException expected) {
}
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:21,代码来源:URLConnectionTest.java
示例17: testMethodInvalidates
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
private void testMethodInvalidates(String requestMethod) throws Exception {
// 1. seed the cache
// 2. invalidate it
// 3. expect a cache miss
server.enqueue(
new MockResponse().setBody("A").addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
server.enqueue(new MockResponse().setBody("B"));
server.enqueue(new MockResponse().setBody("C"));
server.play();
URL url = server.getUrl("/");
assertEquals("A", readAscii(openConnection(url)));
HttpURLConnection invalidate = openConnection(url);
invalidate.setRequestMethod(requestMethod);
addRequestBodyIfNecessary(requestMethod, invalidate);
assertEquals("B", readAscii(invalidate));
assertEquals("C", readAscii(openConnection(url)));
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:22,代码来源:HttpResponseCacheTest.java
示例18: customAuthenticator
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void customAuthenticator() throws Exception {
MockResponse pleaseAuthenticate = new MockResponse().setResponseCode(401)
.addHeader("WWW-Authenticate: Basic realm=\"protected area\"")
.setBody("Please authenticate.");
server.enqueue(pleaseAuthenticate);
server.enqueue(new MockResponse().setBody("A"));
server.play();
Credential credential = Credential.basic("jesse", "peanutbutter");
RecordingOkAuthenticator authenticator = new RecordingOkAuthenticator(credential);
client.setAuthenticator(authenticator);
assertContent("A", client.open(server.getUrl("/private")));
assertContainsNoneMatching(server.takeRequest().getHeaders(), "Authorization: .*");
assertContains(server.takeRequest().getHeaders(),
"Authorization: " + credential.getHeaderValue());
assertEquals(1, authenticator.calls.size());
String call = authenticator.calls.get(0);
assertTrue(call, call.contains("proxy=DIRECT"));
assertTrue(call, call.contains("url=" + server.getUrl("/private")));
assertTrue(call, call.contains("challenges=[Basic realm=\"protected area\"]"));
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:24,代码来源:URLConnectionTest.java
示例19: connectViaHttpsReusingConnections
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void connectViaHttpsReusingConnections() throws IOException, InterruptedException {
server.useHttps(sslContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setBody("this response comes via HTTPS"));
server.enqueue(new MockResponse().setBody("another response via HTTPS"));
server.play();
// The pool will only reuse sockets if the SSL socket factories are the same.
SSLSocketFactory clientSocketFactory = sslContext.getSocketFactory();
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
client.setSslSocketFactory(clientSocketFactory);
client.setHostnameVerifier(hostnameVerifier);
HttpURLConnection connection = client.open(server.getUrl("/"));
assertContent("this response comes via HTTPS", connection);
connection = client.open(server.getUrl("/"));
assertContent("another response via HTTPS", connection);
assertEquals(0, server.takeRequest().getSequenceNumber());
assertEquals(1, server.takeRequest().getSequenceNumber());
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:22,代码来源:URLConnectionTest.java
示例20: varyAndHttps
import com.google.mockwebserver.MockResponse; //导入依赖的package包/类
@Test public void varyAndHttps() throws Exception {
server.useHttps(sslContext.getSocketFactory(), false);
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60")
.addHeader("Vary: Accept-Language")
.setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
server.play();
URL url = server.getUrl("/");
HttpsURLConnection connection1 = (HttpsURLConnection) client.open(url);
connection1.setSSLSocketFactory(sslContext.getSocketFactory());
connection1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
connection1.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection1));
HttpsURLConnection connection2 = (HttpsURLConnection) client.open(url);
connection2.setSSLSocketFactory(sslContext.getSocketFactory());
connection2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
connection2.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection2));
}
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:22,代码来源:HttpResponseCacheTest.java
注:本文中的com.google.mockwebserver.MockResponse类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论