Search in sources :

Example 76 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class JavaApiConverterTest method createOkResponseForCacheGet_withMissingStatusLine.

/** Test for https://code.google.com/p/android/issues/detail?id=160522 */
@Test
public void createOkResponseForCacheGet_withMissingStatusLine() throws Exception {
    URI uri = new URI("http://foo/bar");
    Request request = new Request.Builder().url(uri.toURL()).build();
    CacheResponse cacheResponse = new CacheResponse() {

        @Override
        public Map<String, List<String>> getHeaders() throws IOException {
            Map<String, List<String>> headers = new LinkedHashMap<>();
            // Headers is deliberately missing an entry with a null key.
            headers.put("xyzzy", Arrays.asList("bar", "baz"));
            return headers;
        }

        @Override
        public InputStream getBody() throws IOException {
            // Should never be called
            return null;
        }
    };
    try {
        JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
        fail();
    } catch (IOException expected) {
    }
}
Also used : CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) Request(okhttp3.Request) List(java.util.List) IOException(java.io.IOException) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 77 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class JavaApiConverterTest method createJavaUrlConnection_https_extraHttpsMethods.

@Test
public void createJavaUrlConnection_https_extraHttpsMethods() throws Exception {
    Request okRequest = createArbitraryOkRequest().newBuilder().get().url("https://secure/request").build();
    Handshake handshake = Handshake.get(null, CipherSuite.TLS_RSA_WITH_NULL_MD5, Arrays.<Certificate>asList(SERVER_CERT), Arrays.<Certificate>asList(LOCAL_CERT));
    Response okResponse = createArbitraryOkResponse(okRequest).newBuilder().handshake(handshake).build();
    HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse);
    assertEquals("SSL_RSA_WITH_NULL_MD5", httpsUrlConnection.getCipherSuite());
    assertEquals(SERVER_CERT.getSubjectX500Principal(), httpsUrlConnection.getPeerPrincipal());
    assertArrayEquals(new Certificate[] { LOCAL_CERT }, httpsUrlConnection.getLocalCertificates());
    assertArrayEquals(new Certificate[] { SERVER_CERT }, httpsUrlConnection.getServerCertificates());
    assertEquals(LOCAL_CERT.getSubjectX500Principal(), httpsUrlConnection.getLocalPrincipal());
}
Also used : CacheResponse(java.net.CacheResponse) Response(okhttp3.Response) SecureCacheResponse(java.net.SecureCacheResponse) Request(okhttp3.Request) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Handshake(okhttp3.Handshake) Test(org.junit.Test)

Example 78 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class JavaApiConverterTest method extractJavaHeaders.

@Test
public void extractJavaHeaders() throws Exception {
    Request okRequest = createArbitraryOkRequest().newBuilder().addHeader("key1", "value1_1").addHeader("key2", "value2").addHeader("key1", "value1_2").build();
    Map<String, List<String>> javaHeaders = JavaApiConverter.extractJavaHeaders(okRequest);
    assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
    assertEquals(Arrays.asList("value2"), javaHeaders.get("key2"));
}
Also used : Request(okhttp3.Request) List(java.util.List) Test(org.junit.Test)

Example 79 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class JavaApiConverterTest method createOkResponseForCacheGet.

@Test
public void createOkResponseForCacheGet() throws Exception {
    final String statusLine = "HTTP/1.1 200 Fantastic";
    URI uri = new URI("http://foo/bar");
    Request request = new Request.Builder().url(uri.toURL()).build();
    CacheResponse cacheResponse = new CacheResponse() {

        @Override
        public Map<String, List<String>> getHeaders() throws IOException {
            Map<String, List<String>> headers = new LinkedHashMap<>();
            headers.put(null, Collections.singletonList(statusLine));
            headers.put("xyzzy", Arrays.asList("bar", "baz"));
            return headers;
        }

        @Override
        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream("HelloWorld".getBytes(StandardCharsets.UTF_8));
        }
    };
    Response response = JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
    Request cacheRequest = response.request();
    assertEquals(request.url(), cacheRequest.url());
    assertEquals(request.method(), cacheRequest.method());
    assertEquals(0, request.headers().size());
    assertEquals(Protocol.HTTP_1_1, response.protocol());
    assertEquals(200, response.code());
    assertEquals("Fantastic", response.message());
    Headers okResponseHeaders = response.headers();
    assertEquals("baz", okResponseHeaders.get("xyzzy"));
    assertEquals("HelloWorld", response.body().string());
    assertNull(response.handshake());
}
Also used : CacheResponse(java.net.CacheResponse) Response(okhttp3.Response) SecureCacheResponse(java.net.SecureCacheResponse) CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Headers(okhttp3.Headers) Request(okhttp3.Request) List(java.util.List) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 80 with Request

