Search in sources :

Example 31 with TestTemplate

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);
}
Also used : HttpConnection(com.cloudant.http.HttpConnection) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 32 with TestTemplate

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");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 33 with TestTemplate

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");
}
Also used : HttpConnectionRequestInterceptor(com.cloudant.http.HttpConnectionRequestInterceptor) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) CloudantClient(com.cloudant.client.api.CloudantClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HttpConnectionInterceptorContext(com.cloudant.http.HttpConnectionInterceptorContext) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 34 with TestTemplate

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"));
}
Also used : CloudantClient(com.cloudant.client.api.CloudantClient) MockWebServer(okhttp3.mockwebserver.MockWebServer) TestTemplate(org.junit.jupiter.api.TestTemplate)

Example 35 with TestTemplate

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();
    }
}
Also used : BasicAuthInterceptor(com.cloudant.http.interceptors.BasicAuthInterceptor) InputStreamReader(java.io.InputStreamReader) HttpConnection(com.cloudant.http.HttpConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) TestTemplate(org.junit.jupiter.api.TestTemplate) RequiresCloudant(com.cloudant.test.main.RequiresCloudant)

Aggregations

TestTemplate (org.junit.jupiter.api.TestTemplate)51 CloudantClient (com.cloudant.client.api.CloudantClient)23 URI (java.net.URI)18 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)18 MockResponse (okhttp3.mockwebserver.MockResponse)16 HttpConnection (com.cloudant.http.HttpConnection)11 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 TreeMap (java.util.TreeMap)5 TestTimer (com.cloudant.tests.util.TestTimer)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 URL (java.net.URL)4 TooManyRequestsException (com.cloudant.client.org.lightcouch.TooManyRequestsException)3 Replay429Interceptor (com.cloudant.http.interceptors.Replay429Interceptor)3 CouchDbException (com.cloudant.client.org.lightcouch.CouchDbException)2 HttpConnectionInterceptorContext (com.cloudant.http.HttpConnectionInterceptorContext)2 RequiresCloudant (com.cloudant.test.main.RequiresCloudant)2 RequiresCloudantService (com.cloudant.test.main.RequiresCloudantService)2 Gson (com.google.gson.Gson)2 JsonObject (com.google.gson.JsonObject)2 IOException (java.io.IOException)2