Skip to content

Commit e3e4cf5

Browse files
author
Greg Bowler
authored
feature: request factory creates uri with all parts (#183)
* feature: request factory creates uri with all parts * test: fix existing tests * stan: cast int properly
1 parent f0988f4 commit e3e4cf5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/RequestFactory.php

+20
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ public function createServerRequestFromGlobalState(
5252
$uri = new Uri($server["REQUEST_URI"] ?? null);
5353
$headers = new RequestHeaders();
5454

55+
if($secure = $server["HTTPS"] ?? null) {
56+
$uri = $uri->withScheme("https");
57+
}
58+
else {
59+
$uri = $uri->withScheme("http");
60+
}
61+
62+
if($port = $server["SERVER_PORT"] ?? null) {
63+
$uri = $uri->withPort((int)$port);
64+
}
65+
66+
if($host = $server["HTTP_HOST"] ?? null) {
67+
$host = strtok($host, ":");
68+
$uri = $uri->withHost($host);
69+
}
70+
71+
if($query = $server["QUERY_STRING"] ?? null) {
72+
$uri = $uri->withQuery($query);
73+
}
74+
5575
foreach($server as $key => $value) {
5676
if(str_starts_with($key, "HTTP_")) {
5777
$headerKey = substr($key, strlen("HTTP_"));

test/phpunit/RequestFactoryTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public function testCreateServerRequestFromGlobals_uri():void {
4848
self::assertEquals("/path/to/somewhere", $request->getUri()->getPath());
4949
}
5050

51+
public function testCreateServerRequestFromGlobals_uri_withAllParts():void {
52+
$sut = new RequestFactory();
53+
$request = $sut->createServerRequestFromGlobalState([
54+
"REQUEST_METHOD" => "POST",
55+
"REQUEST_URI" => "/path/to/somewhere/",
56+
"SERVER_PORT" => 8080,
57+
"HTTP_HOST" => "localhost:8080",
58+
"QUERY_STRING" => "example=123",
59+
], [], [], []);
60+
61+
self::assertSame(
62+
"http://localhost:8080/path/to/somewhere/?example=123",
63+
(string)$request->getUri()
64+
);
65+
}
66+
5167
public function testCreateServerRequestFromGlobals_header():void {
5268
$sut = new RequestFactory();
5369
$request = $sut->createServerRequestFromGlobalState([

0 commit comments

Comments
 (0)