use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method inputStreamRetryGeneratorWithLength.
@TestTemplate
public void inputStreamRetryGeneratorWithLength() throws Exception {
HttpConnection request = Http.POST(mockWebServer.url("/").url(), "application/json");
byte[] content = "abcde".getBytes("UTF-8");
request.setRequestBody(new TestInputStreamGenerator(content), content.length);
testInputStreamRetry(request, content);
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method noErrorStream401.
/**
* Test that having no body and hence no error stream on a 401 response correctly results in a
* 401 cookie renewal without a NPE.
*
* @throws Exception
*/
@TestTemplate
public void noErrorStream401() throws Exception {
// Respond with a cookie init to the first request to _session
mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
// Respond to the executeRequest GET of / with a 401 with no body
mockWebServer.enqueue(new MockResponse().setResponseCode(401));
// 401 triggers a renewal so respond with a new cookie for renewal request to _session
mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
// Finally respond 200 OK with body of "TEST" to the replay of GET to /
mockWebServer.enqueue(new MockResponse().setBody("TEST"));
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username("a").password("b").build();
String response = c.executeRequest(Http.GET(c.getBaseUri())).responseAsString();
assertEquals("TEST", response, "The expected response body should be received");
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method testCustomHeader.
@TestTemplate
public void testCustomHeader() throws Exception {
mockWebServer.enqueue(new MockResponse());
final String headerName = "Test-Header";
final String headerValue = "testHeader";
CloudantClient client = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).interceptors(new HttpConnectionRequestInterceptor() {
@Override
public HttpConnectionInterceptorContext interceptRequest(HttpConnectionInterceptorContext context) {
context.connection.requestProperties.put(headerName, headerValue);
return context;
}
}).build();
client.getAllDbs();
assertEquals(1, mockWebServer.getRequestCount(), "There should have been 1 request");
RecordedRequest request = MockWebServerResources.takeRequestWithTimeout(mockWebServer);
assertNotNull(request, "The recorded request should not be null");
assertNotNull(request.getHeader(headerName), "The custom header should have been present");
assertEquals(headerValue, request.getHeader(headerName), "The custom header should have the specified value");
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method testCookieAuthWithPath.
@TestTemplate
public void testCookieAuthWithPath() throws Exception {
MockWebServer mockWebServer = new MockWebServer();
mockWebServer.enqueue(MockWebServerResources.OK_COOKIE);
mockWebServer.enqueue(MockWebServerResources.JSON_OK);
CloudantClient client = ClientBuilder.url(mockWebServer.url("/pathex").url()).username("user").password("password").build();
// send single request
client.database("test", true);
assertThat("_session is the second element of the path", mockWebServer.takeRequest().getPath(), is("/pathex/_session"));
}
use of org.junit.jupiter.api.TestTemplate in project java-cloudant by cloudant.
the class HttpTest method testBasicAuth.
/**
* Test that adding the Basic Authentication interceptor to HttpConnection
* will complete with a response code of 200. The response input stream
* is expected to hold the newly created document's id and rev.
*/
@TestTemplate
@RequiresCloudant
public void testBasicAuth() throws IOException {
BasicAuthInterceptor interceptor = new BasicAuthInterceptor(CloudantClientHelper.COUCH_USERNAME + ":" + CloudantClientHelper.COUCH_PASSWORD);
HttpConnection conn = new HttpConnection("POST", dbResource.get().getDBUri().toURL(), "application/json");
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();
}
}
Aggregations