JavaApiConverter { static Response createOkResponseForCacheGet(Request request, CacheResponse javaResponse) throws IOException { Headers responseHeaders = createHeaders(javaResponse.getHeaders()); Headers varyHeaders; if (OkHeaders.hasVaryAll(responseHeaders)) { varyHeaders = new Headers.Builder().build(); } else { varyHeaders = OkHeaders.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(); okResponseBuilder.request(cacheRequest); StatusLine statusLine = StatusLine.parse(extractStatusLine(javaResponse)); okResponseBuilder.protocol(statusLine.protocol); okResponseBuilder.code(statusLine.code); okResponseBuilder.message(statusLine.message); Headers okHeaders = extractOkHeaders(javaResponse); okResponseBuilder.headers(okHeaders); ResponseBody okBody = createOkBody(okHeaders, javaResponse); okResponseBuilder.body(okBody); if (javaResponse instanceof SecureCacheResponse) { SecureCacheResponse javaSecureCacheResponse = (SecureCacheResponse) javaResponse; 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(); } 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 createOkResponseForCacheGet() throws Exception { final String statusLine = "HTTP/1.1 200 Fantastic"; URI uri = new URI("http: 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 HashMap<>(); 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()); }
@Test public void createOkResponseForCacheGet_withMissingStatusLine() throws Exception { URI uri = new URI("http: 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 HashMap<>(); headers.put("xyzzy", Arrays.asList("bar", "baz")); return headers; } @Override public InputStream getBody() throws IOException { return null; } }; try { JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse); fail(); } catch (IOException expected) { } }
@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: 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 HashMap<>(); 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()); }