當前位置: 首頁>>代碼示例>>Java>>正文


Java MockResponse.addHeader方法代碼示例

本文整理匯總了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) {
  }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:22,代碼來源:URLConnectionTest.java

示例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;
}
 
開發者ID:hortonworks,項目名稱:nifi-android-s2s,代碼行數:25,代碼來源:MockNiFiS2SServer.java

示例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
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:48,代碼來源:ResponseCacheTest.java

示例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
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:51,代碼來源:CacheTest.java

示例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()));
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:14,代碼來源:URLConnectionTest.java

示例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());
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:37,代碼來源:URLConnectionTest.java

示例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;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:29,代碼來源:Benchmark.java

示例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
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:49,代碼來源:UrlConnectionCacheTest.java

示例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


注:本文中的okhttp3.mockwebserver.MockResponse.addHeader方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。