Java Code Examples for com.squareup.okhttp.mockwebserver.MockWebServer#setDispatcher()

The following examples show how to use com.squareup.okhttp.mockwebserver.MockWebServer#setDispatcher() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntegrationApiModule.java    From android-step-by-step with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 2
Source File: SampleServer.java    From httplite with Apache License 2.0 5 votes vote down vote up
public void run() throws IOException {
    MockWebServer server = new MockWebServer();
    server.useHttps(sslContext.getSocketFactory(), false);
    server.setDispatcher(this);
    InetAddress address = HttpUtil.getHostAddress();
    server.start(address,port);
    System.out.println(String.format("Started server for: http://%s:%d/", address.getHostAddress(), port));
}
 
Example 3
Source File: ApiInterfaceTest.java    From android-step-by-step with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 4
Source File: MockAppliveryInstance.java    From applivery-android-sdk with Apache License 2.0 4 votes vote down vote up
public static AppliveryTestApi getApiTestInstance(){
  MockWebServer server = new MockWebServer();
  server.setDispatcher(getMockwebserverDispatcherInstance());
  return initializeApiclient(server);
}
 
Example 5
Source File: MockAppliveryInstance.java    From applivery-android-sdk with Apache License 2.0 4 votes vote down vote up
public static AppliveryApiService getApiServiceInstance(){
  MockWebServer server = new MockWebServer();
  server.setDispatcher(getMockwebserverDispatcherInstance());
  return initializeApiServiceClient(server);
}
 
Example 6
Source File: ConsulConfigurationSourceIntegrationTest.java    From cfg4j with Apache License 2.0 4 votes vote down vote up
private void runMockServer() throws IOException {
  server = new MockWebServer();
  server.setDispatcher(dispatcher);
  server.start(0);
}
 
Example 7
Source File: SimpleConfigurationProviderIntegrationTest.java    From cfg4j with Apache License 2.0 4 votes vote down vote up
private void runMockServer() throws IOException {
  server = new MockWebServer();
  server.setDispatcher(dispatcher);
  server.start(0);
}
 
Example 8
Source File: ClientBasicFlowsTest.java    From ob1k with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setup() throws IOException {
  server = new MockWebServer();
  dispatcher = new CustomDispatcher();
  server.setDispatcher(dispatcher);
}