Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit ee4d294

Browse files
committed
start tests
1 parent 3e1ecc2 commit ee4d294

File tree

5 files changed

+318
-0
lines changed

5 files changed

+318
-0
lines changed

.github/workflows/bot.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Repository[bot]
2+
on:
3+
workflow_dispatch: {}
4+
label:
5+
types: [created, edited, deleted]
6+
issues:
7+
types: [opened, closed, reopened, edited]
8+
issue_comment:
9+
types: [created, edited]
10+
pull_request:
11+
branches: [main]
12+
types: [opened, closed, reopened, edited]
13+
release:
14+
types: [created, published]
15+
16+
jobs:
17+
repository-bot:
18+
name: Repository[bot]
19+
uses: Katsute/Workflows/.github/workflows/bot.yml@main
20+
secrets:
21+
token: ${{ secrets.BOT }}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.katsute.simplehttpserver;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static dev.katsute.simplehttpserver.ContextUtility.*;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
final class ContextUtilityTests {
9+
10+
@Test
11+
public final void testContexts(){
12+
assertEquals("", getContext("/", false, false));
13+
assertEquals("/", getContext("", true, false));
14+
assertEquals("/", getContext("", false, true ));
15+
assertEquals("/", getContext("", true, true ));
16+
assertEquals("a", getContext("a", false, false));
17+
assertEquals("/a", getContext("a", true, false));
18+
assertEquals("a/", getContext("a", false, true ));
19+
assertEquals("/a/", getContext("a", true, true ));
20+
assertEquals("testNone", getContext("/testNone/", false, false));
21+
assertEquals("/testLeading", getContext("testLeading", true, false));
22+
assertEquals("testTrailing/", getContext("testTrailing", false, true ));
23+
assertEquals("/testBoth/", getContext("testBoth", true, true ));
24+
assertEquals("testNoneBackSlash", getContext("\\testNoneBackSlash\\", false, false));
25+
assertEquals("/testBackSlash/", getContext("\\testBackSlash\\", true, true ));
26+
assertEquals("/testConsecutiveBackSlash/", getContext("\\\\testConsecutiveBackSlash\\\\", true, true ));
27+
assertEquals("/testConsecutiveForwardSlash/", getContext("//testConsecutiveForwardSlash//", true, true ));
28+
assertEquals("/testWhitespace/", getContext(" /testWhitespace/ ", true, true));
29+
assertEquals("/ testWhitespace /", getContext("/ testWhitespace /", true, true));
30+
assertEquals(" testWhitespace ", getContext("/ testWhitespace /", false, false));
31+
assertEquals("testWhitespace", getContext(" testWhitespace ", false, false));
32+
assertEquals("/testWhitespace/", getContext(" /testWhitespace/ ", true, true));
33+
}
34+
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.katsute.simplehttpserver;
2+
3+
import java.io.*;
4+
import java.net.HttpURLConnection;
5+
import java.nio.charset.StandardCharsets;
6+
7+
public class Requests {
8+
9+
public static String getBody(final HttpURLConnection conn){
10+
try(final BufferedReader IN = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))){
11+
String buffer;
12+
final StringBuilder OUT = new StringBuilder();
13+
while((buffer = IN.readLine()) != null)
14+
OUT.append(buffer);
15+
return OUT.toString();
16+
}catch(final IOException ignored){}
17+
return null;
18+
}
19+
20+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package dev.katsute.simplehttpserver.handler.file;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static dev.katsute.simplehttpserver.handler.file.ContextUtility.*;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
final class ContextUtilityTests {
9+
10+
@Test
11+
final void testContexts(){
12+
assertEquals("", getContext("/", false, false));
13+
assertEquals("/", getContext("", true, false));
14+
assertEquals("/", getContext("", false, true ));
15+
assertEquals("/", getContext("", true, true ));
16+
assertEquals("a", getContext("a", false, false));
17+
assertEquals("/a", getContext("a", true, false));
18+
assertEquals("a/", getContext("a", false, true ));
19+
assertEquals("/a/", getContext("a", true, true ));
20+
assertEquals("testNone", getContext("/testNone/", false, false));
21+
assertEquals("/testLeading", getContext("testLeading", true, false));
22+
assertEquals("testTrailing/", getContext("testTrailing", false, true ));
23+
assertEquals("/testBoth/", getContext("testBoth", true, true ));
24+
assertEquals("testNoneBackSlash", getContext("\\testNoneBackSlash\\", false, false));
25+
assertEquals("/testBackSlash/", getContext("\\testBackSlash\\", true, true ));
26+
assertEquals("/testConsecutiveBackSlash/", getContext("\\\\testConsecutiveBackSlash\\\\", true, true ));
27+
assertEquals("/testConsecutiveForwardSlash/", getContext("//testConsecutiveForwardSlash//", true, true ));
28+
assertEquals("/testWhitespace/", getContext(" /testWhitespace/ ", true, true));
29+
assertEquals("/ testWhitespace /", getContext("/ testWhitespace /", true, true));
30+
assertEquals(" testWhitespace ", getContext("/ testWhitespace /", false, false));
31+
assertEquals("testWhitespace", getContext(" testWhitespace ", false, false));
32+
assertEquals("/testWhitespace/", getContext(" /testWhitespace/ ", true, true));
33+
}
34+
35+
@Test
36+
final void testJoin(){
37+
assertEquals("testBlank", joinContexts(false, false, "testBlank", ""));
38+
assertEquals("/testBlank/", joinContexts(true, true, "testBlank", ""));
39+
assertEquals("testBlank", joinContexts(false, false, "", "testBlank"));
40+
assertEquals("/testBlank/", joinContexts(true, true, "", "testBlank"));
41+
assertEquals("", joinContexts(false, false, "", ""));
42+
assertEquals("/", joinContexts(true, true, "", ""));
43+
assertEquals("trailing/slash", joinContexts(false, false, "trailing/", "slash/"));
44+
assertEquals("/trailing/slash/", joinContexts(true, true, "trailing/", "slash/"));
45+
assertEquals("leading/slash", joinContexts(false, false, "leading/", "slash/"));
46+
assertEquals("/leading/slash/", joinContexts(true, true, "leading/", "slash/"));
47+
assertEquals("double/slash", joinContexts(false, false, "/double/", "/slash/"));
48+
assertEquals("/double/slash/", joinContexts(true, true, "/double/", "/slash/"));
49+
assertEquals("no/slash", joinContexts(false, false, "no", "slash"));
50+
assertEquals("/no/slash/", joinContexts(true, true, "no", "slash"));
51+
assertEquals("consecutive/slash", joinContexts(false, false, "//consecutive//", "//slash//"));
52+
assertEquals("/consecutive/slash/", joinContexts(true, true, "//consecutive//", "//slash//"));
53+
assertEquals("mixed/slash", joinContexts(false, false, "\\mixed\\", "//slash//"));
54+
assertEquals("/mixed/slash/", joinContexts(true, true, "\\mixed\\", "//slash//"));
55+
}
56+
57+
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package dev.katsute.simplehttpserver.server;
2+
3+
import com.sun.net.httpserver.HttpContext;
4+
import com.sun.net.httpserver.HttpExchange;
5+
import dev.katsute.simplehttpserver.*;
6+
import org.junit.jupiter.api.*;
7+
8+
import java.io.IOException;
9+
import java.net.BindException;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
13+
class ServerTests {
14+
15+
@Test
16+
final void testReference() throws IOException{
17+
assertNotNull(SimpleHttpServer.create().getHttpServer());
18+
}
19+
20+
@Test
21+
final void testProp() throws IOException{
22+
final SimpleHttpServer server = SimpleHttpServer.create();
23+
24+
assertNull(server.getExecutor());
25+
assertNull(server.getSessionHandler());
26+
assertTrue(server.getContexts().isEmpty());
27+
}
28+
29+
@Nested
30+
final class BindTests {
31+
32+
@Test
33+
final void testBind() throws IOException{
34+
final SimpleHttpServer server = SimpleHttpServer.create();
35+
assertNull(server.getAddress());
36+
assertDoesNotThrow(() -> server.bind(8080));
37+
assertTrue(server.getAddress().getAddress().isAnyLocalAddress());
38+
}
39+
40+
@Test
41+
final void testOccupied() throws IOException{
42+
final SimpleHttpServer server = SimpleHttpServer.create(8080);
43+
server.start();
44+
45+
Assertions.assertThrows(BindException.class, () -> SimpleHttpServer.create(8080));
46+
47+
server.stop();
48+
49+
Assertions.assertDoesNotThrow(() -> SimpleHttpServer.create(8080));
50+
}
51+
52+
}
53+
54+
@Nested
55+
final class CreateTests {
56+
57+
@SuppressWarnings("SpellCheckingInspection")
58+
@Test
59+
final void testHttpCreate() throws IOException{
60+
final SimpleHttpServer server = SimpleHttpServer.create();
61+
62+
assertThrows(IllegalStateException.class, server::start, "Unbinded server should throw an excaption");
63+
64+
server.bind(8080);
65+
assertEquals(8080, server.getAddress().getPort());
66+
67+
assertDoesNotThrow(server::start);
68+
assertThrows(IllegalStateException.class, server::start);
69+
70+
assertDoesNotThrow(() -> server.stop());
71+
assertDoesNotThrow(() -> server.stop(), "Second stop should not throw an exception");
72+
}
73+
74+
@SuppressWarnings("SpellCheckingInspection")
75+
@Test
76+
final void testHttpsCreate() throws IOException{
77+
final SimpleHttpsServer server = SimpleHttpsServer.create();
78+
79+
assertThrows(IllegalStateException.class, server::start, "Unbinded server should throw an excaption");
80+
81+
server.bind(8080);
82+
assertEquals(8080, server.getAddress().getPort());
83+
84+
assertDoesNotThrow(server::start);
85+
assertThrows(IllegalStateException.class, server::start);
86+
87+
assertDoesNotThrow(() -> server.stop());
88+
assertDoesNotThrow(() -> server.stop(), "Second stop should not throw an exception");
89+
}
90+
91+
}
92+
93+
@Nested
94+
final class ContextTests {
95+
96+
@Test
97+
final void testRandomContext() throws IOException{
98+
final SimpleHttpServer server = SimpleHttpServer.create();
99+
100+
final String test = server.getRandomContext();
101+
assertNotNull(test);
102+
server.createContext(test);
103+
for(int i = 0; i < 100; i++)
104+
assertNotEquals(test, server.getRandomContext());
105+
}
106+
107+
@Test
108+
final void testRandomContextHead() throws IOException{
109+
final SimpleHttpServer server = SimpleHttpServer.create();
110+
111+
final String test = server.getRandomContext("/head");
112+
assertNotNull(test);
113+
assertTrue(test.startsWith("/head/"));
114+
server.createContext(test);
115+
for(int i = 0; i < 100; i++)
116+
assertNotEquals(test, server.getRandomContext("/head"));
117+
}
118+
119+
@Test
120+
final void testRemoveNullContext() throws IOException{
121+
final SimpleHttpServer server = SimpleHttpServer.create();
122+
123+
assertThrows(NullPointerException.class, () -> server.removeContext((String) null));
124+
assertThrows(IllegalArgumentException.class, () -> server.removeContext((HttpContext) null));
125+
assertThrows(IllegalArgumentException.class, () -> server.removeContext(""));
126+
}
127+
128+
@Test
129+
final void testRemoveContext() throws IOException{
130+
final SimpleHttpServer server = SimpleHttpServer.create();
131+
132+
server.createContext("");
133+
assertDoesNotThrow(() -> server.removeContext(""));
134+
assertDoesNotThrow(() -> server.removeContext(server.createContext("")));
135+
}
136+
137+
@Test
138+
final void testRemoveNativeContext() throws IOException{
139+
final SimpleHttpServer server = SimpleHttpServer.create();
140+
141+
server.getHttpServer().createContext("/");
142+
assertDoesNotThrow(() -> server.removeContext(""));
143+
144+
assertDoesNotThrow(() -> server.removeContext(server.getHttpServer().createContext("/")));
145+
146+
server.getHttpServer().removeContext(server.createContext("/"));
147+
assertDoesNotThrow(() -> server.createContext("/"));
148+
}
149+
150+
@Test
151+
final void testCreateContext() throws IOException{
152+
final SimpleHttpServer server = SimpleHttpServer.create();
153+
154+
server.createContext("");
155+
assertEquals(1, server.getContexts().size());
156+
157+
final SimpleHttpHandler handler = SimpleHttpExchange::close;
158+
159+
assertSame(handler, server.getContextHandler(server.createContext("", handler)));
160+
assertEquals(2, server.getContexts().size());
161+
}
162+
163+
@Test
164+
final void testCreateSlashContext() throws IOException{
165+
final SimpleHttpServer server = SimpleHttpServer.create();
166+
167+
assertEquals("/", server.createContext("/").getPath());
168+
server.removeContext("/");
169+
assertEquals("/", server.createContext("\\").getPath());
170+
server.removeContext("/");
171+
assertEquals("/", server.createContext("").getPath());
172+
server.removeContext("/");
173+
}
174+
175+
@Test
176+
final void testDuplicateContext() throws IOException{
177+
final SimpleHttpServer server = SimpleHttpServer.create();
178+
179+
server.createContext("");
180+
server.createContext("", HttpExchange::close); // supposed to throw an exception, docs are invalid
181+
}
182+
183+
}
184+
185+
}

0 commit comments

Comments
 (0)