@@ -1219,4 +1219,56 @@ public void doAction(HttpURLConnection con) throws IOException {
1219
1219
1220
1220
userInfoRequestConnection .disconnect ();
1221
1221
}
1222
+
1223
+ // Test that doesSessionExist returns true after access token expiry (with refresh)
1224
+ @ Test
1225
+ public void httpUrlConnection_testThatDoesSessionExistReturnsTrueAfterAccessTokenExpiry () throws Exception {
1226
+ com .example .TestUtils .startST (2 );
1227
+ new SuperTokens .Builder (context , Constants .apiDomain ).build ();
1228
+
1229
+ HttpURLConnection loginRequestConnection = SuperTokensHttpURLConnection .newRequest (new URL (loginAPIURL ), new SuperTokensHttpURLConnection .PreConnectCallback () {
1230
+ @ Override
1231
+ public void doAction (HttpURLConnection con ) throws IOException {
1232
+ con .setDoOutput (true );
1233
+ con .setRequestMethod ("POST" );
1234
+ con .setRequestProperty ("Accept" , "application/json" );
1235
+ con .setRequestProperty ("Content-Type" , "application/json" );
1236
+
1237
+ JsonObject bodyJson = new JsonObject ();
1238
+ bodyJson .addProperty ("userId" , Constants .userId );
1239
+
1240
+ OutputStream outputStream = con .getOutputStream ();
1241
+ outputStream .write (bodyJson .toString ().getBytes (StandardCharsets .UTF_8 ));
1242
+ outputStream .close ();
1243
+ }
1244
+ });
1245
+
1246
+ if (loginRequestConnection .getResponseCode () != 200 ) {
1247
+ throw new Exception ("Login request failed" );
1248
+ }
1249
+ loginRequestConnection .disconnect ();
1250
+
1251
+ if (!SuperTokens .doesSessionExist (context )) {
1252
+ throw new Exception ("Session should exist immediately after login" );
1253
+ }
1254
+
1255
+ Thread .sleep (3000 );
1256
+
1257
+ // Call doesSessionExist after access token expiry
1258
+ // This should trigger a refresh and still return true
1259
+ boolean sessionExists = SuperTokens .doesSessionExist (context );
1260
+
1261
+ if (!sessionExists ) {
1262
+ throw new Exception ("doesSessionExist should return true after access token expiry with successful refresh" );
1263
+ }
1264
+
1265
+ int refreshCount = com .example .TestUtils .getRefreshTokenCounter ();
1266
+ if (refreshCount != 1 ) {
1267
+ throw new Exception ("Expected refresh to be called 1 time but it was called " + refreshCount + " times" );
1268
+ }
1269
+
1270
+ if (!SuperTokens .doesSessionExist (context )) {
1271
+ throw new Exception ("Session should still exist after refresh" );
1272
+ }
1273
+ }
1222
1274
}
0 commit comments