use of org.openclinica.ns.response.v31.Response in project okhttp by square.
the class JavaApiConverterTest method createJavaUrlConnection_responseHeadersOk.
@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());
// Check retrieval by string key.
assertEquals("HTTP/1.1 200 Fantastic", httpUrlConnection.getHeaderField(null));
assertEquals("e", httpUrlConnection.getHeaderField("A"));
// The RI and OkHttp supports case-insensitive matching for this method.
assertEquals("e", httpUrlConnection.getHeaderField("a"));
// Check retrieval using a Map.
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")));
// OkHttp supports case-insensitive matching here. The RI does not.
assertEquals(newSet("c", "e"), newSet(responseHeaders.get("a")));
// Check the Map iterator contains the expected mappings.
assertHeadersContainsMapping(responseHeaders, null, "HTTP/1.1 200 Fantastic");
assertHeadersContainsMapping(responseHeaders, "A", "c", "e");
assertHeadersContainsMapping(responseHeaders, "B", "d");
// Check immutability of the headers Map.
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) {
}
// Check retrieval of headers by index.
assertEquals(null, httpUrlConnection.getHeaderFieldKey(0));
assertEquals("HTTP/1.1 200 Fantastic", httpUrlConnection.getHeaderField(0));
// After header zero there may be additional entries provided at the beginning or end by the
// implementation. It's probably important that the relative ordering of the headers is
// preserved, particularly if there are multiple value for the same key.
int i = 1;
while (!httpUrlConnection.getHeaderFieldKey(i).equals("A")) {
i++;
}
// Check the ordering of the headers set by app code.
assertResponseHeaderAtIndex(httpUrlConnection, i++, "A", "c");
assertResponseHeaderAtIndex(httpUrlConnection, i++, "B", "d");
assertResponseHeaderAtIndex(httpUrlConnection, i++, "A", "e");
// There may be some additional headers provided by the implementation.
while (httpUrlConnection.getHeaderField(i) != null) {
assertNotNull(httpUrlConnection.getHeaderFieldKey(i));
i++;
}
// Confirm the correct behavior when the index is out-of-range.
assertNull(httpUrlConnection.getHeaderFieldKey(i));
}
use of org.openclinica.ns.response.v31.Response in project okhttp by square.
the class JavaApiConverterTest method createJavaUrlConnection_connectionChangesForbidden.
@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) {
}
}
use of org.openclinica.ns.response.v31.Response in project okhttp by square.
the class JavaApiConverterTest method createJavaUrlConnection_accessibleRequestInfo_POST.
@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());
}
use of org.openclinica.ns.response.v31.Response in project okhttp by square.
the class JavaApiConverterTest method createJavaCacheResponse_httpsPost.
@Test
public void createJavaCacheResponse_httpsPost() throws Exception {
Request okRequest = createArbitraryOkRequest().newBuilder().url("https://secure/request").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());
}
use of org.openclinica.ns.response.v31.Response in project okhttp by square.
the class JavaApiConverterTest method createJavaUrlConnection_accessibleRequestInfo_GET.
@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());
}
Aggregations