47
47
48
48
import static org .junit .Assert .assertEquals ;
49
49
import static org .junit .Assert .assertFalse ;
50
+ import static org .junit .Assert .assertNotEquals ;
50
51
import static org .junit .Assert .assertNotNull ;
51
52
import static org .junit .Assert .assertNull ;
52
53
import static org .junit .Assert .assertTrue ;
@@ -74,7 +75,9 @@ public class IndexTest extends RobolectricTestCase {
74
75
private static boolean didInitIndices = false ;
75
76
76
77
Client client ;
78
+ Client clientWithLongApiKey ;
77
79
Index index ;
80
+ Index indexWithLongApiKey ;
78
81
String indexName ;
79
82
80
83
List <JSONObject > objects ;
@@ -84,9 +87,11 @@ public class IndexTest extends RobolectricTestCase {
84
87
public void setUp () throws Exception {
85
88
super .setUp ();
86
89
client = new Client (Helpers .app_id , Helpers .api_key );
90
+ clientWithLongApiKey = new Client (Helpers .app_id , Helpers .getLongApiKey ());
87
91
// WARNING: Robolectric cannot work with custom executors in `AsyncTask`, so we substitute the client's
88
92
// executor with a Robolectric-compliant one.
89
93
Whitebox .setInternalState (client , "searchExecutorService" , new RoboExecutorService ());
94
+ Whitebox .setInternalState (clientWithLongApiKey , "searchExecutorService" , new RoboExecutorService ());
90
95
91
96
if (!didInitIndices ) {
92
97
Index originalIndex = client .getIndex (originalIndexName );
@@ -119,6 +124,7 @@ public void setUp() throws Exception {
119
124
objects = new ArrayList <>(originalObjects );
120
125
indexName = originalIndexName + countIndices ++;
121
126
index = client .getIndex (indexName );
127
+ indexWithLongApiKey = clientWithLongApiKey .getIndex ("some_index_name" );
122
128
}
123
129
124
130
@ Override
@@ -141,7 +147,8 @@ public void searchAsync(int waitTimeoutSeconds) throws Exception {
141
147
final long begin = System .nanoTime ();
142
148
// Search with query.
143
149
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 ) {
145
152
if (error == null ) {
146
153
assertEquals (1 , content .optInt ("nbHits" ));
147
154
} else {
@@ -155,6 +162,22 @@ public void searchAsync(int waitTimeoutSeconds) throws Exception {
155
162
assertTrue ("The test took longer than given timeout (" + elapsedMillis + " > " + waitTimeoutMillis + ")." , elapsedMillis <= waitTimeoutMillis );
156
163
}
157
164
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
+
158
181
@ Test
159
182
public void searchDisjunctiveFacetingAsync () throws Exception {
160
183
// Set index settings.
@@ -508,8 +531,7 @@ public void DNSTimeout() throws Exception {
508
531
// Expect failed search after timeout
509
532
final long startTime = System .nanoTime ();
510
533
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 ) {
513
535
if (error != null ) {
514
536
final long duration = (System .nanoTime () - startTime ) / 1000000 ;
515
537
assertTrue ("We should hit 4 times the timeout before failing, but test took only " + duration + " ms." , duration > timeout * 4 );
0 commit comments