Java Code Examples for okhttp3.mockwebserver.MockResponse#setBodyDelay()

The following examples show how to use okhttp3.mockwebserver.MockResponse#setBodyDelay() . 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: PrebidServerAdapterTest.java    From prebid-mobile-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testStopRequest() throws Exception {
    MockResponse response = new MockResponse();
    response.setBody(MockPrebidServerResponses.noBid());
    response.setBodyDelay(1, TimeUnit.SECONDS);
    server.enqueue(response);
    HttpUrl hostUrl = server.url("/");
    Host.CUSTOM.setHostUrl(hostUrl.toString());
    PrebidMobile.setPrebidServerHost(Host.CUSTOM);
    PrebidMobile.setPrebidServerAccountId("12345");
    PrebidMobile.setShareGeoLocation(true);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    final DemandAdapter.DemandAdapterListener mockListener = mock(DemandAdapter.DemandAdapterListener.class);
    final PrebidServerAdapter adapter = new PrebidServerAdapter();
    HashSet<AdSize> sizes = new HashSet<>();
    sizes.add(new AdSize(320, 50));
    final RequestParams requestParams = new RequestParams("67890", AdType.BANNER, sizes);
    final String uuid = UUID.randomUUID().toString();
    adapter.requestDemand(requestParams, mockListener, uuid);
    bgScheduler.runOneTask();
    adapter.stopRequest(uuid);
    Robolectric.flushBackgroundThreadScheduler();
    Robolectric.flushForegroundThreadScheduler();
    verify(mockListener, never()).onDemandFailed(ResultCode.NO_BIDS, uuid);
}
 
Example 2
Source File: SimpleResponse.java    From mockwebserver with Apache License 2.0 6 votes vote down vote up
public MockResponse toMockResponse(RecordedRequest request) {
  MockResponse mockResponse = new MockResponse();
  mockResponse.setHeaders(bodyProvider.getHeaders());
  mockResponse.setResponseCode(bodyProvider.getStatusCode(request));

  if (webSocketSession != null) {
    mockResponse.withWebSocketUpgrade(webSocketSession);
  } else {
    mockResponse.setBody(bodyProvider.getBody(request));
  }

  if (responseDelay > 0) {
    mockResponse.setBodyDelay(responseDelay, responseDelayUnit);
  }

  return mockResponse;
}
 
Example 3
Source File: Fixture.java    From mockwebserverplus with Apache License 2.0 6 votes vote down vote up
public MockResponse toMockResponse() {
  MockResponse mockResponse = new MockResponse();
  if (this.statusCode != 0) {
    mockResponse.setResponseCode(this.statusCode);
  }
  if (this.body != null) {
    mockResponse.setBody(this.body);
  }
  if (this.delay != 0) {
    mockResponse.setBodyDelay(this.delay, TimeUnit.MILLISECONDS);
  }
  if (this.headers != null) {
    for (String header : this.headers) {
      mockResponse.addHeader(header);
    }
  }
  return mockResponse;
}
 
Example 4
Source File: MatchableCall.java    From RESTMock with Apache License 2.0 6 votes vote down vote up
private void setResponseDelayInternal(MockResponse response) {
    long bodyDelay;
    long headerDelay;
    if (bodyDelays.isEmpty()) {
        bodyDelay = 0;
    } else if (responseIndex >= bodyDelays.size()) {
        bodyDelay = bodyDelays.get(bodyDelays.size() - 1);
    } else {
        bodyDelay = bodyDelays.get(responseIndex);
    }
    if (headerDelays.isEmpty()) {
        headerDelay = 0;
    } else if (responseIndex >= headerDelays.size()) {
        headerDelay = headerDelays.get(headerDelays.size() - 1);
    } else {
        headerDelay = headerDelays.get(responseIndex);
    }
    if (bodyDelay != 0) {
        response.setBodyDelay(bodyDelay, TimeUnit.MILLISECONDS);
    }
    if (headerDelay != 0) {
        response.setHeadersDelay(headerDelay, TimeUnit.MILLISECONDS);
    }
}
 
Example 5
Source File: ChunkedResponse.java    From mockwebserver with Apache License 2.0 5 votes vote down vote up
public MockResponse toMockResponse(RecordedRequest request) {
    MockResponse mockResponse = new MockResponse();
    mockResponse.setHeaders(bodyProvider.getHeaders());
    mockResponse.setChunkedBody(concatBody(request), DEFAULT_MAX_CHUNK_SIZE);
    mockResponse.setResponseCode(bodyProvider.getStatusCode(request));

    if (responseDelay > 0) {
        mockResponse.setBodyDelay(responseDelay, responseDelayUnit);
    }

    return mockResponse;
}
 
Example 6
Source File: OpenCensusTest.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
private MockResponse mockResponse(int code, String status, int delayMs) {
  MockResponse response = new MockResponse();
  response.setResponseCode(code);
  if (status != null) {
    response.setBody("{\"results\" : [{}], \"status\" : \"" + status + "\" }");
  }
  response.setBodyDelay(delayMs, TimeUnit.MILLISECONDS);
  return response;
}
 
Example 7
Source File: MockWebServerUtil.java    From gandalf with Apache License 2.0 3 votes vote down vote up
/**
 * Starts a mock web server to return the bootstrap file set using setMockBootstrapRes(Context context, int rawResourceId)
 * @param context
 * @return
 */
public static String startMockWebServer(@NonNull final Context context) {

    shutdownWebServer();
    mockWebServer = new MockWebServer();

    try {
        mockWebServer.start();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

        int mockBootstrapResId = sharedPreferences.getInt(KEY_BOOTSTRAP_RES_ID, R.raw.no_action_bootstrap);

        String mockBootstrapJsonBody = getMockJsonBootstrap(context, mockBootstrapResId);

        MockResponse mockResponse = new MockResponse();
        mockResponse.setResponseCode(200);
        mockResponse.setBody(mockBootstrapJsonBody);
        mockResponse.setBodyDelay(2, TimeUnit.SECONDS);

        mockWebServer.enqueue(mockResponse);

    } catch (IOException e) {
        throw new RuntimeException("Problem starting mock web server");
    }


    return String.valueOf(mockWebServer.url(""));
}