本文整理汇总了Java中okhttp3.mockwebserver.MockResponse.addHeader方法的典型用法代码示例。如果您正苦于以下问题:Java MockResponse.addHeader方法的具体用法?Java MockResponse.addHeader怎么用?Java MockResponse.addHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类okhttp3.mockwebserver.MockResponse
的用法示例。
在下文中一共展示了MockResponse.addHeader方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: contentDisagreesWithChunkedHeaderBodyTooShort
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
@Test public void contentDisagreesWithChunkedHeaderBodyTooShort() throws IOException {
MockResponse mockResponse = new MockResponse();
mockResponse.setChunkedBody("abcde", 5);
Buffer truncatedBody = new Buffer();
Buffer fullBody = mockResponse.getBody();
truncatedBody.write(fullBody, fullBody.indexOf((byte) 'e'));
mockResponse.setBody(truncatedBody);
mockResponse.clearHeaders();
mockResponse.addHeader("Transfer-encoding: chunked");
mockResponse.setSocketPolicy(DISCONNECT_AT_END);
server.enqueue(mockResponse);
try {
readAscii(urlFactory.open(server.url("/").url()).getInputStream(), 5);
fail();
} catch (ProtocolException expected) {
}
}
示例2: enqueuCreateTransaction
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
public String enqueuCreateTransaction(String portIdentifier, String transactionIdentifier, Object ttl, boolean addLocationHeader) {
final String transactionsPath = "/nifi-api/data-transfer/input-ports/" + portIdentifier + "/transactions";
String transactionPath = transactionsPath + "/" + transactionIdentifier;
MockResponse mockResponse = new MockResponse();
if (transactionIdentifier != null) {
mockResponse = mockResponse.addHeader(LOCATION_URI_INTENT_NAME, LOCATION_URI_INTENT_VALUE);
}
if (addLocationHeader) {
mockResponse = mockResponse.addHeader(LOCATION_HEADER_NAME, mockWebServer.url(transactionPath));
}
if (ttl != null) {
mockResponse = mockResponse.addHeader(SERVER_SIDE_TRANSACTION_TTL, ttl.toString());
}
enqueue(mockResponse, new RequestAssertion() {
@Override
public RecordedRequest check() throws Exception {
RecordedRequest recordedRequest = mockWebServer.takeRequest();
assertEquals(transactionsPath, recordedRequest.getPath());
assertEquals(HttpMethod.POST.name(), recordedRequest.getMethod());
return recordedRequest;
}
});
return transactionPath;
}
示例3: assertCached
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse mockResponse = new MockResponse()
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setResponseCode(responseCode)
.setBody("ABCDE")
.addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT
|| responseCode == HttpURLConnection.HTTP_RESET) {
mockResponse.setBody(""); // We forbid bodies for 204 and 205.
}
server.enqueue(mockResponse);
if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
// 408's are a bit of an outlier because we may repeat the request if we encounter this
// response code. In this scenario, there are 2 responses: the initial 408 and then the 200
// because of the retry. We just want to ensure the initial 408 isn't cached.
expectedResponseCode = 200;
server.enqueue(new MockResponse()
.setHeader("Cache-Control", "no-store")
.setBody("FGHIJ"));
}
server.start();
URL url = server.url("/").url();
HttpURLConnection connection = openConnection(url);
assertEquals(expectedResponseCode, connection.getResponseCode());
// Exhaust the content stream.
readAscii(connection);
CacheResponse cached = cache.get(url.toURI(), "GET", null);
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
} else {
assertNull(Integer.toString(responseCode), cached);
}
server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers
}
示例4: assertCached
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse mockResponse = new MockResponse()
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setResponseCode(responseCode)
.setBody("ABCDE")
.addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT
|| responseCode == HttpURLConnection.HTTP_RESET) {
mockResponse.setBody(""); // We forbid bodies for 204 and 205.
}
server.enqueue(mockResponse);
if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
// 408's are a bit of an outlier because we may repeat the request if we encounter this
// response code. In this scenario, there are 2 responses: the initial 408 and then the 200
// because of the retry. We just want to ensure the initial 408 isn't cached.
expectedResponseCode = 200;
server.enqueue(new MockResponse()
.setHeader("Cache-Control", "no-store")
.setBody("FGHIJ"));
}
server.start();
Request request = new Request.Builder()
.url(server.url("/"))
.build();
Response response = client.newCall(request).execute();
assertEquals(expectedResponseCode, response.code());
// Exhaust the content stream.
response.body().string();
Response cached = cache.get(request);
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
cached.body().close();
} else {
assertNull(Integer.toString(responseCode), cached);
}
server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers
}
示例5: contentDisagreesWithChunkedHeaderBodyTooLong
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
@Test public void contentDisagreesWithChunkedHeaderBodyTooLong() throws IOException {
MockResponse mockResponse = new MockResponse();
mockResponse.setChunkedBody("abc", 3);
Buffer buffer = mockResponse.getBody();
buffer.writeUtf8("\r\nYOU SHOULD NOT SEE THIS");
mockResponse.setBody(buffer);
mockResponse.clearHeaders();
mockResponse.addHeader("Transfer-encoding: chunked");
server.enqueue(mockResponse);
assertContent("abc", urlFactory.open(server.url("/").url()));
}
示例6: testClientConfiguredGzipContentEncodingAndConnectionReuse
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
/**
* Test a bug where gzip input streams weren't exhausting the input stream, which corrupted the
* request that followed or prevented connection reuse.
* http://code.google.com/p/android/issues/detail?id=7059
* http://code.google.com/p/android/issues/detail?id=38817
*/
private void testClientConfiguredGzipContentEncodingAndConnectionReuse(TransferKind transferKind,
boolean tls) throws Exception {
if (tls) {
SSLSocketFactory socketFactory = sslClient.socketFactory;
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
server.useHttps(socketFactory, false);
urlFactory.setClient(urlFactory.client().newBuilder()
.sslSocketFactory(socketFactory, sslClient.trustManager)
.hostnameVerifier(hostnameVerifier)
.build());
}
MockResponse responseOne = new MockResponse();
responseOne.addHeader("Content-Encoding: gzip");
transferKind.setBody(responseOne, gzip("one (gzipped)"), 5);
server.enqueue(responseOne);
MockResponse responseTwo = new MockResponse();
transferKind.setBody(responseTwo, "two (identity)", 5);
server.enqueue(responseTwo);
HttpURLConnection connection1 = urlFactory.open(server.url("/").url());
connection1.addRequestProperty("Accept-Encoding", "gzip");
InputStream gunzippedIn = new GZIPInputStream(connection1.getInputStream());
assertEquals("one (gzipped)", readAscii(gunzippedIn, Integer.MAX_VALUE));
assertEquals(0, server.takeRequest().getSequenceNumber());
HttpURLConnection connection2 = urlFactory.open(server.url("/").url());
assertEquals("two (identity)", readAscii(connection2.getInputStream(), Integer.MAX_VALUE));
assertEquals(1, server.takeRequest().getSequenceNumber());
}
示例7: newResponse
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
private MockResponse newResponse() throws IOException {
byte[] bytes = new byte[bodyByteCount];
random.nextBytes(bytes);
Buffer body = new Buffer().write(bytes);
MockResponse result = new MockResponse();
if (gzip) {
Buffer gzipBody = new Buffer();
GzipSink gzipSink = new GzipSink(gzipBody);
gzipSink.write(body, body.size());
gzipSink.close();
body = gzipBody;
result.addHeader("Content-Encoding: gzip");
}
if (chunked) {
result.setChunkedBody(body, 1024);
} else {
result.setBody(body);
}
for (int i = 0; i < headerCount; i++) {
result.addHeader(randomString(12), randomString(20));
}
return result;
}
示例8: assertCached
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse response = new MockResponse()
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setResponseCode(responseCode)
.setBody("ABCDE")
.addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
response.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
response.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT
|| responseCode == HttpURLConnection.HTTP_RESET) {
response.setBody(""); // We forbid bodies for 204 and 205.
}
server.enqueue(response);
if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
// 408's are a bit of an outlier because we may repeat the request if we encounter this
// response code. In this scenario, there are 2 responses: the initial 408 and then the 200
// because of the retry. We just want to ensure the initial 408 isn't cached.
expectedResponseCode = 200;
server.enqueue(new MockResponse()
.addHeader("Cache-Control", "no-store")
.setBody("FGHIJ"));
}
server.start();
URL url = server.url("/").url();
HttpURLConnection conn = urlFactory.open(url);
assertEquals(expectedResponseCode, conn.getResponseCode());
// exhaust the content stream
readAscii(conn);
Response cached = cache.get(new Request.Builder().url(url).build());
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
cached.body().close();
} else {
assertNull(Integer.toString(responseCode), cached);
}
server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers
}
示例9: enqueueResponse
import okhttp3.mockwebserver.MockResponse; //导入方法依赖的package包/类
private void enqueueResponse(String fileName, Map<String, String> headers) throws IOException {
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream("api-response/" + fileName);
BufferedSource source = Okio.buffer(Okio.source(inputStream));
MockResponse mockResponse = new MockResponse();
for (Map.Entry<String, String> header : headers.entrySet()) {
mockResponse.addHeader(header.getKey(), header.getValue());
}
mockWebServer.enqueue(mockResponse
.setBody(source.readString(StandardCharsets.UTF_8)));
}
开发者ID:hbmartin,项目名称:firebase-chat-android-architecture-components,代码行数:12,代码来源:GithubServiceTest.java