use of com.tonyodev.fetch2.Request in project okhttp by square.

the class JavaApiConverter method createOkResponseForCacheGet.

/**
   * Creates an OkHttp {@link Response} using the supplied {@link Request} and {@link CacheResponse}
   * to supply the data.
   */
static Response createOkResponseForCacheGet(Request request, CacheResponse javaResponse) throws IOException {
    // Build a cache request for the response to use.
    Headers responseHeaders = createHeaders(javaResponse.getHeaders());
    Headers varyHeaders;
    if (HttpHeaders.hasVaryAll(responseHeaders)) {
        // "*" means that this will be treated as uncacheable anyway.
        varyHeaders = new Headers.Builder().build();
    } else {
        varyHeaders = HttpHeaders.varyHeaders(request.headers(), responseHeaders);
    }
    Request cacheRequest = new Request.Builder().url(request.url()).method(request.method(), null).headers(varyHeaders).build();
    Response.Builder okResponseBuilder = new Response.Builder();
    // Request: Use the cacheRequest we built.
    okResponseBuilder.request(cacheRequest);
    // Status line: Java has this as one of the headers.
    StatusLine statusLine = StatusLine.parse(extractStatusLine(javaResponse));
    okResponseBuilder.protocol(statusLine.protocol);
    okResponseBuilder.code(statusLine.code);
    okResponseBuilder.message(statusLine.message);
    // Response headers
    Headers okHeaders = extractOkHeaders(javaResponse, okResponseBuilder);
    okResponseBuilder.headers(okHeaders);
    // Response body
    ResponseBody okBody = createOkBody(okHeaders, javaResponse);
    okResponseBuilder.body(okBody);
    // Handle SSL handshake information as needed.
    if (javaResponse instanceof SecureCacheResponse) {
        SecureCacheResponse javaSecureCacheResponse = (SecureCacheResponse) javaResponse;
        // Handshake doesn't support null lists.
        List<Certificate> peerCertificates;
        try {
            peerCertificates = javaSecureCacheResponse.getServerCertificateChain();
        } catch (SSLPeerUnverifiedException e) {
            peerCertificates = Collections.emptyList();
        }
        List<Certificate> localCertificates = javaSecureCacheResponse.getLocalCertificateChain();
        if (localCertificates == null) {
            localCertificates = Collections.emptyList();
        }
        String cipherSuiteString = javaSecureCacheResponse.getCipherSuite();
        CipherSuite cipherSuite = CipherSuite.forJavaName(cipherSuiteString);
        Handshake handshake = Handshake.get(null, cipherSuite, peerCertificates, localCertificates);
        okResponseBuilder.handshake(handshake);
    }
    return okResponseBuilder.build();
}
Also used : SecureCacheResponse(java.net.SecureCacheResponse) HttpHeaders(okhttp3.internal.http.HttpHeaders) Headers(okhttp3.Headers) JavaNetHeaders(okhttp3.internal.JavaNetHeaders) CipherSuite(okhttp3.CipherSuite) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) CacheRequest(okhttp3.internal.cache.CacheRequest) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) CacheResponse(java.net.CacheResponse) Response(okhttp3.Response) SecureCacheResponse(java.net.SecureCacheResponse) StatusLine(okhttp3.internal.http.StatusLine) Certificate(java.security.cert.Certificate) Handshake(okhttp3.Handshake)

Aggregations

Request (okhttp3.Request)1617 Response (okhttp3.Response)1022 IOException (java.io.IOException)525 Test (org.junit.Test)407 OkHttpClient (okhttp3.OkHttpClient)331 RequestBody (okhttp3.RequestBody)256 Call (okhttp3.Call)239 ResponseBody (okhttp3.ResponseBody)189 HttpUrl (okhttp3.HttpUrl)146 Callback (okhttp3.Callback)109 Map (java.util.Map)85 File (java.io.File)77 InputStream (java.io.InputStream)77 JSONObject (org.json.JSONObject)76 MediaType (okhttp3.MediaType)75 Buffer (okio.Buffer)73 List (java.util.List)71 Headers (okhttp3.Headers)71 FormBody (okhttp3.FormBody)64 HashMap (java.util.HashMap)63