Skip to content

Commit

Permalink
fix: more test
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Jul 8, 2024
1 parent 3a7beba commit 404edaf
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/main/java/io/supertokens/httpRequest/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,72 @@ public static <T> T sendGETRequest(Main main, String requestID, String url, Map<
}

@SuppressWarnings("unchecked")
public static <T> T sendGETRequestWithResponseHeaders(Main main, String requestID, String url, Map<String, String> params,
int connectionTimeoutMS, int readTimeoutMS, Integer version, Map<String, String> responseHeaders) throws IOException, HttpResponseException {
StringBuilder paramBuilder = new StringBuilder();

if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
paramBuilder.append(entry.getKey()).append("=")
.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)).append("&");
}
}
String paramsStr = paramBuilder.toString();
if (!paramsStr.equals("")) {
paramsStr = paramsStr.substring(0, paramsStr.length() - 1);
url = url + "?" + paramsStr;
}
URL obj = getURL(main, requestID, url);
InputStream inputStream = null;
HttpURLConnection con = null;

try {
con = (HttpURLConnection) obj.openConnection();
con.setConnectTimeout(connectionTimeoutMS);
con.setReadTimeout(readTimeoutMS);
if (version != null) {
con.setRequestProperty("api-version", version + "");
}

int responseCode = con.getResponseCode();

con.getHeaderFields().forEach((key, value) -> {
if (key != null) {
responseHeaders.put(key, value.get(0));
}
});

if (responseCode < STATUS_CODE_ERROR_THRESHOLD) {
inputStream = con.getInputStream();
} else {
inputStream = con.getErrorStream();
}

StringBuilder response = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(inputStream))) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
}
if (responseCode < STATUS_CODE_ERROR_THRESHOLD) {
if (!isJsonValid(response.toString())) {
return (T) response.toString();
}
return (T) (new JsonParser().parse(response.toString()));
}
throw new HttpResponseException(responseCode, response.toString());
} finally {
if (inputStream != null) {
inputStream.close();
}

if (con != null) {
con.disconnect();
}
}
}

private static <T> T sendJsonRequest(Main main, String requestID, String url, JsonElement requestBody,
int connectionTimeoutMS, int readTimeoutMS, Integer version, String method)
throws IOException, HttpResponseException {
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/io/supertokens/test/JWKSPublicAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -72,4 +74,56 @@ public void testSuccessOutput() throws Exception {
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void testCacheControlValue() throws Exception {
String[] args = { "../" };

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args, false);
Utils.setValueInConfig("access_token_dynamic_signing_key_update_interval", "1");
process.startProcess();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

// check regular output
Map<String, String> responseHeaders = new HashMap<>();
JsonObject response = HttpRequest.sendGETRequestWithResponseHeaders(process.getProcess(), "", "http://localhost:3567/.well-known/jwks.json", null,
1000, 1000, null, responseHeaders);

assertEquals(response.entrySet().size(), 1);

assertTrue(response.has("keys"));
JsonArray keys = response.get("keys").getAsJsonArray();
assertEquals(keys.size(), 2);

long maxAge = getMaxAgeValue(responseHeaders.get("Cache-Control"));
assertTrue(maxAge >= 3538 && maxAge <= 3540);

Thread.sleep(2000);

response = HttpRequest.sendGETRequestWithResponseHeaders(process.getProcess(), "", "http://localhost:3567/.well-known/jwks.json", null,
1000, 1000, null, responseHeaders);

assertEquals(response.entrySet().size(), 1);

assertTrue(response.has("keys"));
keys = response.get("keys").getAsJsonArray();
assertEquals(keys.size(), 2);

long newMaxAge = getMaxAgeValue(responseHeaders.get("Cache-Control"));
assertTrue(maxAge - newMaxAge >= 2 && maxAge - newMaxAge <= 3);

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

private static long getMaxAgeValue(String input) {
String pattern = "max-age=(\\d+)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);

if (m.find()) {
return Long.parseLong(m.group(1));
} else {
throw new IllegalArgumentException("No max-age found");
}
}
}

0 comments on commit 404edaf

Please sign in to comment.