Skip to content

Commit 42fba64

Browse files
committed
implements hello API on root path as well
1 parent 7ee56ac commit 42fba64

File tree

4 files changed

+75
-26
lines changed

4 files changed

+75
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [unreleased]
99

10+
### Added
11+
12+
- Hello API on `/` route.
13+
1014
## [3.16.2] - 2022-09-02
1115

1216
### Bug fixes

src/main/java/io/supertokens/webserver/Webserver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void start() {
154154
}
155155

156156
private void setupRoutes() throws Exception {
157-
addAPI(new NotFoundAPI(main));
157+
addAPI(new NotFoundOrHelloAPI(main));
158158
addAPI(new HelloAPI(main));
159159
addAPI(new SessionAPI(main));
160160
addAPI(new VerifySessionAPI(main));

src/main/java/io/supertokens/webserver/api/core/NotFoundAPI.java renamed to src/main/java/io/supertokens/webserver/api/core/NotFoundOrHelloAPI.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@
1818

1919
import io.supertokens.Main;
2020
import io.supertokens.output.Logging;
21+
import io.supertokens.pluginInterface.Storage;
22+
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
23+
import io.supertokens.storageLayer.StorageLayer;
2124
import io.supertokens.webserver.WebserverAPI;
2225

2326
import javax.servlet.http.HttpServletRequest;
2427
import javax.servlet.http.HttpServletResponse;
2528
import java.io.IOException;
2629

27-
public class NotFoundAPI extends WebserverAPI {
30+
public class NotFoundOrHelloAPI extends WebserverAPI {
2831

2932
private static final long serialVersionUID = 1L;
3033

31-
public NotFoundAPI(Main main) {
34+
public NotFoundOrHelloAPI(Main main) {
3235
super(main, "");
3336
}
3437

@@ -39,9 +42,20 @@ public String getPath() {
3942

4043
@Override
4144
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
42-
super.sendTextResponse(404, "Not found", resp);
43-
44-
Logging.error(main, "Unknown API called: " + req.getRequestURL(), false);
45+
if (req.getRequestURI().equals("/")) {
46+
Storage storage = StorageLayer.getStorage(main);
47+
try {
48+
storage.getKeyValue("Test");
49+
super.sendTextResponse(200, "Hello", resp);
50+
} catch (StorageQueryException e) {
51+
// we send 500 status code
52+
throw new IOException(e);
53+
}
54+
} else {
55+
super.sendTextResponse(404, "Not found", resp);
56+
57+
Logging.error(main, "Unknown API called: " + req.getRequestURL(), false);
58+
}
4559
}
4660

4761
}

src/test/java/io/supertokens/test/WebserverTest.java

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package io.supertokens.test;
1818

1919
import com.google.gson.JsonObject;
20-
import io.supertokens.config.Config;
2120
import io.supertokens.ProcessState;
2221
import io.supertokens.ProcessState.EventAndException;
2322
import io.supertokens.ProcessState.PROCESS_STATE;
23+
import io.supertokens.config.Config;
2424
import io.supertokens.exceptions.QuitProgramException;
2525
import io.supertokens.httpRequest.HttpRequest;
2626
import io.supertokens.httpRequest.HttpResponseException;
@@ -490,31 +490,62 @@ public void serverHelloWithoutDB() throws Exception {
490490
}
491491

492492
private void hello(String hostName, String port) throws InterruptedException, IOException, HttpResponseException {
493-
String[] args = { "../" };
494-
TestingProcess process = TestingProcessManager.start(args);
495-
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STARTED));
496-
try {
493+
{
494+
String[] args = { "../" };
495+
TestingProcess process = TestingProcessManager.start(args);
496+
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STARTED));
497+
try {
497498

498-
String response = HttpRequest.sendGETRequest(process.getProcess(), "",
499-
"http://" + hostName + ":" + port + "/hello", null, 1000, 1000, null);
500-
assertEquals("Hello", response);
499+
String response = HttpRequest.sendGETRequest(process.getProcess(), "",
500+
"http://" + hostName + ":" + port + "/hello", null, 1000, 1000, null);
501+
assertEquals("Hello", response);
501502

502-
response = HttpRequest.sendJsonPOSTRequest(process.getProcess(), "",
503-
"http://" + hostName + ":" + port + "/hello", null, 1000, 1000, null);
504-
assertEquals("Hello", response);
503+
response = HttpRequest.sendJsonPOSTRequest(process.getProcess(), "",
504+
"http://" + hostName + ":" + port + "/hello", null, 1000, 1000, null);
505+
assertEquals("Hello", response);
505506

506-
response = HttpRequest.sendJsonPUTRequest(process.getProcess(), "",
507-
"http://" + hostName + ":" + port + "/hello", null, 1000, 1000, null);
508-
assertEquals("Hello", response);
507+
response = HttpRequest.sendJsonPUTRequest(process.getProcess(), "",
508+
"http://" + hostName + ":" + port + "/hello", null, 1000, 1000, null);
509+
assertEquals("Hello", response);
509510

510-
response = HttpRequest.sendJsonDELETERequest(process.getProcess(), "",
511-
"http://" + hostName + ":" + port + "/hello", null, 1000, 1000, null);
512-
assertEquals("Hello", response);
511+
response = HttpRequest.sendJsonDELETERequest(process.getProcess(), "",
512+
"http://" + hostName + ":" + port + "/hello", null, 1000, 1000, null);
513+
assertEquals("Hello", response);
513514

514-
} finally {
515-
process.kill();
516-
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STOPPED));
515+
} finally {
516+
process.kill();
517+
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STOPPED));
518+
}
519+
}
520+
521+
{
522+
String[] args = { "../" };
523+
TestingProcess process = TestingProcessManager.start(args);
524+
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STARTED));
525+
try {
526+
527+
String response = HttpRequest.sendGETRequest(process.getProcess(), "",
528+
"http://" + hostName + ":" + port + "/", null, 1000, 1000, null);
529+
assertEquals("Hello", response);
530+
531+
response = HttpRequest.sendJsonPOSTRequest(process.getProcess(), "", "http://" + hostName + ":" + port,
532+
null, 1000, 1000, null);
533+
assertEquals("Hello", response);
534+
535+
response = HttpRequest.sendJsonPUTRequest(process.getProcess(), "",
536+
"http://" + hostName + ":" + port + "/", null, 1000, 1000, null);
537+
assertEquals("Hello", response);
538+
539+
response = HttpRequest.sendJsonDELETERequest(process.getProcess(), "",
540+
"http://" + hostName + ":" + port, null, 1000, 1000, null);
541+
assertEquals("Hello", response);
542+
543+
} finally {
544+
process.kill();
545+
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STOPPED));
546+
}
517547
}
548+
518549
}
519550

520551
@Test

0 commit comments

Comments
 (0)