|
| 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