1414package com .cloudant .tests ;
1515
1616import static com .cloudant .tests .util .MockWebServerResources .EXPECTED_OK_COOKIE ;
17+ import static com .cloudant .tests .util .MockWebServerResources .EXPECTED_OK_COOKIE_2 ;
18+ import static com .cloudant .tests .util .MockWebServerResources .OK_COOKIE ;
19+ import static com .cloudant .tests .util .MockWebServerResources .OK_COOKIE_2 ;
1720import static org .hamcrest .CoreMatchers .anyOf ;
1821import static org .hamcrest .CoreMatchers .containsString ;
1922import static org .hamcrest .MatcherAssert .assertThat ;
@@ -262,7 +265,7 @@ public void testCookieAuthWithoutRetry() throws IOException {
262265 @ TestTemplate
263266 public void testCookieAuthWithPath () throws Exception {
264267 MockWebServer mockWebServer = new MockWebServer ();
265- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
268+ mockWebServer .enqueue (OK_COOKIE );
266269 mockWebServer .enqueue (MockWebServerResources .JSON_OK );
267270 CloudantClient client = ClientBuilder .url (mockWebServer .url ("/pathex" ).url ())
268271 .username ("user" )
@@ -331,7 +334,7 @@ public void cookieInterceptorURLEncoding() throws Exception {
331334 String mockPass = "?&=NotAsStrangeInAPassword" ;
332335
333336 //expect a cookie request then a GET
334- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
337+ mockWebServer .enqueue (OK_COOKIE );
335338 mockWebServer .enqueue (new MockResponse ());
336339
337340 CloudantClient c = CloudantClientHelper .newMockWebServerClientBuilder (mockWebServer )
@@ -369,7 +372,7 @@ public void cookieRenewal() throws Exception {
369372 // _session request to get Cookie
370373 // GET request -> 200 with a Set-Cookie
371374 // GET replay -> 200
372- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
375+ mockWebServer .enqueue (OK_COOKIE );
373376 mockWebServer .enqueue (new MockResponse ().setResponseCode (200 ).addHeader ("Set-Cookie" ,
374377 MockWebServerResources .authSessionCookie (renewalCookieToken , null ))
375378 .setBody (hello ));
@@ -478,7 +481,7 @@ public void handleNonExpiry403NullReason() throws Exception {
478481 */
479482 private void basic403Test (String error , String reason , int expectedRequests ) throws
480483 Exception {
481- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
484+ mockWebServer .enqueue (OK_COOKIE );
482485 JsonObject responseBody = new JsonObject ();
483486 responseBody .add ("error" , new JsonPrimitive (error ));
484487 JsonElement jsonReason ;
@@ -493,7 +496,7 @@ private void basic403Test(String error, String reason, int expectedRequests) thr
493496 }
494497 mockWebServer .enqueue (new MockResponse ().setResponseCode (403 ).setBody (responseBody
495498 .toString ()));
496- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
499+ mockWebServer .enqueue (OK_COOKIE );
497500 mockWebServer .enqueue (new MockResponse ());
498501
499502 CloudantClient c = CloudantClientHelper .newMockWebServerClientBuilder (mockWebServer )
@@ -667,10 +670,10 @@ private void testInputStreamRetry(HttpConnection request, byte[] expectedContent
667670 @ TestTemplate
668671 public void testCookieRenewOnPost () throws Exception {
669672
670- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
673+ mockWebServer .enqueue (OK_COOKIE );
671674 mockWebServer .enqueue (new MockResponse ().setResponseCode (403 ).setBody
672675 ("{\" error\" :\" credentials_expired\" , \" reason\" :\" Session expired\" }\r \n " ));
673- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
676+ mockWebServer .enqueue (OK_COOKIE );
674677 mockWebServer .enqueue (new MockResponse ());
675678
676679 CloudantClient c = CloudantClientHelper .newMockWebServerClientBuilder (mockWebServer )
@@ -976,7 +979,7 @@ public void execute() throws Throwable {
976979 */
977980 @ TestTemplate
978981 public void cookieAppliedToDifferentURL () throws Exception {
979- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
982+ mockWebServer .enqueue (OK_COOKIE );
980983 mockWebServer .enqueue (new MockResponse ().setBody ("first" ));
981984 mockWebServer .enqueue (new MockResponse ().setBody ("second" ));
982985
@@ -1023,54 +1026,26 @@ public void cookieAppliedToDifferentURL() throws Exception {
10231026 }
10241027
10251028 /**
1026- * Test that cookie authentication is stopped if the credentials were bad.
1029+ * Test that cookie authentication throws a CouchDbException if the credentials were bad.
10271030 *
10281031 * @throws Exception
10291032 */
10301033 @ TestTemplate
1031- public void badCredsDisablesCookie () throws Exception {
1032- mockWebServer .setDispatcher (new Dispatcher () {
1033- private int counter = 0 ;
1034-
1035- @ Override
1036- public MockResponse dispatch (RecordedRequest request ) throws InterruptedException {
1037- counter ++;
1038- // Return a 401 for the first _session request, after that return 200 OKs
1039- if (counter == 1 ) {
1040- return new MockResponse ().setResponseCode (401 );
1041- } else {
1042- return new MockResponse ().setBody ("TEST" );
1043- }
1044- }
1045- });
1034+ public void badCredsCookieThrows () {
1035+ mockWebServer .enqueue (new MockResponse ().setResponseCode (401 ));
10461036
10471037 CloudantClient c = CloudantClientHelper .newMockWebServerClientBuilder (mockWebServer )
10481038 .username ("bad" )
10491039 .password ("worse" )
10501040 .build ();
10511041
1052- String response = c .executeRequest (Http .GET (c .getBaseUri ())).responseAsString ();
1053- assertEquals ("TEST" , response , "The expected response body should be received" );
1042+ CouchDbException re =
1043+ assertThrows (CouchDbException .class ,
1044+ () -> c .executeRequest (Http .GET (c .getBaseUri ())).responseAsString (),
1045+ "Bad credentials should throw a CouchDbException." );
10541046
1055- // There should only be two requests: an initial auth failure followed by an ok response.
1056- // If the cookie interceptor keeps trying then there will be more _session requests.
1057- assertEquals (2 , mockWebServer .getRequestCount (), "There should be 2 requests" );
1058-
1059- assertEquals ("/_session" ,
1060- MockWebServerResources .takeRequestWithTimeout (mockWebServer ).getPath (), "The " +
1061- "first request should have been for a cookie" );
1062- assertEquals ("/" ,
1063- MockWebServerResources .takeRequestWithTimeout (mockWebServer ).getPath (), "The " +
1064- "second request should have been for /" );
1065-
1066- response = c .executeRequest (Http .GET (c .getBaseUri ())).responseAsString ();
1067- assertEquals ("TEST" , response , "The expected response body should be received" );
1068-
1069- // Make another request, the cookie interceptor should not try again so there should only be
1070- // one more request.
1071- assertEquals (3 , mockWebServer .getRequestCount (), "There should be 3 requests" );
1072- assertEquals ("/" , MockWebServerResources .takeRequestWithTimeout (mockWebServer ).getPath (),
1073- "The third request should have been for /" );
1047+ assertTrue (re .getMessage ().startsWith ("Credentials are incorrect for server" ), "The " +
1048+ "exception should have been for bad creds." );
10741049 }
10751050
10761051 /**
@@ -1087,7 +1062,7 @@ public void noErrorStream403() throws Exception {
10871062 public void execute () throws Throwable {
10881063
10891064 // Respond with a cookie init to the first request to _session
1090- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
1065+ mockWebServer .enqueue (OK_COOKIE );
10911066 // Respond to the executeRequest GET of / with a 403 with no body
10921067 mockWebServer .enqueue (new MockResponse ().setResponseCode (403 ));
10931068 CloudantClient c = CloudantClientHelper .newMockWebServerClientBuilder (mockWebServer )
@@ -1111,11 +1086,11 @@ public void execute() throws Throwable {
11111086 public void noErrorStream401 () throws Exception {
11121087
11131088 // Respond with a cookie init to the first request to _session
1114- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
1089+ mockWebServer .enqueue (OK_COOKIE );
11151090 // Respond to the executeRequest GET of / with a 401 with no body
11161091 mockWebServer .enqueue (new MockResponse ().setResponseCode (401 ));
11171092 // 401 triggers a renewal so respond with a new cookie for renewal request to _session
1118- mockWebServer .enqueue (MockWebServerResources . OK_COOKIE );
1093+ mockWebServer .enqueue (OK_COOKIE );
11191094 // Finally respond 200 OK with body of "TEST" to the replay of GET to /
11201095 mockWebServer .enqueue (new MockResponse ().setBody ("TEST" ));
11211096
0 commit comments