4747
4848import static org .junit .Assert .assertEquals ;
4949import static org .junit .Assert .assertFalse ;
50+ import static org .junit .Assert .assertNotEquals ;
5051import static org .junit .Assert .assertNotNull ;
5152import static org .junit .Assert .assertNull ;
5253import static org .junit .Assert .assertTrue ;
@@ -74,7 +75,9 @@ public class IndexTest extends RobolectricTestCase {
7475 private static boolean didInitIndices = false ;
7576
7677 Client client ;
78+ Client clientWithLongApiKey ;
7779 Index index ;
80+ Index indexWithLongApiKey ;
7881 String indexName ;
7982
8083 List <JSONObject > objects ;
@@ -84,9 +87,11 @@ public class IndexTest extends RobolectricTestCase {
8487 public void setUp () throws Exception {
8588 super .setUp ();
8689 client = new Client (Helpers .app_id , Helpers .api_key );
90+ clientWithLongApiKey = new Client (Helpers .app_id , Helpers .getLongApiKey ());
8791 // WARNING: Robolectric cannot work with custom executors in `AsyncTask`, so we substitute the client's
8892 // executor with a Robolectric-compliant one.
8993 Whitebox .setInternalState (client , "searchExecutorService" , new RoboExecutorService ());
94+ Whitebox .setInternalState (clientWithLongApiKey , "searchExecutorService" , new RoboExecutorService ());
9095
9196 if (!didInitIndices ) {
9297 Index originalIndex = client .getIndex (originalIndexName );
@@ -119,6 +124,7 @@ public void setUp() throws Exception {
119124 objects = new ArrayList <>(originalObjects );
120125 indexName = originalIndexName + countIndices ++;
121126 index = client .getIndex (indexName );
127+ indexWithLongApiKey = clientWithLongApiKey .getIndex ("some_index_name" );
122128 }
123129
124130 @ Override
@@ -141,7 +147,8 @@ public void searchAsync(int waitTimeoutSeconds) throws Exception {
141147 final long begin = System .nanoTime ();
142148 // Search with query.
143149 index .searchAsync (new Query ("Francisco" ), new AssertCompletionHandler () {
144- @ Override public void doRequestCompleted (JSONObject content , AlgoliaException error ) {
150+ @ Override
151+ public void doRequestCompleted (JSONObject content , AlgoliaException error ) {
145152 if (error == null ) {
146153 assertEquals (1 , content .optInt ("nbHits" ));
147154 } else {
@@ -155,6 +162,22 @@ public void searchAsync(int waitTimeoutSeconds) throws Exception {
155162 assertTrue ("The test took longer than given timeout (" + elapsedMillis + " > " + waitTimeoutMillis + ")." , elapsedMillis <= waitTimeoutMillis );
156163 }
157164
165+ @ Test
166+ public void searchAsyncWithVeryLongApiKey () throws Exception {
167+ final long begin = System .nanoTime ();
168+ // Search with query.
169+ indexWithLongApiKey .searchAsync (new Query ("Francisco" ), new AssertCompletionHandler () {
170+ @ Override
171+ public void doRequestCompleted (JSONObject content , AlgoliaException error ) {
172+ assertNotEquals (494 , error .getStatusCode ());
173+ }
174+ });
175+
176+ final long elapsedMillis = (System .nanoTime () - begin ) / 1000000 ;
177+ final int waitTimeoutMillis = Helpers .wait * 1000 ;
178+ assertTrue ("The test took longer than given timeout (" + elapsedMillis + " > " + waitTimeoutMillis + ")." , elapsedMillis <= waitTimeoutMillis );
179+ }
180+
158181 @ Test
159182 public void searchDisjunctiveFacetingAsync () throws Exception {
160183 // Set index settings.
@@ -508,8 +531,7 @@ public void DNSTimeout() throws Exception {
508531 // Expect failed search after timeout
509532 final long startTime = System .nanoTime ();
510533 index .searchAsync (new Query (), new AssertCompletionHandler () {
511- @ Override
512- public void doRequestCompleted (JSONObject content , AlgoliaException error ) {
534+ @ Override public void doRequestCompleted (JSONObject content , AlgoliaException error ) {
513535 if (error != null ) {
514536 final long duration = (System .nanoTime () - startTime ) / 1000000 ;
515537 assertTrue ("We should hit 4 times the timeout before failing, but test took only " + duration + " ms." , duration > timeout * 4 );
0 commit comments