Skip to content

Commit eff90a4

Browse files
author
Greg Bowler
authored
feature: reload and throw away query string (#178)
closes #177
1 parent 6d32a4c commit eff90a4

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/Response.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ public function setExitCallback(callable $callback):void {
2929
$this->exitCallback = $callback;
3030
}
3131

32-
public function reload():void {
32+
public function reload(bool $keepQuery = true):void {
3333
$uri = $this->request?->getUri() ?? new Uri();
34-
$this->redirect($uri->withPath("./"));
34+
$uri = $uri->withPath("./");
35+
if(!$keepQuery) {
36+
$uri = $uri->withQuery("");
37+
}
38+
$this->redirect($uri);
3539
}
3640

3741
public function redirect(string|UriInterface $uri, int $statusCode = 303):void {
3842
$this->statusCode = $statusCode;
39-
$this->headers->set("Location", $uri);
43+
$this->headers->set("Location", (string)$uri);
4044
if(isset($this->exitCallback)) {
4145
call_user_func($this->exitCallback);
4246
}

test/phpunit/ResponseTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
namespace Gt\Http\Test;
33

44
use Gt\Http\Header\ResponseHeaders;
5+
use Gt\Http\Request;
56
use Gt\Http\Response;
7+
use Gt\Http\StatusCode;
8+
use Gt\Http\Uri;
69
use PHPUnit\Framework\TestCase;
710

811
class ResponseTest extends TestCase {
@@ -55,4 +58,53 @@ public function testRedirect() {
5558
$sut->getHeaderLine("Location")
5659
);
5760
}
61+
62+
public function testReloadKeepsQuery() {
63+
$expectedRelativePath = "./?test=123";
64+
65+
$uri = self::createMock(Uri::class);
66+
$uri->expects(self::once())
67+
->method("withPath")
68+
->with("./")
69+
->willReturn($uri);
70+
$uri->expects(self::never())
71+
->method("withQuery");
72+
$uri->method("__toString")
73+
->willReturn($expectedRelativePath);
74+
75+
$request = self::createMock(Request::class);
76+
$request->method("getUri")
77+
->willReturn($uri);
78+
79+
$sut = new Response(200, request: $request);
80+
self::assertSame(StatusCode::OK, $sut->getStatusCode());
81+
$sut->reload();
82+
self::assertSame(StatusCode::SEE_OTHER, $sut->getStatusCode());
83+
self::assertSame($expectedRelativePath, $sut->getHeaderLine("Location"));
84+
}
85+
86+
public function testReloadWithoutKeepQuery() {
87+
$expectedRelativePath = "./";
88+
89+
$uri = self::createMock(Uri::class);
90+
$uri->expects(self::once())
91+
->method("withPath")
92+
->with("./")
93+
->willReturn($uri);
94+
$uri->expects(self::once())
95+
->method("withQuery")
96+
->with("")
97+
->willReturn($uri);
98+
$uri->method("__toString")
99+
->willReturn($expectedRelativePath);
100+
101+
$request = self::createMock(Request::class);
102+
$request->method("getUri")
103+
->willReturn($uri);
104+
105+
$sut = new Response(200, request: $request);
106+
$sut->reload(false);
107+
self::assertSame(StatusCode::SEE_OTHER, $sut->getStatusCode());
108+
self::assertSame($expectedRelativePath, $sut->getHeaderLine("Location"));
109+
}
58110
}

0 commit comments

Comments
 (0)