Skip to content

Commit

Permalink
feature: send debug header
Browse files Browse the repository at this point in the history
closes #222
  • Loading branch information
g105b committed Oct 13, 2024
1 parent 8e25985 commit 4e9be59
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Response implements ResponseInterface {
use Message;
use MagicProp;

const DEBUG_LOCATION_HEADER = "X-Location-Sent-From";

/** @var null|callable */
private $exitCallback;
private Deferred $deferred;
Expand Down Expand Up @@ -99,7 +101,7 @@ public function setExitCallback(callable $callback):void {
}

public function reload():void {
$this->redirect($this->request?->getUri() ?? new Uri());
$this->redirect($this->request?->getUri() ?? new Uri("./"));
}

public function reloadWithoutQuery():void {
Expand All @@ -112,8 +114,22 @@ public function redirect(
string|UriInterface $uri,
int $statusCode = 303
):void {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$thisFile = __FILE__;
do {
$previousBacktrace = array_shift($backtrace);
$found = $previousBacktrace["file"] !== $thisFile;

Check failure on line 121 in src/Response.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Offset 'file' does not exist on array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: array, object?: object}.
}
while(!$found);

$cwd = getcwd();
$debugLocation = $previousBacktrace["file"];

Check failure on line 126 in src/Response.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Offset 'file' does not exist on array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: array, object?: object}.
$debugLocation = str_replace($cwd, "", $debugLocation);

Check failure on line 127 in src/Response.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Parameter #1 $search of function str_replace expects array|string, string|false given.
$debugLocation = trim($debugLocation, "/");
$debugLocation .= ":$previousBacktrace[line]";

Check failure on line 129 in src/Response.php

View workflow job for this annotation

GitHub Actions / phpstan (8.1)

Offset 'line' does not exist on array{function: string, line?: int, file?: string, class?: class-string, type?: '->'|'::', args?: array, object?: object}.
$this->statusCode = $statusCode;
$this->headers->set("Location", (string)$uri);
$this->headers->set(self::DEBUG_LOCATION_HEADER, $debugLocation);
if(isset($this->exitCallback)) {
call_user_func($this->exitCallback);
}
Expand Down
31 changes: 31 additions & 0 deletions test/phpunit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,35 @@ public function testAwaitText():void {
$actualText = $sut->awaitText();
self::assertSame($responseText, $actualText);
}

public function testRedirect_sendsFileLineDebug():void {
$sut = (new Response());
$sut->redirect("/somewhere");
$expectedLine = __LINE__ - 1;
$expectedFile = __FILE__;
$expectedFile = str_replace(getcwd(), "", $expectedFile);
$expectedFile = trim($expectedFile, "/");

$actualLocation = $sut->getHeaderLine("Location");
$actualDebugLocation = $sut->getHeaderLine(Response::DEBUG_LOCATION_HEADER);

self::assertSame("/somewhere", $actualLocation);
self::assertStringContainsString($expectedFile, $actualDebugLocation);
self::assertStringEndsWith($expectedLine, $actualDebugLocation);
}

public function testReload_sendsFileLineDebug():void {
$sut = (new Response());
$sut->reload();
$expectedLine = __LINE__ - 1;
$expectedFile = __FILE__;
$expectedFile = str_replace(getcwd(), "", $expectedFile);
$expectedFile = trim($expectedFile, "/");

$actualLocation = $sut->getHeaderLine("Location");
$actualDebugLocation = $sut->getHeaderLine(Response::DEBUG_LOCATION_HEADER);

self::assertSame("./", $actualLocation);
self::assertSame("$expectedFile:$expectedLine", $actualDebugLocation);
}
}

0 comments on commit 4e9be59

Please sign in to comment.