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


Java MockWebServer.play方法代碼示例

本文整理匯總了Java中com.squareup.okhttp.mockwebserver.MockWebServer.play方法的典型用法代碼示例。如果您正苦於以下問題:Java MockWebServer.play方法的具體用法?Java MockWebServer.play怎麽用?Java MockWebServer.play使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.squareup.okhttp.mockwebserver.MockWebServer的用法示例。


在下文中一共展示了MockWebServer.play方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testSendingCookiesFromStore

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testSendingCookiesFromStore() throws Exception {
  MockWebServer server = new MockWebServer();
  server.enqueue(new MockResponse());
  server.play();

  CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  HttpCookie cookieA = new HttpCookie("a", "android");
  cookieA.setDomain(server.getCookieDomain());
  cookieA.setPath("/");
  cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieA);
  HttpCookie cookieB = new HttpCookie("b", "banana");
  cookieB.setDomain(server.getCookieDomain());
  cookieB.setPath("/");
  cookieManager.getCookieStore().add(server.getUrl("/").toURI(), cookieB);
  CookieHandler.setDefault(cookieManager);

  get(server, "/");
  RecordedRequest request = server.takeRequest();

  List<String> receivedHeaders = request.getHeaders();
  assertContains(receivedHeaders, "Cookie: $Version=\"1\"; "
      + "a=\"android\";$Path=\"/\";$Domain=\"" + server.getCookieDomain() + "\"; "
      + "b=\"banana\";$Path=\"/\";$Domain=\"" + server.getCookieDomain() + "\"");
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:25,代碼來源:CookiesTest.java

示例2: testCookiesSentIgnoresCase

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testCookiesSentIgnoresCase() throws Exception {
  CookieHandler.setDefault(new CookieManager() {
    @Override public Map<String, List<String>> get(URI uri,
        Map<String, List<String>> requestHeaders) throws IOException {
      Map<String, List<String>> result = new HashMap<String, List<String>>();
      result.put("COOKIE", Collections.singletonList("Bar=bar"));
      result.put("cooKIE2", Collections.singletonList("Baz=baz"));
      return result;
    }
  });
  MockWebServer server = new MockWebServer();
  server. enqueue(new MockResponse());
  server.play();

  get(server, "/");

  RecordedRequest request = server.takeRequest();
  assertContainsAll(request.getHeaders(), "COOKIE: Bar=bar", "cooKIE2: Baz=baz");
  assertFalse(request.getHeaders().contains("Quux: quux"));
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:21,代碼來源:CookiesTest.java

示例3: redirectedFromHttpsToHttpFollowingProtocolRedirects

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的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:xin3liang,項目名稱:platform_external_okhttp,代碼行數:23,代碼來源:URLConnectionTest.java

示例4: redirectedFromHttpToHttpsFollowingProtocolRedirects

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void redirectedFromHttpToHttpsFollowingProtocolRedirects() throws Exception {
  server2 = new MockWebServer();
  server2.useHttps(sslContext.getSocketFactory(), false);
  server2.enqueue(new MockResponse().setBody("This is secure HTTPS!"));
  server2.play();

  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);
  connection = client.open(server.getUrl("/"));
  assertContent("This is secure HTTPS!", connection);
  assertFalse(connection instanceof HttpsURLConnection);
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:19,代碼來源:URLConnectionTest.java

示例5: getKeepAlive

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的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
  HttpURLConnection connection1 = client.open(server.getUrl(""));
  connection1.setReadTimeout(100);
  InputStream input = connection1.getInputStream();
  assertEquals("ABC", readAscii(input, Integer.MAX_VALUE));
  server.shutdown();
  try {
    HttpURLConnection connection2 = client.open(server.getUrl(""));
    connection2.setReadTimeout(100);
    connection2.getInputStream();
    fail();
  } catch (ConnectException expected) {
  }
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:20,代碼來源:URLConnectionTest.java

示例6: startServer

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
private MockWebServer startServer() throws IOException {
  Logger.getLogger(MockWebServer.class.getName()).setLevel(Level.WARNING);
  MockWebServer server = new MockWebServer();

  if (tls) {
    SSLContext sslContext = SslContextBuilder.localhost();
    server.useHttps(sslContext.getSocketFactory(), false);
    server.setNpnEnabled(true);
    server.setNpnProtocols(protocols);
  }

  final MockResponse response = newResponse();
  server.setDispatcher(new Dispatcher() {
    @Override public MockResponse dispatch(RecordedRequest request) {
      return response;
    }
  });

  server.play();
  return server;
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:22,代碼來源:Benchmark.java

示例7: start

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@BeforeMethod
public void start() throws IOException {
   server = new MockWebServer();
   server.play();
   ApiContext<DimensionDataCloudControllerApi> ctx = ContextBuilder.newBuilder("dimensiondata-cloudcontroller")
           .credentials("", "")
           .endpoint(url(""))
           //.modules(modules)
           .overrides(overrides())
           .build();
   json = ctx.utils().injector().getInstance(Json.class);
   api = ctx.getApi();
   server.enqueue(xmlResponse("/account.xml"));
}
 
開發者ID:cloudsoft,項目名稱:amp-dimensiondata,代碼行數:15,代碼來源:BaseDimensionDataCloudControllerMockTest.java

示例8: setup

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Before
public void setup() throws Exception {
    mMainActivity = Robolectric.buildActivity(MainActivity.class).create().start().resume().visible().get();

    mServer = new MockWebServer();
    mServer.play();

}
 
開發者ID:mahdihijazi,項目名稱:interview_tasks,代碼行數:9,代碼來源:AsyncTasksTest.java

示例9: given_mock_web_server_is_started

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@BeforeClass public static void
given_mock_web_server_is_started() throws IOException {
    mockWebServer = new MockWebServer();
    InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("hystrix_circuit_breaker_opened.txt");
    String hystrixEvent = new Scanner(resourceAsStream, UTF_8.name()).useDelimiter("\\A").next();
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(hystrixEvent));
    mockWebServer.play(6543);
}
 
開發者ID:johanhaleby,項目名稱:hystrix-monitor,代碼行數:9,代碼來源:HystrixMonitorTest.java

示例10: testNetscapeResponse

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test
public void testNetscapeResponse() throws Exception {
  CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  CookieHandler.setDefault(cookieManager);
  MockWebServer server = new MockWebServer();
  server.play();

  server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
      + "expires=Fri, 31-Dec-9999 23:59:59 GMT; "
      + "path=/path; "
      + "domain=" + server.getCookieDomain() + "; "
      + "secure"));
  get(server, "/path/foo");

  List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
  assertEquals(1, cookies.size());
  HttpCookie cookie = cookies.get(0);
  assertEquals("a", cookie.getName());
  assertEquals("android", cookie.getValue());
  assertEquals(null, cookie.getComment());
  assertEquals(null, cookie.getCommentURL());
  assertEquals(false, cookie.getDiscard());
  assertEquals(server.getCookieDomain(), cookie.getDomain());
  assertTrue(cookie.getMaxAge() > 100000000000L);
  assertEquals("/path", cookie.getPath());
  assertEquals(true, cookie.getSecure());
  assertEquals(0, cookie.getVersion());
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:29,代碼來源:CookiesTest.java

