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

The following examples show how to use okhttp3.mockwebserver.MockResponse#addHeader() . 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: RequestMatcherRule.java    From requestmatcher with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a fixture to be used during the test case.
 *
 * @param fixturePath The path of the fixture inside the fixtures folder.
 * @param statusCode  The status of the mocked response.
 * @return A dsl instance {@link IfRequestMatches} for chaining
 */
public IfRequestMatches<RequestMatchersGroup> addFixture(int statusCode, String fixturePath) {

    final MockResponse mockResponse = new MockResponse()
            .setResponseCode(statusCode)
            .setBody(readFixture(fixturePath));

    if (guessMimeType) {
        final String mimeType = IOReader.mimeTypeFromExtension(fixturePath);

        if (mimeType != null) {
            mockResponse.addHeader("Content-Type", mimeType);
        }
    }

    if (!defaultHeaders.isEmpty()) {
        for (String headerKey : defaultHeaders.keySet()) {
            mockResponse.addHeader(headerKey, defaultHeaders.get(headerKey));
        }
    }

    return addResponse(mockResponse);
}
 
Example 2
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 3
Source File: RawJSONMockResponse.java    From android-sdk with MIT License 6 votes vote down vote up
public static MockResponse fromRawResource(InputStream inputStream) throws IOException, JSONException {

        String theString = Utils.toString(inputStream);
        JSONObject json = new JSONObject(theString);
        MockResponse value = new MockResponse();

        value.setBody(json.getJSONObject("body").toString());
        value.setResponseCode(json.optInt("statusCode", 200));

        JSONObject headers = json.optJSONObject("headers");
        if (headers != null) {
            Iterator<String> keys = headers.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                value.addHeader(key, headers.get(key));
            }
        }

        return value;
    }
 
Example 4
Source File: MockServer.java    From auth0-java with MIT License 5 votes vote down vote up
public void rateLimitReachedResponse(long limit, long remaining, long reset) {
    MockResponse response = new MockResponse().setResponseCode(429);
    if (limit != -1) {
        response.addHeader("X-RateLimit-Limit", String.valueOf(limit));
    }
    if (remaining != -1) {
        response.addHeader("X-RateLimit-Remaining", String.valueOf(remaining));
    }
    if (reset != -1) {
        response.addHeader("X-RateLimit-Reset", String.valueOf(reset));
    }
    server.enqueue(response);
}
 
Example 5
Source File: MoodleWebServiceTest.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
private void enqueueResponse(String fileName, Map<String, String> headers) throws IOException {
    String file = "api-response/" + fileName;
    InputStream inputStream = getClass().getClassLoader()
            .getResourceAsStream(file);
    BufferedSource source = Okio.buffer(Okio.source(inputStream));
    MockResponse mockResponse = new MockResponse();
    for (Map.Entry<String, String> header : headers.entrySet()) {
        mockResponse.addHeader(header.getKey(), header.getValue());
    }
    mockWebServer.enqueue(mockResponse
            .setBody(source.readString(Charset.forName("UTF-8"))));
}
 
Example 6
Source File: Benchmark.java    From jus with Apache License 2.0 5 votes vote down vote up
private MockResponse newResponse(States.GenericState state) throws IOException {
  byte[] bytes = new byte[state.bodyByteCount];
  state.random.nextBytes(bytes);
  Buffer body = new Buffer().write(bytes);

  MockResponse result = new MockResponse();

  if (state.gzip) {
    Buffer gzipBody = new Buffer();
    GzipSink gzipSink = new GzipSink(gzipBody);
    gzipSink.write(body, body.size());
    gzipSink.close();
    body = gzipBody;
    result.addHeader("Content-Encoding: gzip");
  }

  if (state.chunked) {
    result.setChunkedBody(body, 1024);
  } else {
    result.setBody(body);
  }

  for (int i = 0; i < state.headerCount; i++) {
    result.addHeader(randomString(12, state), randomString(20, state));
  }

  return result;
}
 
Example 7
Source File: AwsClientTracingTest.java    From zipkin-aws with Apache License 2.0 4 votes vote down vote up
private MockResponse createDeleteItemResponse() {
  MockResponse response = new MockResponse();
  response.setBody("{}");
  response.addHeader("x-amzn-RequestId", "abcd");
  return response;
}