Search in sources :

Example 31 with Headers

use of okhttp3.Headers in project retrofit by square.

the class RequestBuilderTest method headerParam.

@Test
public void headerParam() {
    class Example {

        //
        @GET("/foo/bar/")
        //
        @Headers("ping: pong")
        Call<ResponseBody> method(@Header("kit") String kit) {
            return null;
        }
    }
    Request request = buildRequest(Example.class, "kat");
    assertThat(request.method()).isEqualTo("GET");
    okhttp3.Headers headers = request.headers();
    assertThat(headers.size()).isEqualTo(2);
    assertThat(headers.get("ping")).isEqualTo("pong");
    assertThat(headers.get("kit")).isEqualTo("kat");
    assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
    assertThat(request.body()).isNull();
}
Also used : Header(retrofit2.http.Header) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 32 with Headers

use of okhttp3.Headers in project retrofit by square.

the class RequestBuilderTest method getWithHeaderMap.

@Test
public void getWithHeaderMap() {
    class Example {

        @GET("/search")
        Call<ResponseBody> method(@HeaderMap Map<String, Object> headers) {
            return null;
        }
    }
    Map<String, Object> headers = new LinkedHashMap<>();
    headers.put("Accept", "text/plain");
    headers.put("Accept-Charset", "utf-8");
    Request request = buildRequest(Example.class, headers);
    assertThat(request.method()).isEqualTo("GET");
    assertThat(request.url().toString()).isEqualTo("http://example.com/search");
    assertThat(request.body()).isNull();
    assertThat(request.headers().size()).isEqualTo(2);
    assertThat(request.header("Accept")).isEqualTo("text/plain");
    assertThat(request.header("Accept-Charset")).isEqualTo("utf-8");
}
Also used : HeaderMap(retrofit2.http.HeaderMap) Request(okhttp3.Request) PartMap(retrofit2.http.PartMap) HashMap(java.util.HashMap) HeaderMap(retrofit2.http.HeaderMap) FieldMap(retrofit2.http.FieldMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) QueryMap(retrofit2.http.QueryMap) ResponseBody(okhttp3.ResponseBody) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 33 with Headers

use of okhttp3.Headers in project retrofit by square.

the class RequestBuilderTest method malformedContentTypeHeaderThrows.

@Test
public void malformedContentTypeHeaderThrows() {
    class Example {

        //
        @POST("/")
        //
        @Headers("Content-Type: hello, world!")
        Call<ResponseBody> method(@Body RequestBody body) {
            return null;
        }
    }
    RequestBody body = RequestBody.create(MediaType.parse("text/plain"), "hi");
    try {
        buildRequest(Example.class, body);
        fail();
    } catch (IllegalArgumentException e) {
        assertThat(e).hasMessage("Malformed content type: hello, world!\n" + "    for method Example.method");
    }
}
Also used : RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) MultipartBody(okhttp3.MultipartBody) Body(retrofit2.http.Body) RequestBody(okhttp3.RequestBody) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 34 with Headers

use of okhttp3.Headers in project okhttp by square.

the class MockWebServer method handleWebSocketUpgrade.

private void handleWebSocketUpgrade(Socket socket, BufferedSource source, BufferedSink sink, RecordedRequest request, MockResponse response) throws IOException {
    String key = request.getHeader("Sec-WebSocket-Key");
    response.setHeader("Sec-WebSocket-Accept", WebSocketProtocol.acceptHeader(key));
    writeHttpResponse(socket, sink, response);
    // Adapt the request and response into our Request and Response domain model.
    String scheme = request.getTlsVersion() != null ? "https" : "http";
    // Has host and port.
    String authority = request.getHeader("Host");
    final Request fancyRequest = new Request.Builder().url(scheme + "://" + authority + "/").headers(request.getHeaders()).build();
    final Response fancyResponse = new Response.Builder().code(Integer.parseInt(response.getStatus().split(" ")[1])).message(response.getStatus().split(" ", 3)[2]).headers(response.getHeaders()).request(fancyRequest).protocol(Protocol.HTTP_1_1).build();
    final CountDownLatch connectionClose = new CountDownLatch(1);
    RealWebSocket.Streams streams = new RealWebSocket.Streams(false, source, sink) {

        @Override
        public void close() {
            connectionClose.countDown();
        }
    };
    RealWebSocket webSocket = new RealWebSocket(fancyRequest, response.getWebSocketListener(), new SecureRandom());
    response.getWebSocketListener().onOpen(webSocket, fancyResponse);
    String name = "MockWebServer WebSocket " + request.getPath();
    webSocket.initReaderAndWriter(name, 0, streams);
    try {
        webSocket.loopReader();
        // Even if messages are no longer being read we need to wait for the connection close signal.
        try {
            connectionClose.await();
        } catch (InterruptedException ignored) {
        }
    } catch (IOException e) {
        webSocket.failWebSocket(e, null);
    } finally {
        closeQuietly(sink);
        closeQuietly(source);
    }
}
Also used : Response(okhttp3.Response) RealWebSocket(okhttp3.internal.ws.RealWebSocket) Request(okhttp3.Request) SecureRandom(java.security.SecureRandom) ByteString(okio.ByteString) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 35 with Headers

use of okhttp3.Headers in project okhttp by square.

the class CacheAdapterTest method get_httpGet.

@Test
public void get_httpGet() throws Exception {
    final URL serverUrl = configureServer(new MockResponse());
    assertEquals("http", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)69 Request (okhttp3.Request)58 Response (okhttp3.Response)53 Headers (okhttp3.Headers)46 IOException (java.io.IOException)34 MockResponse (okhttp3.mockwebserver.MockResponse)33 HttpHeaders (okhttp3.internal.http.HttpHeaders)29 ResponseBody (okhttp3.ResponseBody)27 RequestBody (okhttp3.RequestBody)21 List (java.util.List)19 MediaType (okhttp3.MediaType)16 HashMap (java.util.HashMap)15 Map (java.util.Map)15 ANResponse (com.androidnetworking.common.ANResponse)13 AnalyticsListener (com.androidnetworking.interfaces.AnalyticsListener)13 LinkedHashMap (java.util.LinkedHashMap)13 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)13 Buffer (okio.Buffer)12 ANError (com.androidnetworking.error.ANError)11 HttpURLConnection (java.net.HttpURLConnection)11