about summary refs log tree commit diff stats
path: root/classes
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-28 12:44:30 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-28 12:44:30 +0300
commit830ce716ae5658ff85ee85e55319896cbac0a878 (patch)
treeee728f3e794e1404a10b8edce63d741af3d8fb27 /classes
parent5b2f9dd12183f8de1498a988b8551438eae12e75 (diff)
downloadthe-epube-830ce716ae5658ff85ee85e55319896cbac0a878.tar.gz
the-epube-830ce716ae5658ff85ee85e55319896cbac0a878.tar.xz
add tests for router
Diffstat (limited to 'classes')
-rw-r--r--classes/Router.php30
1 files changed, 20 insertions, 10 deletions
diff --git a/classes/Router.php b/classes/Router.php
index bae01ec..d5d4349 100644
--- a/classes/Router.php
+++ b/classes/Router.php
@@ -18,25 +18,37 @@ class Router
 		$this->middleware[] = $middleware;
 	}
 
-	public function dispatch(string $op): Response
+	/**
+	 * Dispatch an operation and send the response.
+	 * Calls exit after sending the response.
+	 */
+	public function dispatch(string $op): void
+	{
+		$response = $this->dispatchResponse($op);
+		$response->send();
+		exit;
+	}
+
+	/**
+	 * Get the Response for a dispatch without sending it.
+	 * Used for testing and for cases where the caller
+	 * wants to modify the response before sending.
+	 */
+	public function dispatchResponse(string $op): Response
 	{
 		$handler = $this->routes[$op] ?? null;
 
 		if (!$handler) {
-			$response = (new Response())
+			return (new Response())
 				->withStatus(404)
 				->withBody(json_encode(["error" => "Method not found"]));
-			$response->send();
-			exit;
 		}
 
 		// Check auth for handlers that require it
 		if ($handler instanceof AuthRequired && empty($_SESSION["owner"])) {
-			$response = (new Response())
+			return (new Response())
 				->withStatus(401)
 				->withBody(json_encode(["error" => "Unauthorized"]));
-			$response->send();
-			exit;
 		}
 
 		$pipe = $handler->handle(...);
@@ -50,8 +62,6 @@ class Router
 		$request = new Request();
 		$request = $request->withAttribute('op', $op);
 
-		$response = $pipe($request);
-		$response->send();
-		exit;
+		return $pipe($request);
 	}
 }