当前位置: 首页>>代码示例>>Java>>正文


Java MockWebServer类代码示例

本文整理汇总了Java中com.google.mockwebserver.MockWebServer的典型用法代码示例。如果您正苦于以下问题:Java MockWebServer类的具体用法?Java MockWebServer怎么用?Java MockWebServer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


MockWebServer类属于com.google.mockwebserver包,在下文中一共展示了MockWebServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setUp

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Override
protected void setUp() throws Exception {
    super.setUp();
    mMockitoHelper.setUp(getClass());

    // Since we're testing a system app, AppDataDirGuesser doesn't find our
    // cache dir, so set it explicitly.
    System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());

    final Context realContext = getContext();
    mTestContext = new TestContext(realContext);
    setupProviderAndResolver();
    mTestContext.setResolver(mResolver);
    setContext(mTestContext);
    setupService();
    // getService().mSystemFacade = mSystemFacade;
    mSystemFacade.setUp();
    assertTrue(isDatabaseEmpty()); // ensure we're not messing with real data
    mServer = new MockWebServer();
    mServer.play();
}
 
开发者ID:wuyexiong,项目名称:android-downloadprovider,代码行数:22,代码来源:AbstractDownloadProviderFunctionalTest.java

示例2: testImagePush

import com.google.mockwebserver.MockWebServer; //导入依赖的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

示例3: redirectedFromHttpsToHttpFollowingProtocolRedirects

import com.google.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:c-ong,项目名称:mirrored-okhttp,代码行数:23,代码来源:URLConnectionTest.java

示例4: redirectedFromHttpToHttpsFollowingProtocolRedirects

import com.google.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);
  HttpURLConnection connection = client.open(server.getUrl("/"));
  assertContent("This is secure HTTPS!", connection);
  assertFalse(connection instanceof HttpsURLConnection);
}
 
开发者ID:c-ong,项目名称:mirrored-okhttp,代码行数:19,代码来源:URLConnectionTest.java

示例5: getKeepAlive

import com.google.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
  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

示例6: configure

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Override
public void configure(Binder binder) {

	/** Error Handler binding */
	binder.bind(new Key<ErrorHandler<HttpResponse>>() {
	}).to(DefaultErrorHandler.class).in(Scopes.SINGLETON);

	/** MockWebServir binding */
	binder.bind(MockWebServer.class).toInstance(new MockWebServer());

}
 
开发者ID:reportportal,项目名称:client-java-rest-core,代码行数:12,代码来源:GuiceTestModule.java

示例7: setUp

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Before public void setUp() throws Exception {
  server = new MockWebServer();
  server.play();

  Activity activity = Robolectric.buildActivity(Activity.class).get();
  loader = new UrlConnectionDownloader(activity) {
    @Override protected HttpURLConnection openConnection(Uri path) throws IOException {
      return (HttpURLConnection) server.getUrl(path.toString()).openConnection();
    }
  };
}
 
开发者ID:Pixate,项目名称:picasso,代码行数:12,代码来源:UrlConnectionDownloaderTest.java

示例8: setUp

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Before public void setUp() throws Exception {
  server = new MockWebServer();
  server.play();

  Activity activity = Robolectric.buildActivity(Activity.class).get();
  loader = new OkHttpDownloader(activity) {
    @Override protected HttpURLConnection openConnection(Uri path) throws IOException {
      return (HttpURLConnection) server.getUrl(path.toString()).openConnection();
    }
  };
}
 
开发者ID:Pixate,项目名称:picasso,代码行数:12,代码来源:OkHttpDownloaderTest.java

示例9: setup

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Before
public void setup() throws IOException {
    redirectedServer = new MockWebServer();
    redirectedServer.enqueue(new MockResponse()
        .setResponseCode(200)
        .setHeader("Content-type", "text/plain")
        .setBody("OK"));       
    redirectingServer = new MockWebServer(); 
    redirectedServer.play();
    redirectingServer.enqueue(new MockResponse()
        .setResponseCode(302)
        .setHeader("Location", "http://localhost:" + redirectedServer.getPort()));
    redirectingServer.play();
}
 
