use of okhttp3.mockwebserver.MockResponse in project feign by OpenFeign.
the class AbstractClientTest method postWithSpacesInPath.
@Test
public void postWithSpacesInPath() throws IOException, InterruptedException {
server.enqueue(new MockResponse().setBody("foo"));
TestInterface api = newBuilder().target(TestInterface.class, "http://localhost:" + server.getPort());
Response response = api.post("current documents", "foo");
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST").hasPath("/path/current%20documents/resource").hasBody("foo");
}
use of okhttp3.mockwebserver.MockResponse in project feign by OpenFeign.
the class FeignTest method responseCoercesToStringBody.
@Test
public void responseCoercesToStringBody() throws Exception {
server.enqueue(new MockResponse().setBody("foo"));
TestInterface api = new TestInterfaceBuilder().target("http://localhost:" + server.getPort());
Response response = api.response();
assertTrue(response.body().isRepeatable());
assertEquals("foo", response.body().toString());
}
use of okhttp3.mockwebserver.MockResponse in project Parse-SDK-Android by ParsePlatform.
the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse.
@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
// Make mock response
Buffer buffer = new Buffer();
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
gzipOut.write("content".getBytes());
gzipOut.close();
buffer.write(byteOut.toByteArray());
MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer).setHeader("Content-Encoding", "gzip");
// Start mock server
server.enqueue(mockResponse);
server.start();
ParseHttpClient client = new ParseOkHttpClient(10000, null);
final Semaphore done = new Semaphore(0);
// Add plain interceptor to disable decompress response stream
client.addExternalInterceptor(new ParseNetworkInterceptor() {
@Override
public ParseHttpResponse intercept(Chain chain) throws IOException {
done.release();
ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
// Make sure the response we get from the interceptor is the raw gzip stream
byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
assertArrayEquals(byteOut.toByteArray(), content);
// We need to set a new stream since we have read it
return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray())).build();
}
});
// We do not need to add Accept-Encoding header manually, httpClient library should do that.
String requestUrl = server.url("/").toString();
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.GET).build();
// Execute request
ParseHttpResponse parseResponse = client.execute(parseRequest);
// Make sure the response we get is ungziped by OkHttp library
byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
assertArrayEquals("content".getBytes(), content);
// Make sure interceptor is called
assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));
server.shutdown();
}
use of okhttp3.mockwebserver.MockResponse in project sonarqube by SonarSource.
the class WebhookCallerImplTest method send_posts_payload_to_http_server.
@Test
public void send_posts_payload_to_http_server() throws Exception {
Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", server.url("/ping").toString());
server.enqueue(new MockResponse().setBody("pong").setResponseCode(201));
WebhookDelivery delivery = newSender().call(webhook, PAYLOAD);
assertThat(delivery.getHttpStatus().get()).isEqualTo(201);
assertThat(delivery.getDurationInMs().get()).isGreaterThanOrEqualTo(0);
assertThat(delivery.getError()).isEmpty();
assertThat(delivery.getAt()).isEqualTo(NOW);
assertThat(delivery.getWebhook()).isSameAs(webhook);
assertThat(delivery.getPayload()).isSameAs(PAYLOAD);
RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("POST");
assertThat(recordedRequest.getPath()).isEqualTo("/ping");
assertThat(recordedRequest.getBody().readUtf8()).isEqualTo(PAYLOAD.getJson());
assertThat(recordedRequest.getHeader("User-Agent")).isEqualTo("SonarQube/6.2");
assertThat(recordedRequest.getHeader("Content-Type")).isEqualTo("application/json; charset=utf-8");
assertThat(recordedRequest.getHeader("X-SonarQube-Project")).isEqualTo(PAYLOAD.getProjectKey());
}
use of okhttp3.mockwebserver.MockResponse in project sonarqube by SonarSource.
the class WebhookCallerImplTest method redirects_throws_ISE_if_header_Location_does_not_relate_to_a_supported_protocol.
@Test
public void redirects_throws_ISE_if_header_Location_does_not_relate_to_a_supported_protocol() throws Exception {
HttpUrl url = server.url("/redirect");
Webhook webhook = new Webhook(PROJECT_UUID, CE_TASK_UUID, "my-webhook", url.toString());
server.enqueue(new MockResponse().setResponseCode(307).setHeader("Location", "ftp://foo"));
WebhookDelivery delivery = newSender().call(webhook, PAYLOAD);
Throwable error = delivery.getError().get();
assertThat(error).isInstanceOf(IllegalStateException.class).hasMessage("Unsupported protocol in redirect of " + url + " to ftp://foo");
}
Aggregations