Java 类com.squareup.okhttp.mockwebserver.Dispatcher 实例源码

项目:gigigo-utils-suite    文件:NetworkExecutorTest.java   
private Dispatcher getMockwebserverDispatcherInstance() {
  return  new Dispatcher() {
    @Override public MockResponse dispatch(RecordedRequest request)
        throws InterruptedException {
      try {

        if (request.getPath().equals("/ok/")) {
          return new MockResponse().setResponseCode(200).setBody(
              TestUtils.getContentFromFile("testOkHttp.json", this));
        } else if (request.getPath().equals("/error/")) {
          return new MockResponse().setStatus("HTTP/1.1 500 KO")
              .setBody(TestUtils.getContentFromFile("testErrorHttp.json", this));
        } else {
          return new MockResponse().setResponseCode(ERROR_RESPONSE_CODE).setBody(
              TestUtils.getContentFromFile("testBadHttp.json", this));
        }
      } catch (Exception e) {
        e.printStackTrace();
        return new MockResponse().setResponseCode(ERROR_RESPONSE_CODE).setBody("");
      }
    }
  };
}
项目:external-resources    文件:DownloaderTest.java   
@Before public void setUp() throws Exception {
  initMocks(this);

  setDefaultConfiguration();
  when(context.getApplicationContext()).thenReturn(RuntimeEnvironment.application);

  final String successJson =
      IOUtils.toString(getClass().getResourceAsStream("/test.json"), "UTF-8");

  server.setDispatcher(new Dispatcher() {
    @Override public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
      if (request.getPath().startsWith("/success")) {
        return new MockResponse().setResponseCode(200).setBody(successJson);
      } else {
        return new MockResponse().setResponseCode(400);
      }
    }
  });
}
项目:android-step-by-step    文件:IntegrationBaseTest.java   
protected void setCustomAnswer(boolean enableBranches, boolean enableContributors) {
    mockWebServer.setDispatcher(new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {

            if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/repos.json"));
            } else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches") && enableBranches) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/branches.json"));
            } else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors") && enableContributors) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/contributors.json"));
            }
            return new MockResponse().setResponseCode(404);
        }
    });

}
项目:android-step-by-step    文件:IntegrationApiModule.java   
public ApiInterface getApiInterface(MockWebServer mockWebServer) throws IOException {
    mockWebServer.start();
    TestUtils testUtils = new TestUtils();
    final Dispatcher dispatcher = new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {

            if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/repos.json"));
            } else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/branches.json"));
            } else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/contributors.json"));
            }
            return new MockResponse().setResponseCode(404);
        }
    };

    mockWebServer.setDispatcher(dispatcher);
    HttpUrl baseUrl = mockWebServer.url("/");
    return ApiModule.getApiInterface(baseUrl.toString());
}
项目:ecampus-client-android    文件:UserServiceTest.java   
@Before
public void setUp() throws Exception {
    super.setUp();
    server = new MockWebServer();
    server.start();
    final Dispatcher dispatcher = new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws
                InterruptedException {
            if (request.getPath().equals("/account/info")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(jsonReader.readString("json/user.json"));
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    server.setDispatcher(dispatcher);
    HttpUrl baseUrl = server.url("/");
    service = ServiceCreator.createTestService(baseUrl.toString(),
            UserService.class);
}
项目:platform_external_okhttp    文件:Benchmark.java   
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;
}
项目:ecampus-client-android    文件:UserServiceTest.java   
@Before
public void setUp() throws Exception {
    super.setUp();
    server = new MockWebServer();
    server.start();
    final Dispatcher dispatcher = new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws
                InterruptedException {
            if (request.getPath().equals("/account/info")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(jsonReader.readString("json/user.json"));
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    server.setDispatcher(dispatcher);
    HttpUrl baseUrl = server.url("/");
    service = ServiceCreator.createTestService(baseUrl.toString(),
            UserService.class);
}
项目:jaxrs-utils    文件:OkHttpClientEngineTest.java   
@Before
public void configureServer() {
  mockServer.setDispatcher(
      new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
          assertThat(request.getHeader(INJECTED_HEADER_NAME)).isEqualTo(HEADER_VALUE);
          switch (request.getPath()) {
            case "/simple":
              return new MockResponse()
                  .setResponseCode(Response.Status.NOT_FOUND.getStatusCode())
                  .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
                  .setHeader(HEADER_NAME, HEADER_VALUE)
                  .setBody("Not found");
            case "/writerInterceptor":
              assertThat(request.getBody().readByteArray()).isEqualTo(PAYLOAD);
              // fall-through
            case "/requestHeader":
              assertThat(request.getHeader(HEADER_NAME)).isEqualTo(HEADER_VALUE);
              return new MockResponse()
                  .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
                  .setBody(request.getHeader(HEADER_NAME));
          }
          throw new AssertionError("Unexpected request: " + request);
        }
      });
}
项目:android-step-by-step    文件:IntegrationBaseTest.java   
protected void setErrorAnswerWebServer() {
    mockWebServer.setDispatcher(new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            return new MockResponse().setResponseCode(500);
        }
    });
}
项目:android-step-by-step    文件:ApiInterfaceTest.java   
@Before
public void setUp() throws Exception {
    super.setUp();
    server = new MockWebServer();
    server.start();
    final Dispatcher dispatcher = new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {

            if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/repos.json"));
            } else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/branches.json"));
            } else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/contributors.json"));
            }
            return new MockResponse().setResponseCode(404);
        }
    };

    server.setDispatcher(dispatcher);
    HttpUrl baseUrl = server.url("/");
    apiInterface = ApiModule.getApiInterface(baseUrl.toString());
}
项目:applivery-android-sdk    文件:MockAppliveryInstance.java   
private static Dispatcher getMockwebserverDispatcherInstance() {
  return  new Dispatcher() {
    @Override public MockResponse dispatch(RecordedRequest request)
        throws InterruptedException {
      try {

        if (request.getPath().equals("/api/apps/test")) {
          return new MockResponse().setResponseCode(200).setBody(
              TestUtils.getContentFromFile("testOkHttp.json", this));
        } else if (request.getPath().equals("/api/apps/error")) {
          return new MockResponse().setStatus("HTTP/1.1 500 KO")
              .setBody(TestUtils.getContentFromFile("testErrorHttp.json", this));
        } else if (request.getPath().equals("/api/apps/errorBusiness")) {
          return new MockResponse().setResponseCode(200).setBody(
              TestUtils.getContentFromFile("testErrorBusiness.json", this));
        } else if (request.getPath().equals("/api/apps/bad")) {
          return new MockResponse().setResponseCode(ERROR_RESPONSE_CODE).setBody(
              TestUtils.getContentFromFile("testBadHttp.json", this));
        } else if (request.getPath().equals("/api/builds/1/token")) {
          return new MockResponse().setResponseCode(200).setBody(
              TestUtils.getContentFromFile("testOkBuildToken.json", this));
        } else if (request.getPath().equals("/download/1/manifest/2")) {
          return new MockResponse().setResponseCode(200).setBody(
              TestUtils.getContentFromFile("testOkBuild.json", this));
        }else{
          return null;
        }
      } catch (Exception e) {
        e.printStackTrace();
        return new MockResponse().setResponseCode(ERROR_RESPONSE_CODE).setBody("");
      }
    }
  };
}
项目:trabajando-en-diferido    文件:MockApiCalls.java   
public void init() {
  new Thread(new Runnable() {
    public void run() {
      mockWebServer.setDispatcher(new Dispatcher() {
        @Override public MockResponse dispatch(RecordedRequest request)
            throws InterruptedException {
          if (request.getPath().equals("/1/")) {
            Thread.sleep(2000);
            return new MockResponse().setResponseCode(OK_STATUS).setBody("{\"value\":\"1\"}");
          } else if (request.getPath().equals("/2/")) {
            Thread.sleep(3000);
            return new MockResponse().setResponseCode(OK_STATUS).setBody("{\"value\":\"2\"}");
          } else {
            Thread.sleep(4000);
            return new MockResponse().setResponseCode(OK_STATUS).setBody("{\"value\":\"3\"}");
          }
        }
      });

      try {
        mockWebServer.start();
      } catch (Exception e) {
        Log.e(LOGTAG, "ERROR ", e);
      }
    }
  }, "serverThread").start();
}
项目:android-simple-architecture    文件:ApiInterfaceTest.java   
@Before
public void setUp() throws Exception {
    server = new MockWebServer();
    server.start();
    final Dispatcher dispatcher = new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {

            if (request.getPath().equals("/users/" + TestConst.TEST_OWNER + "/repos")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/repos"));
            } else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/branches")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/branches"));
            } else if (request.getPath().equals("/repos/" + TestConst.TEST_OWNER + "/" + TestConst.TEST_REPO + "/contributors")) {
                return new MockResponse().setResponseCode(200)
                        .setBody(testUtils.readString("json/contributors"));
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    server.setDispatcher(dispatcher);
    HttpUrl baseUrl = server.url("/");

    Retrofit.Builder builder = new Retrofit.Builder().
            baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create());

    apiInterface = builder.build().create(ApiInterface.class);
}
项目:platform_external_okhttp    文件:AsyncApiTest.java   
@Test public void canceledBeforeResponseReadIsNeverDelivered() throws Exception {
  client.getDispatcher().setMaxRequests(1); // Force requests to be executed serially.
  server.setDispatcher(new Dispatcher() {
    char nextResponse = 'A';
    @Override public MockResponse dispatch(RecordedRequest request) {
      client.cancel("request A");
      return new MockResponse().setBody(Character.toString(nextResponse++));
    }
  });
  server.play();

  // Canceling a request after the server has received a request but before
  // it has delivered the response. That request will never be received to the
  // client.
  Request requestA = new Request.Builder().url(server.getUrl("/a")).tag("request A").build();
  client.enqueue(requestA, receiver);
  assertEquals("/a", server.takeRequest().getPath());

  // We then make a second request (not canceled) to make sure the receiver
  // has nothing left to wait for.
  Request requestB = new Request.Builder().url(server.getUrl("/b")).tag("request B").build();
  client.enqueue(requestB, receiver);
  assertEquals("/b", server.takeRequest().getPath());
  receiver.await(requestB.url()).assertBody("B");

  // At this point we know the receiver is ready: if it hasn't received 'A'
  // yet it never will.
  receiver.assertNoResponse(requestA.url());
}
项目:ecampus-client-android    文件:BulletinServiceTest.java   
@Before
public void setUp() throws Exception {
    super.setUp();
    server = new MockWebServer();
    server.start();
    final Dispatcher dispatcher = new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws
                InterruptedException {
            String p = request.getPath();
            MockResponse response = new MockResponse().setResponseCode(200);
            if (p.matches("/board/all.*") ||
                    p.matches("/board/moderator/all.*")) {
                return response.setBody(jsonReader.readString
                        ("json/bulletins.json"));
            } else if (p.equals("/board") ||
                    p.equals("/board/" + BULLETIN_ID)) {
                return response.setBody(jsonReader.readString
                        ("json/ok.json"));
            } else if (p.equals("/board/" + BULLETIN_ID + "/recipient")) {
                return response.setBody(jsonReader.readString
                        ("json/bull_recipients.json"));
            } else if (p.equals("/subdivision/" + SUBDIV_ID +
                    "/children")) {
                return response.setBody(jsonReader.readString
                        ("json/desc_subdivision.json"));
            } else if (p.equals("/roles")) {
                return response.setBody(jsonReader.readString("json/roles" +
                        ".json"));
            } else if (p.equals("/subdivision/" + SUBDIV_ID + "/group")) {
                return response.setBody(jsonReader.readString
                        ("json/groups.json"));
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    server.setDispatcher(dispatcher);
    HttpUrl baseUrl = server.url("/");
    service = ServiceCreator.createTestService(baseUrl.toString(),
            BulletinService.class);
}
项目:Rx-Soap    文件:SoapDispatcher.java   
public Dispatcher getDispatcher() {
    return dispatcher;
}
项目:ecampus-client-android    文件:BulletinServiceTest.java   
@Before
public void setUp() throws Exception {
    super.setUp();
    server = new MockWebServer();
    server.start();
    final Dispatcher dispatcher = new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws
                InterruptedException {
            String p = request.getPath();
            MockResponse response = new MockResponse().setResponseCode(200);
            if (p.matches("/board/all.*") ||
                    p.matches("/board/moderator/all.*")) {
                return response.setBody(jsonReader.readString
                        ("json/bulletins.json"));
            } else if (p.equals("/board") ||
                    p.equals("/board/" + BULLETIN_ID)) {
                return response.setBody(jsonReader.readString
                        ("json/ok.json"));
            } else if (p.equals("/board/" + BULLETIN_ID + "/recipient")) {
                return response.setBody(jsonReader.readString
                        ("json/bull_recipients.json"));
            } else if (p.equals("/subdivision/" + SUBDIV_ID +
                    "/children")) {
                return response.setBody(jsonReader.readString
                        ("json/desc_subdivision.json"));
            } else if (p.equals("/roles")) {
                return response.setBody(jsonReader.readString("json/roles" +
                        ".json"));
            } else if (p.equals("/subdivision/" + SUBDIV_ID + "/group")) {
                return response.setBody(jsonReader.readString
                        ("json/groups.json"));
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    server.setDispatcher(dispatcher);
    HttpUrl baseUrl = server.url("/");
    service = ServiceCreator.createTestService(baseUrl.toString(),
            BulletinService.class);
}