public static HttpConnection connect(String requestMethod, URI uri, String contentType) { try { return new HttpConnection(requestMethod, uri.toURL(), contentType); } catch (MalformedURLException mue) { return null; } }
private HttpConnection postAndAssertNothingReadBeforeSettingBodyGenerator(CouchConfig config) throws Exception { HttpConnection conn = new HttpConnection("POST", config.getRootUri().toURL(), "application/json"); // nothing read from stream Assert.assertEquals(bis.available(), data.getBytes().length); conn.setRequestBody(new HttpConnection.InputStreamGenerator() { @Override public InputStream getInputStream() { return bis; } }); // This test invokes HttpConnection directly rather than via the CouchClient, so we need to // force the addition of some default interceptors conn.requestInterceptors.addAll(CouchClient.DEFAULT_REQUEST_INTERCEPTORS); return conn; }
@TestTemplate public void testReadBeforeExecute() throws Exception { HttpConnection conn = new HttpConnection("POST", new URL(dbResource.getDbURIWithUserInfo()), "application/json"); ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes()); // nothing read from stream assertEquals(data.getBytes().length, bis.available()); conn.setRequestBody(bis); try { String response = conn.responseAsString(); fail("IOException not thrown as expected instead had response " + response); } catch (IOException ioe) { ; // "Attempted to read response from server before calling execute()" } // stream was not read because execute() was not called assertEquals(data.getBytes().length, bis.available()); }
@TestTemplate public void testWriteToServerOk() throws Exception { HttpConnection conn = new HttpConnection("POST", new URL(dbResource.getDbURIWithUserInfo()), "application/json"); ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes()); // nothing read from stream assertEquals(data.getBytes().length, bis.available()); conn.setRequestBody(bis); HttpConnection response = conn.execute(); // Consume response stream String responseStr = response.responseAsString(); String okPattern = ".*\"ok\"\\s*:\\s*true.*"; assertTrue(Pattern.compile(okPattern, Pattern.DOTALL).matcher(responseStr).matches(), "There should be an ok response: " + "" + responseStr); // stream was read to end assertEquals(0, bis.available()); assertEquals(2, response.getConnection().getResponseCode () / 100, "Should be a 2XX response code"); }
@TestTemplate @RequiresCloudant public void testCookieAuthWithoutRetry() throws IOException { CookieInterceptor interceptor = new CookieInterceptor(CloudantClientHelper.COUCH_USERNAME, CloudantClientHelper.COUCH_PASSWORD, clientResource.get().getBaseUri().toString()); HttpConnection conn = new HttpConnection("POST", dbResource.get().getDBUri().toURL(), "application/json"); conn.responseInterceptors.add(interceptor); conn.requestInterceptors.add(interceptor); ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes()); // nothing read from stream assertEquals(data.getBytes().length, bis.available()); conn.setRequestBody(bis); HttpConnection responseConn = conn.execute(); // stream was read to end assertEquals(0, bis.available()); assertEquals(2, responseConn.getConnection().getResponseCode() / 100); //check the json Gson gson = new Gson(); InputStream is = responseConn.responseAsInputStream(); try { JsonObject response = gson.fromJson(new InputStreamReader(is), JsonObject.class); assertTrue(response.has("ok")); assertTrue(response.get("ok").getAsBoolean()); assertTrue(response.has("id")); assertTrue(response.has("rev")); } finally { is.close(); } }
+ ":" + CloudantClientHelper.COUCH_PASSWORD); HttpConnection conn = new HttpConnection("POST", dbResource.get().getDBUri().toURL(), "application/json"); conn.requestInterceptors.add(interceptor);