JavaApiConverter { public static CacheResponse createJavaCacheResponse(final Response response) { final Headers headers = response.headers(); final ResponseBody body = response.body(); if (response.request().isHttps()) { final Handshake handshake = response.handshake(); return new SecureCacheResponse() { @Override public String getCipherSuite() { return handshake != null ? handshake.cipherSuite().javaName() : null; } @Override public List<Certificate> getLocalCertificateChain() { if (handshake == null) return null; List<Certificate> certificates = handshake.localCertificates(); return certificates.size() > 0 ? certificates : null; } @Override public List<Certificate> getServerCertificateChain() throws SSLPeerUnverifiedException { if (handshake == null) return null; List<Certificate> certificates = handshake.peerCertificates(); return certificates.size() > 0 ? certificates : null; } @Override public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { if (handshake == null) return null; return handshake.peerPrincipal(); } @Override public Principal getLocalPrincipal() { if (handshake == null) return null; return handshake.localPrincipal(); } @Override public Map<String, List<String>> getHeaders() throws IOException { return JavaNetHeaders.toMultimap(headers, StatusLine.get(response).toString()); } @Override public InputStream getBody() throws IOException { if (body == null) return null; return body.byteStream(); } }; } else { return new CacheResponse() { @Override public Map<String, List<String>> getHeaders() throws IOException { return JavaNetHeaders.toMultimap(headers, StatusLine.get(response).toString()); } @Override public InputStream getBody() throws IOException { if (body == null) return null; return body.byteStream(); } }; } } private  JavaApiConverter(); static Response createOkResponseForCachePut(URI uri, URLConnection urlConnection); static Request createOkRequest(
      URI uri, String requestMethod, Map<String, List<String>> requestHeaders); static CacheResponse createJavaCacheResponse(final Response response); static java.net.CacheRequest createJavaCacheRequest(final CacheRequest okCacheRequest);  }
@Test public void createJavaCacheResponse_httpGet() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .url("http: .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()); }
@Test public void createJavaCacheResponse_httpPost() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .url("http: .post(createRequestBody("RequestBody")) .build(); ResponseBody responseBody = createResponseBody("ResponseBody"); 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(responseBody) .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)); assertEquals("ResponseBody", readAll(javaCacheResponse.getBody())); }
@Test public void createJavaCacheResponse_httpsPost() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .url("https: .post(createRequestBody("RequestBody")) .build(); ResponseBody responseBody = createResponseBody("ResponseBody"); 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() .protocol(Protocol.HTTP_1_1) .code(200) .message("Fantastic") .addHeader("key1", "value1_1") .addHeader("key2", "value2") .addHeader("key1", "value1_2") .body(responseBody) .handshake(handshake) .build(); SecureCacheResponse javaCacheResponse = (SecureCacheResponse) JavaApiConverter.createJavaCacheResponse(okResponse); 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)); assertEquals("ResponseBody", readAll(javaCacheResponse.getBody())); assertEquals(handshake.cipherSuite().javaName(), javaCacheResponse.getCipherSuite()); assertEquals(handshake.localCertificates(), javaCacheResponse.getLocalCertificateChain()); assertEquals(handshake.peerCertificates(), javaCacheResponse.getServerCertificateChain()); assertEquals(handshake.localPrincipal(), javaCacheResponse.getLocalPrincipal()); assertEquals(handshake.peerPrincipal(), javaCacheResponse.getPeerPrincipal()); }