Skip to content

Commit

Permalink
fix: tests for the tempDirLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
tamassoltesz committed Oct 8, 2024
1 parent d40ca79 commit 925ad64
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" }
// }
//}

version = "9.2.2"
version = "9.2.3"


repositories {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/supertokens/webserver/Webserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.jetbrains.annotations.TestOnly;

import java.io.File;
import java.util.UUID;
Expand Down Expand Up @@ -364,6 +365,11 @@ public void closeLogger() {
}
}

@TestOnly
public TomcatReference getTomcatReference(){
return tomcatReference;
}

public static class TomcatReference {
private Tomcat tomcat;
private StandardContext context;
Expand All @@ -377,6 +383,11 @@ Tomcat getTomcat() {
return tomcat;
}

@TestOnly
public Tomcat getTomcatForTest(){
return tomcat;
}

StandardContext getContext() {
return context;
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/io/supertokens/test/CLIOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,32 @@ public void testMultipleInstancesAtTheSameTime() throws Exception {

}

@Test
public void cli2TempLocationTest() throws Exception {

String[] args = {"../"};

TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STARTED));

assertEquals(Config.getConfig(process.getProcess()).getHost(process.getProcess()), "localhost");
assertEquals(Config.getConfig(process.getProcess()).getPort(process.getProcess()), 3567);

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

//process starts with tempDirLocation param too.
args = new String[]{"../", "tempDirLocation=" + new File("../temp/").getAbsolutePath()};

process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STARTED));

assertEquals(Config.getConfig(process.getProcess()).getHost(process.getProcess()), "localhost");
assertEquals(Config.getConfig(process.getProcess()).getPort(process.getProcess()), 3567);

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

}

}
32 changes: 32 additions & 0 deletions src/test/java/io/supertokens/test/DotStartedFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,36 @@ public void processFailToStartDotStartedFileTest() throws Exception {
process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}

@Test
public void dotStartedFileAtTempDirLocation() throws Exception {
String tempDirLocation = new File("../temp/").getAbsolutePath();
String[] args = {"../", "tempDirLocation=" + tempDirLocation};

String host = "localhost";
String port = "8081";
String hostPortNameCheck = host + "-" + port;

Utils.setValueInConfig("port", port);

TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED));

File loc = new File(tempDirLocation + "/.started");

File[] dotStartedNameAndContent = loc.listFiles();
assert dotStartedNameAndContent != null;
assertEquals(1, dotStartedNameAndContent.length);
assertEquals(dotStartedNameAndContent[0].getName(), hostPortNameCheck);

String[] dotStartedContent = Files.readString(Paths.get(dotStartedNameAndContent[0].getPath())).split("\n");
String line = dotStartedContent[0];
assertEquals(line, Long.toString(ProcessHandle.current().pid()));
line = dotStartedContent.length > 1 ? dotStartedContent[1] : "";
assertEquals(line, Config.getConfig(process.main).getBasePath());
assertEquals(line, "");

process.kill();
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));
}
}
21 changes: 21 additions & 0 deletions src/test/java/io/supertokens/test/WebserverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.startup.Tomcat;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
Expand Down Expand Up @@ -963,6 +965,25 @@ public void validBasePath() throws InterruptedException, IOException, HttpRespon

}

@Test
public void tempDirLocationWebserverStarts() throws InterruptedException, HttpResponseException, IOException {
String tempDirLocation = new File("../temp/").getCanonicalPath();
String[] args = {"../", "tempDirLocation=" + tempDirLocation};
TestingProcess process = TestingProcessManager.start(args);
assertNotNull(process.checkOrWaitForEvent(PROCESS_STATE.STARTED));

Webserver.TomcatReference reference = Webserver.getInstance(process.getProcess()).getTomcatReference();
File catalinaBase = reference.getTomcatForTest().getServer().getCatalinaBase();
assertEquals(tempDirLocation, catalinaBase.getAbsolutePath());

String response = HttpRequest.sendGETRequest(process.getProcess(), "", "http://localhost:3567/", null,
1000, 1000, null);
assertEquals("Hello", response);

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

@Test
public void validBasePathWithEmptyHelloPath() throws InterruptedException, IOException, HttpResponseException {
{
Expand Down

0 comments on commit 925ad64

Please sign in to comment.