开发者ID:Netflix,项目名称:ribbon,代码行数:15,代码来源:FollowRedirectTest.java

示例10: survive

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Test
public void survive() throws IOException, ClientException, URISyntaxException, InterruptedException {

    String clientName = "RibbonClientTest-loadBalancingDefaultPolicyRoundRobin";
    String serverListKey = clientName + ".ribbon.listOfServers";
    int nbHitsPerServer = 60;
    MockWebServer server1 = new MockWebServer();
    MockWebServer server2 = new MockWebServer();

    for (int i = 0; i < nbHitsPerServer; i++) {
        server1.enqueue(new MockResponse().setResponseCode(200).setBody("server1 success <" + i + ">!"));
        server2.enqueue(new MockResponse().setResponseCode(200).setBody("server2 success <" + i + ">!"));
    }

    server1.play();
    server2.play();

    getConfigInstance().setProperty(serverListKey, hostAndPort(server1.getUrl("")) + "," + hostAndPort(server2.getUrl("")));

    RestClient client = (RestClient) ClientFactory.getNamedClient(clientName);
    HttpRequest request;
    for (int i = 0; i < nbHitsPerServer * 2; i++) {
        request = HttpRequest.newBuilder().uri(new URI("/")).build();
        HttpResponse response = client.executeWithLoadBalancer(request);
        response.close();
    }
}
 
开发者ID:Netflix,项目名称:ribbon,代码行数:28,代码来源:ManyShortLivedRequestsSurvivorTest.java

示例11: init

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@BeforeClass
public static void init() throws IOException {
    server = new MockWebServer();
    String content = "Hello world";
    MockResponse response = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain")
            .setBody(content);
    server.enqueue(response);
    server.play();
}
 
开发者ID:Netflix,项目名称:ribbon,代码行数:10,代码来源:DiscoveryEnabledServerListTest.java

示例12: testCommand

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Test
public void testCommand() throws IOException, InterruptedException, ExecutionException {
    MockWebServer server = new MockWebServer();
    String content = "Hello world";
    MockResponse response = new MockResponse()
        .setResponseCode(200)
        .setHeader("Content-type", "text/plain")
        .setBody(content);
    
    server.enqueue(response);        
    server.enqueue(response);       
    server.enqueue(response);       
    server.play();
    
    HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient",
            ClientOptions.create()
            .withMaxAutoRetriesNextServer(3)
            .withReadTimeout(300000)
            .withConfigurationBasedServerList("localhost:12345, localhost:10092, localhost:" + server.getPort()));
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class)
            .withUriTemplate("/")
            .withMethod("GET")
            .build();
    
    RibbonRequest<ByteBuf> request = template.requestBuilder().build();
    
    String result = request.execute().toString(Charset.defaultCharset());
    assertEquals(content, result);
    // repeat the same request
    ByteBuf raw = request.execute();
    result = raw.toString(Charset.defaultCharset());
    raw.release();
    assertEquals(content, result);
    
    result = request.queue().get().toString(Charset.defaultCharset());
    assertEquals(content, result);
}
 
开发者ID:Netflix,项目名称:ribbon,代码行数:38,代码来源:RibbonTest.java

示例13: testHystrixCache

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Test
public void testHystrixCache() throws IOException {
    // LogManager.getRootLogger().setLevel((Level)Level.DEBUG);
    MockWebServer server = new MockWebServer();
    String content = "Hello world";
    MockResponse response = new MockResponse()
        .setResponseCode(200)
        .setHeader("Content-type", "text/plain")
        .setBody(content);
    server.enqueue(response);
    
    server.enqueue(response);       
    server.play();
    
    HttpResourceGroup group = Ribbon.createHttpResourceGroupBuilder("myclient").build();
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test", ByteBuf.class)
            .withUriTemplate("http://localhost:" + server.getPort())
            .withMethod("GET")
            .withRequestCacheKey("xyz")
            .build();
    RibbonRequest<ByteBuf> request = template
            .requestBuilder().build();
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
        RibbonResponse<ByteBuf> ribbonResponse = request.withMetadata().execute();
        assertFalse(ribbonResponse.getHystrixInfo().isResponseFromCache());
        ribbonResponse = request.withMetadata().execute();
        assertTrue(ribbonResponse.getHystrixInfo().isResponseFromCache());
    } finally {
        context.shutdown();
    }
}
 
