Add test coverage for core server components: annotation scanning, routing, rate limiting, CORS, and JSON handling
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package dev.coph.nextusweb.server.router;
|
||||
|
||||
import io.netty.handler.codec.http.HttpMethod;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class RouterTest {
|
||||
|
||||
private final Router.Handler noop = (req, res) -> {};
|
||||
|
||||
@Test
|
||||
void getRegistersAndResolvesExactPath() {
|
||||
Router r = new Router().get("/hello", noop);
|
||||
assertInstanceOf(Router.Resolution.Match.class, r.resolve(HttpMethod.GET, "/hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void postPutDeleteRegister() {
|
||||
Router r = new Router()
|
||||
.post("/p", noop)
|
||||
.put("/u", noop)
|
||||
.delete("/d", noop);
|
||||
assertInstanceOf(Router.Resolution.Match.class, r.resolve(HttpMethod.POST, "/p"));
|
||||
assertInstanceOf(Router.Resolution.Match.class, r.resolve(HttpMethod.PUT, "/u"));
|
||||
assertInstanceOf(Router.Resolution.Match.class, r.resolve(HttpMethod.DELETE, "/d"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void notFoundForUnknownPath() {
|
||||
Router r = new Router().get("/a", noop);
|
||||
assertInstanceOf(Router.Resolution.NotFound.class, r.resolve(HttpMethod.GET, "/x"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void methodNotAllowedWhenPathMatchesDifferentMethod() {
|
||||
Router r = new Router().get("/a", noop);
|
||||
Router.Resolution res = r.resolve(HttpMethod.POST, "/a");
|
||||
Router.Resolution.MethodNotAllowed mna = assertInstanceOf(Router.Resolution.MethodNotAllowed.class, res);
|
||||
assertTrue(mna.allowedMethods().contains(HttpMethod.GET));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathParamsAreExtracted() {
|
||||
Router r = new Router().get("/u/{id}", noop);
|
||||
Router.Resolution res = r.resolve(HttpMethod.GET, "/u/42");
|
||||
Router.Resolution.Match m = assertInstanceOf(Router.Resolution.Match.class, res);
|
||||
assertEquals("42", m.pathParams().get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardMatches() {
|
||||
Router r = new Router().get("/files/*", noop);
|
||||
assertInstanceOf(Router.Resolution.Match.class, r.resolve(HttpMethod.GET, "/files/anything"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void useAddsMiddlewareReturned() {
|
||||
AtomicInteger count = new AtomicInteger();
|
||||
Router r = new Router().use((req, res) -> count.incrementAndGet());
|
||||
assertEquals(1, r.middlewares().size());
|
||||
r.middlewares().getFirst().accept(null, null);
|
||||
assertEquals(1, count.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void registerWorksWithCustomMethod() {
|
||||
Router r = new Router().register(HttpMethod.valueOf("OPTIONS"), "/x", noop);
|
||||
assertInstanceOf(Router.Resolution.Match.class,
|
||||
r.resolve(HttpMethod.valueOf("OPTIONS"), "/x"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handlerInvocationWorks() throws Exception {
|
||||
AtomicInteger called = new AtomicInteger();
|
||||
Router r = new Router().get("/x", (req, res) -> called.incrementAndGet());
|
||||
var match = (Router.Resolution.Match) r.resolve(HttpMethod.GET, "/x");
|
||||
match.handler().handle(null, null);
|
||||
assertEquals(1, called.get());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user