JavaApiConverter { static HttpURLConnection createJavaUrlConnectionForCachePut(Response okResponse) { Request request = okResponse.request(); if (request.isHttps()) { return new CacheHttpsURLConnection(new CacheHttpURLConnection(okResponse)); } else { return new CacheHttpURLConnection(okResponse); } } 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 createJavaUrlConnection_requestChangesForbidden() throws Exception { Response okResponse = createArbitraryOkResponse(); HttpURLConnection httpUrlConnection = JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse); 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) { } }
@Test public void createJavaUrlConnection_connectionChangesForbidden() throws Exception { Response okResponse = createArbitraryOkResponse(); HttpURLConnection httpUrlConnection = JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse); try { httpUrlConnection.connect(); fail(); } catch (UnsupportedOperationException expected) { } try { httpUrlConnection.disconnect(); fail(); } catch (UnsupportedOperationException expected) { } }
@Test public void createJavaUrlConnection_responseChangesForbidden() throws Exception { Response okResponse = createArbitraryOkResponse(); HttpURLConnection httpUrlConnection = JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse); InputStream is = httpUrlConnection.getInputStream(); try { is.read(); fail(); } catch (UnsupportedOperationException expected) { } try { httpUrlConnection.getContent(); 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) { } }
@Test public void createJavaUrlConnection_responseHeadersOk() throws Exception { ResponseBody responseBody = createResponseBody("BodyText"); Response okResponse = new Response.Builder() .request(createArbitraryOkRequest()) .protocol(Protocol.HTTP_1_1) .code(200) .message("Fantastic") .addHeader("A", "c") .addHeader("B", "d") .addHeader("A", "e") .addHeader("Content-Length", Long.toString(responseBody.contentLength())) .body(responseBody) .build(); HttpURLConnection httpUrlConnection = JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse); assertEquals(200, httpUrlConnection.getResponseCode()); assertEquals("Fantastic", httpUrlConnection.getResponseMessage()); assertEquals(responseBody.contentLength(), httpUrlConnection.getContentLength()); assertEquals("HTTP/1.1 200 Fantastic", httpUrlConnection.getHeaderField(null)); assertEquals("e", httpUrlConnection.getHeaderField("A")); assertEquals("e", httpUrlConnection.getHeaderField("a")); Map<String, List<String>> responseHeaders = httpUrlConnection.getHeaderFields(); assertEquals(Arrays.asList("HTTP/1.1 200 Fantastic"), responseHeaders.get(null)); assertEquals(newSet("c", "e"), newSet(responseHeaders.get("A"))); assertEquals(newSet("c", "e"), newSet(responseHeaders.get("a"))); assertHeadersContainsMapping(responseHeaders, null, "HTTP/1.1 200 Fantastic"); assertHeadersContainsMapping(responseHeaders, "A", "c", "e"); assertHeadersContainsMapping(responseHeaders, "B", "d"); try { responseHeaders.put("N", Arrays.asList("o")); fail("Modified an unmodifiable view."); } catch (UnsupportedOperationException expected) { } try { responseHeaders.get("A").add("f"); fail("Modified an unmodifiable view."); } catch (UnsupportedOperationException expected) { } assertEquals(null, httpUrlConnection.getHeaderFieldKey(0)); assertEquals("HTTP/1.1 200 Fantastic", httpUrlConnection.getHeaderField(0)); int i = 1; while (!httpUrlConnection.getHeaderFieldKey(i).equals("A")) { i++; } assertResponseHeaderAtIndex(httpUrlConnection, i++, "A", "c"); assertResponseHeaderAtIndex(httpUrlConnection, i++, "B", "d"); assertResponseHeaderAtIndex(httpUrlConnection, i++, "A", "e"); while (httpUrlConnection.getHeaderField(i) != null) { assertNotNull(httpUrlConnection.getHeaderFieldKey(i)); i++; } assertNull(httpUrlConnection.getHeaderFieldKey(i)); }
@Test public void createJavaUrlConnection_accessibleRequestInfo_GET() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .get() .build(); Response okResponse = createArbitraryOkResponse(okRequest); HttpURLConnection httpUrlConnection = JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse); assertEquals("GET", httpUrlConnection.getRequestMethod()); assertTrue(httpUrlConnection.getDoInput()); assertFalse(httpUrlConnection.getDoOutput()); }
@Test public void createJavaUrlConnection_accessibleRequestInfo_POST() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .post(createRequestBody("PostBody")) .build(); Response okResponse = createArbitraryOkResponse(okRequest); HttpURLConnection httpUrlConnection = JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse); assertEquals("POST", httpUrlConnection.getRequestMethod()); assertTrue(httpUrlConnection.getDoInput()); assertTrue(httpUrlConnection.getDoOutput()); }
@Test public void createJavaUrlConnection_https_extraHttpsMethods() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .get() .url("https: .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()); }
@Test public void createJavaUrlConnection_https_forbiddenFields() throws Exception { Request okRequest = createArbitraryOkRequest().newBuilder() .url("https: .build(); Response okResponse = createArbitraryOkResponse(okRequest); HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) JavaApiConverter.createJavaUrlConnectionForCachePut(okResponse); try { httpsUrlConnection.getHostnameVerifier(); fail(); } catch (UnsupportedOperationException expected) { } try { httpsUrlConnection.getSSLSocketFactory(); fail(); } catch (UnsupportedOperationException expected) { } }