开发者ID:Netflix,项目名称:ribbon,代码行数:33,代码来源:RibbonTest.java

示例14: testCacheMiss

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
@Test
public void testCacheMiss() throws IOException, InterruptedException {
    MockWebServer server = new MockWebServer();
    String content = "Hello world";
    server.enqueue(new MockResponse()
            .setResponseCode(200)
            .setHeader("Content-type", "text/plain")
            .setBody(content));       
    server.play();
            
    HttpResourceGroup group = Ribbon.createHttpResourceGroup("myclient", ClientOptions.create()
            .withConfigurationBasedServerList("localhost:" + server.getPort())
            .withMaxAutoRetriesNextServer(1));
    final String cacheKey = "somekey";
    HttpRequestTemplate<ByteBuf> template = group.newTemplateBuilder("test")
            .withCacheProvider(cacheKey, new CacheProvider<ByteBuf>(){
                @Override
                public Observable<ByteBuf> get(String key, Map<String, Object> vars) {
                    return Observable.error(new Exception("Cache miss again"));
                }
            })
            .withMethod("GET")
            .withUriTemplate("/").build();
    RibbonRequest<ByteBuf> request = template
            .requestBuilder().build();
    String result = toStringBlocking(request);
    assertEquals(content, result);
}
 
开发者ID:Netflix,项目名称:ribbon,代码行数:29,代码来源:RibbonTest.java

示例15: testServerListRefresh

import com.google.mockwebserver.MockWebServer; //导入依赖的package包/类
/**
 * This test ensures that when server list is refreshed in the load balancer, the set of servers
 * which equals to (oldList - newList) should be removed from the map of cached RxClient. Any server
 * that is not part of oldList should stay in the map.
 *
 * @throws IOException
 */
@Test
public void testServerListRefresh() throws IOException {
    String content = "Hello world";
    MockWebServer server1 = new MockWebServer();
    MockWebServer server2 = new MockWebServer();
    MockWebServer server3 = new MockWebServer();
    MockResponse mockResponse = new MockResponse().setResponseCode(200).setHeader("Content-type", "text/plain")
            .setBody(content);
    server1.enqueue(mockResponse);
    server2.enqueue(mockResponse);
    server3.enqueue(mockResponse);
    server1.play();
    server2.play();
    server3.play();
    try {
        BaseLoadBalancer lb = new BaseLoadBalancer();
        List<Server> initialList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()));
        lb.setServersList(initialList);
        LoadBalancingHttpClient<ByteBuf, ByteBuf> client = RibbonTransport.newHttpClient(lb);
        HttpClientRequest<ByteBuf> request = HttpClientRequest.createGet("/");
        client.submit(request).toBlocking().last();
        client.submit(request).toBlocking().last();
        HttpClientRequest<ByteBuf> request2 = HttpClientRequest.createGet("http://localhost:" + server3.getPort());
        client.submit(request2).toBlocking().last();
        Set<Server> cachedServers = client.getRxClients().keySet();
        assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server2.getPort()), new Server("localhost", server3.getPort())), cachedServers);
        List<Server> newList = Lists.newArrayList(new Server("localhost", server1.getPort()), new Server("localhost", 99999));
        lb.setServersList(newList);
        cachedServers = client.getRxClients().keySet();
        assertEquals(Sets.newHashSet(new Server("localhost", server1.getPort()), new Server("localhost", server3.getPort())), cachedServers);
    } finally {
        server1.shutdown();
        server2.shutdown();
        server3.shutdown();
    }
}
 
开发者ID:Netflix,项目名称:ribbon,代码行数:44,代码来源:ServerListRefreshTest.java


注:本文中的com.google.mockwebserver.MockWebServer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。