Search in sources :

Example 86 with Response

use of org.openclinica.ns.response.v31.Response in project okhttp by square.

the class JavaApiConverterTest method createOkResponseForCacheGet_secure.

@Test
public void createOkResponseForCacheGet_secure() throws Exception {
    final String statusLine = "HTTP/1.1 200 Fantastic";
    final Principal localPrincipal = LOCAL_CERT.getSubjectX500Principal();
    final List<Certificate> localCertificates = Arrays.<Certificate>asList(LOCAL_CERT);
    final Principal serverPrincipal = SERVER_CERT.getSubjectX500Principal();
    final List<Certificate> serverCertificates = Arrays.<Certificate>asList(SERVER_CERT);
    URI uri = new URI("https://foo/bar");
    Request request = new Request.Builder().url(uri.toURL()).build();
    SecureCacheResponse cacheResponse = new SecureCacheResponse() {

        @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));
        }

        @Override
        public String getCipherSuite() {
            return "SSL_RSA_WITH_NULL_MD5";
        }

        @Override
        public List<Certificate> getLocalCertificateChain() {
            return localCertificates;
        }

        @Override
        public List<Certificate> getServerCertificateChain() throws SSLPeerUnverifiedException {
            return serverCertificates;
        }

        @Override
        public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
            return serverPrincipal;
        }

        @Override
        public Principal getLocalPrincipal() {
            return localPrincipal;
        }
    };
    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());
    Handshake handshake = response.handshake();
    assertNotNull(handshake);
    assertNotNullAndEquals(CipherSuite.TLS_RSA_WITH_NULL_MD5, handshake.cipherSuite());
    assertEquals(localPrincipal, handshake.localPrincipal());
    assertEquals(serverPrincipal, handshake.peerPrincipal());
    assertEquals(serverCertificates, handshake.peerCertificates());
    assertEquals(localCertificates, handshake.localCertificates());
}
Also used : SecureCacheResponse(java.net.SecureCacheResponse) Headers(okhttp3.Headers) Request(okhttp3.Request) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) CacheResponse(java.net.CacheResponse) Response(okhttp3.Response) SecureCacheResponse(java.net.SecureCacheResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) Principal(java.security.Principal) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Handshake(okhttp3.Handshake) Test(org.junit.Test)

Example 87 with Response

use of org.openclinica.ns.response.v31.Response in project okhttp by square.

the class JavaApiConverterTest method createJavaCacheResponse_httpGet.

@Test
public void createJavaCacheResponse_httpGet() throws Exception {
    Request okRequest = createArbitraryOkRequest().newBuilder().url("http://insecure/request").get().build();
    Response okResponse = createArbitraryOkResponse(okRequest).newBuilder().protocol(Protocol.HTTP_1_1).code(200).message("Fantastic").addHeader("key1", "value1_1").addHeader("key2", "value2").addHeader("key1", "value1_2").body(null).build();
    CacheResponse javaCacheResponse = JavaApiConverter.createJavaCacheResponse(okResponse);
    assertFalse(javaCacheResponse instanceof SecureCacheResponse);
    Map<String, List<String>> javaHeaders = javaCacheResponse.getHeaders();
    assertEquals(Arrays.asList("value1_1", "value1_2"), javaHeaders.get("key1"));
    assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), javaHeaders.get(null));
    assertNull(javaCacheResponse.getBody());
}
Also used : CacheResponse(java.net.CacheResponse) Response(okhttp3.Response) SecureCacheResponse(java.net.SecureCacheResponse) CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) Request(okhttp3.Request) List(java.util.List) Test(org.junit.Test)

Example 88 with Response

use of org.openclinica.ns.response.v31.Response 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 89 with Response

use of org.openclinica.ns.response.v31.Response in project okhttp by square.

the class JavaApiConverterTest method createJavaUrlConnection_requestChangesForbidden.

@Test
public void createJavaUrlConnection_requestChangesForbidden() throws Exception {
    Response okResponse = createArbitraryOkResponse();
    HttpURLConnection httpUrlConnection = JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse);
    // request.
    try {
        httpUrlConnection.setRequestProperty("key", "value");
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    try {
        httpUrlConnection.setFixedLengthStreamingMode(1234);
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    try {
        httpUrlConnection.setRequestMethod("PUT");
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    try {
        httpUrlConnection.getHeaderFields().put("key", Collections.singletonList("value"));
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    try {
        httpUrlConnection.getOutputStream();
        fail();
    } catch (UnsupportedOperationException expected) {
    }
}
Also used : CacheResponse(java.net.CacheResponse) Response(okhttp3.Response) SecureCacheResponse(java.net.SecureCacheResponse) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 90 with Response

use of org.openclinica.ns.response.v31.Response 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)

Aggregations

Response (okhttp3.Response)478 Request (okhttp3.Request)354 Test (org.junit.Test)213 IOException (java.io.IOException)177 Response (retrofit2.Response)156 ResponseBody (okhttp3.ResponseBody)137 ServiceResponse (com.microsoft.rest.ServiceResponse)114 Call (okhttp3.Call)104 Observable (rx.Observable)98 MockResponse (okhttp3.mockwebserver.MockResponse)76 RequestBody (okhttp3.RequestBody)71 OkHttpClient (okhttp3.OkHttpClient)67 Callback (okhttp3.Callback)44 List (java.util.List)39 TestClients.clientRequest (keywhiz.TestClients.clientRequest)37 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)32 MediaType (okhttp3.MediaType)27 HttpUrl (okhttp3.HttpUrl)26 Interceptor (okhttp3.Interceptor)26 ANResponse (com.androidnetworking.common.ANResponse)23