Search in sources :

Example 26 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project okhttp by square.

the class OkApacheClientTest method putEmptyEntity.

@Test
public void putEmptyEntity() throws Exception {
    server.enqueue(new MockResponse());
    final HttpPut put = new HttpPut(server.url("/").url().toURI());
    client.execute(put);
    RecordedRequest request = server.takeRequest();
    assertEquals(0, request.getBodySize());
    assertNotNull(request.getBody());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 27 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project okhttp by square.

the class OkApacheClientTest method jsonGzipResponse.

@Test
public void jsonGzipResponse() throws Exception {
    String text = "{\"Message\": { \"text\": \"Hello, World!\" } }";
    server.enqueue(new MockResponse().setBody(gzip(text)).setHeader("Content-Encoding", "gzip").setHeader("Content-Type", "application/json"));
    HttpGet request1 = new HttpGet(server.url("/").url().toURI());
    // Not transparent gzip.
    request1.setHeader("Accept-encoding", "gzip");
    HttpResponse response = client.execute(request1);
    HttpEntity entity = response.getEntity();
    Header[] encodingHeaders = response.getHeaders("Content-Encoding");
    assertEquals(1, encodingHeaders.length);
    assertEquals("gzip", encodingHeaders[0].getValue());
    assertNotNull(entity.getContentEncoding());
    assertEquals("gzip", entity.getContentEncoding().getValue());
    Header[] typeHeaders = response.getHeaders("Content-Type");
    assertEquals(1, typeHeaders.length);
    assertEquals("application/json", typeHeaders[0].getValue());
    assertNotNull(entity.getContentType());
    assertEquals("application/json", entity.getContentType().getValue());
    assertEquals(text, gunzip(entity));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 28 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project okhttp by square.

the class OkApacheClientTest method contentTypeIsCaseInsensitive.

@Test
public void contentTypeIsCaseInsensitive() throws URISyntaxException, IOException {
    server.enqueue(new MockResponse().setBody("{\"Message\": { \"text\": \"Hello, World!\" } }").setHeader("cONTENT-tYPE", "application/json"));
    HttpGet request = new HttpGet(server.url("/").url().toURI());
    HttpResponse response = client.execute(request);
    assertEquals("application/json", response.getEntity().getContentType().getValue());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 29 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project okhttp by square.

the class OkApacheClientTest method contentType.

@Test
public void contentType() throws Exception {
    server.enqueue(new MockResponse().setBody("<html><body><h1>Hello, World!</h1></body></html>").setHeader("Content-Type", "text/html"));
    server.enqueue(new MockResponse().setBody("{\"Message\": { \"text\": \"Hello, World!\" } }").setHeader("Content-Type", "application/json"));
    server.enqueue(new MockResponse().setBody("Hello, World!"));
    HttpGet request1 = new HttpGet(server.url("/").url().toURI());
    HttpResponse response1 = client.execute(request1);
    Header[] headers1 = response1.getHeaders("Content-Type");
    assertEquals(1, headers1.length);
    assertEquals("text/html", headers1[0].getValue());
    assertNotNull(response1.getEntity().getContentType());
    assertEquals("text/html", response1.getEntity().getContentType().getValue());
    HttpGet request2 = new HttpGet(server.url("/").url().toURI());
    HttpResponse response2 = client.execute(request2);
    Header[] headers2 = response2.getHeaders("Content-Type");
    assertEquals(1, headers2.length);
    assertEquals("application/json", headers2[0].getValue());
    assertNotNull(response2.getEntity().getContentType());
    assertEquals("application/json", response2.getEntity().getContentType().getValue());
    HttpGet request3 = new HttpGet(server.url("/").url().toURI());
    HttpResponse response3 = client.execute(request3);
    Header[] headers3 = response3.getHeaders("Content-Type");
    assertEquals(0, headers3.length);
    assertNull(response3.getEntity().getContentType());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 30 with MockResponse

use of okhttp3.mockwebserver.MockResponse in project okhttp by square.

the class ResponseCacheTest method getHeadersDeletesCached100LevelWarnings.

@Test
public void getHeadersDeletesCached100LevelWarnings() throws Exception {
    server.enqueue(new MockResponse().addHeader("Warning: 199 test danger").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    URLConnection connection1 = openConnection(server.url("/").url());
    assertEquals("A", readAscii(connection1));
    assertEquals("199 test danger", connection1.getHeaderField("Warning"));
    URLConnection connection2 = openConnection(server.url("/").url());
    assertEquals("A", readAscii(connection2));
    assertEquals(null, connection2.getHeaderField("Warning"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Aggregations

MockResponse (okhttp3.mockwebserver.MockResponse)1754 Test (org.junit.Test)1284 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)483 AtomicReference (java.util.concurrent.atomic.AtomicReference)258 Test (org.junit.jupiter.api.Test)216 MockWebServer (okhttp3.mockwebserver.MockWebServer)201 CountDownLatch (java.util.concurrent.CountDownLatch)196 IOException (java.io.IOException)158 HttpURLConnection (java.net.HttpURLConnection)157 ANError (com.androidnetworking.error.ANError)148 Response (okhttp3.Response)147 MockResponse (mockwebserver3.MockResponse)115 Buffer (okio.Buffer)104 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)89 WatsonServiceUnitTest (com.ibm.watson.developer_cloud.WatsonServiceUnitTest)84 List (java.util.List)84 URL (java.net.URL)78 URLConnection (java.net.URLConnection)76 Request (okhttp3.Request)70 ANResponse (com.androidnetworking.common.ANResponse)61