示例11: testRfc2109Response

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testRfc2109Response() throws Exception {
  CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  CookieHandler.setDefault(cookieManager);
  MockWebServer server = new MockWebServer();
  server.play();

  server.enqueue(new MockResponse().addHeader("Set-Cookie: a=android; "
      + "Comment=this cookie is delicious; "
      + "Domain=" + server.getCookieDomain() + "; "
      + "Max-Age=60; "
      + "Path=/path; "
      + "Secure; "
      + "Version=1"));
  get(server, "/path/foo");

  List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
  assertEquals(1, cookies.size());
  HttpCookie cookie = cookies.get(0);
  assertEquals("a", cookie.getName());
  assertEquals("android", cookie.getValue());
  assertEquals("this cookie is delicious", cookie.getComment());
  assertEquals(null, cookie.getCommentURL());
  assertEquals(false, cookie.getDiscard());
  assertEquals(server.getCookieDomain(), cookie.getDomain());
  assertEquals(60, cookie.getMaxAge());
  assertEquals("/path", cookie.getPath());
  assertEquals(true, cookie.getSecure());
  assertEquals(1, cookie.getVersion());
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:30,代碼來源:CookiesTest.java

示例12: testRfc2965Response

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testRfc2965Response() throws Exception {
  CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  CookieHandler.setDefault(cookieManager);
  MockWebServer server = new MockWebServer();
  server.play();

  server.enqueue(new MockResponse().addHeader("Set-Cookie2: a=android; "
      + "Comment=this cookie is delicious; "
      + "CommentURL=http://google.com/; "
      + "Discard; "
      + "Domain=" + server.getCookieDomain() + "; "
      + "Max-Age=60; "
      + "Path=/path; "
      + "Port=\"80,443," + server.getPort() + "\"; "
      + "Secure; "
      + "Version=1"));
  get(server, "/path/foo");

  List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
  assertEquals(1, cookies.size());
  HttpCookie cookie = cookies.get(0);
  assertEquals("a", cookie.getName());
  assertEquals("android", cookie.getValue());
  assertEquals("this cookie is delicious", cookie.getComment());
  assertEquals("http://google.com/", cookie.getCommentURL());
  assertEquals(true, cookie.getDiscard());
  assertEquals(server.getCookieDomain(), cookie.getDomain());
  assertEquals(60, cookie.getMaxAge());
  assertEquals("/path", cookie.getPath());
  assertEquals("80,443," + server.getPort(), cookie.getPortlist());
  assertEquals(true, cookie.getSecure());
  assertEquals(1, cookie.getVersion());
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:34,代碼來源:CookiesTest.java

示例13: testQuotedAttributeValues

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testQuotedAttributeValues() throws Exception {
  CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  CookieHandler.setDefault(cookieManager);
  MockWebServer server = new MockWebServer();
  server.play();

  server.enqueue(new MockResponse().addHeader("Set-Cookie2: a=\"android\"; "
      + "Comment=\"this cookie is delicious\"; "
      + "CommentURL=\"http://google.com/\"; "
      + "Discard; "
      + "Domain=\"" + server.getCookieDomain() + "\"; "
      + "Max-Age=\"60\"; "
      + "Path=\"/path\"; "
      + "Port=\"80,443," + server.getPort() + "\"; "
      + "Secure; "
      + "Version=\"1\""));
  get(server, "/path/foo");

  List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
  assertEquals(1, cookies.size());
  HttpCookie cookie = cookies.get(0);
  assertEquals("a", cookie.getName());
  assertEquals("android", cookie.getValue());
  assertEquals("this cookie is delicious", cookie.getComment());
  assertEquals("http://google.com/", cookie.getCommentURL());
  assertEquals(true, cookie.getDiscard());
  assertEquals(server.getCookieDomain(), cookie.getDomain());
  assertEquals(60, cookie.getMaxAge());
  assertEquals("/path", cookie.getPath());
  assertEquals("80,443," + server.getPort(), cookie.getPortlist());
  assertEquals(true, cookie.getSecure());
  assertEquals(1, cookie.getVersion());
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:34,代碼來源:CookiesTest.java

示例14: testRedirectsDoNotIncludeTooManyCookies

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Test public void testRedirectsDoNotIncludeTooManyCookies() throws Exception {
  MockWebServer redirectTarget = new MockWebServer();
  redirectTarget.enqueue(new MockResponse().setBody("A"));
  redirectTarget.play();

  MockWebServer redirectSource = new MockWebServer();
  redirectSource.enqueue(new MockResponse()
      .setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP)
      .addHeader("Location: " + redirectTarget.getUrl("/")));
  redirectSource.play();

  CookieManager cookieManager = new CookieManager(null, ACCEPT_ORIGINAL_SERVER);
  HttpCookie cookie = new HttpCookie("c", "cookie");
  cookie.setDomain(redirectSource.getCookieDomain());
  cookie.setPath("/");
  String portList = Integer.toString(redirectSource.getPort());
  cookie.setPortlist(portList);
  cookieManager.getCookieStore().add(redirectSource.getUrl("/").toURI(), cookie);
  CookieHandler.setDefault(cookieManager);

  get(redirectSource, "/");
  RecordedRequest request = redirectSource.takeRequest();

  assertContains(request.getHeaders(), "Cookie: $Version=\"1\"; "
      + "c=\"cookie\";$Path=\"/\";$Domain=\"" + redirectSource.getCookieDomain()
      + "\";$Port=\"" + portList + "\"");

  for (String header : redirectTarget.takeRequest().getHeaders()) {
    if (header.startsWith("Cookie")) {
      fail(header);
    }
  }
}
 
開發者ID:xin3liang,項目名稱:platform_external_okhttp,代碼行數:34,代碼來源:CookiesTest.java

示例15: provideMockWebServer

import com.squareup.okhttp.mockwebserver.MockWebServer; //導入方法依賴的package包/類
@Provides
@Singleton
public MockWebServer provideMockWebServer() {
    MockWebServer mockWebServer = new MockWebServer();
    try {
        mockWebServer.play();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return mockWebServer;
}
 
開發者ID:padc,項目名稱:DevConSummit,代碼行數:12,代碼來源:MockerTestModule.java


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