com.squareup.okhttp.mockwebserver.Dispatcher Java Examples

The following examples show how to use com.squareup.okhttp.mockwebserver.Dispatcher. 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: DownloaderTest.java    From external-resources with Apache License 2.0 6 votes vote down vote up
@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);
      }
    }
  });
}
 
Example #2
Source File: IntegrationBaseTest.java    From android-step-by-step with Apache License 2.0 6 votes vote down vote up
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);
        }
    });

}
 
Example #3
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 #4
Source File: IntegrationBaseTest.java    From android-step-by-step with Apache License 2.0 5 votes vote down vote up
protected void setErrorAnswerWebServer() {
    mockWebServer.setDispatcher(new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            return new MockResponse().setResponseCode(500);
        }
    });
}
 
Example #5
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 #6
Source File: MockAppliveryInstance.java    From applivery-android-sdk with Apache License 2.0 5 votes vote down vote up
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("");
      }
    }
  };
}
 
Example #7
Source File: NativeRequestTest.java    From mobile-sdk-android with Apache License 2.0 4 votes vote down vote up
@Test
public void testNativeCSRResponseLogImpresionsClicksProperly() {
    final HashMap<String, Boolean> logs = new HashMap<>();
    logs.put("impression", false);
    logs.put("click", false);
    logs.put("request_url", false);
    logs.put("response_url", false);
    HttpUrl impression = server.url("/impression");
    HttpUrl click = server.url("/click");
    final HttpUrl request_url = server.url("/request_url");
    final HttpUrl response_url = server.url("/response_url");
    final MockResponse impbusResponse = new MockResponse().setResponseCode(200).setBody(TestResponsesUT.csrNativeSuccesfulWithMockTrackers(impression.toString(), click.toString(), request_url.toString(), response_url.toString()));
    final Dispatcher dispatcher = new Dispatcher() {
        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            String path = request.getPath();
            if ("/".equals(path)) {
                return impbusResponse;
            } else if ("/impression".equals(path)) {
                logs.put("impresssion", true);
                return new MockResponse().setResponseCode(200);
            } else if ("/click".equals(path)) {
                logs.put("click", true);
                return new MockResponse().setResponseCode(200);
            } else if ("/request_url".equals(path)) {
                logs.put("request_url", true);
                return new MockResponse().setResponseCode(200);
            } else if (path != null && path.startsWith("/response_url")) {
                logs.put("response_url", true);
                return new MockResponse().setResponseCode(200);
            }
            return new MockResponse().setResponseCode(404);
        }
    };
    server.setDispatcher(dispatcher);
    HttpUrl impbus = server.url("/");
    UTConstants.REQUEST_BASE_URL_UT = impbus.toString();
    NativeAdRequestListener listener = new NativeAdRequestListener() {

        @Override
        public void onAdLoaded(NativeAdResponse response) {
            nativeAdResponse = response;
            ((MockFBNativeBannerAdResponse) response).logImpression();
            ((MockFBNativeBannerAdResponse) response).clickAd();
        }

        @Override
        public void onAdFailed(ResultCode errorcode, ANAdResponseInfo adResponseInfo) {
        }
    };
    adRequest.setListener(listener);
    adRequest.loadAd();
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertNotNull(nativeAdResponse);
    assertTrue(nativeAdResponse instanceof MockFBNativeBannerAdResponse);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    waitForTasks();
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    assertTrue(logs.get("impresssion"));
    assertTrue(logs.get("click"));
    assertTrue(logs.get("response_url"));
}