Skip to content

Commit e5e5eba

Browse files
authored
Allow request IP retrieval during tests via server parameters (#7187)
* Enable retrieving the a mocked request IP during test execution by accessing server parameters This update allows retrieving the request IP address while running tests by enabling access to the REMOTE_ADDR server parameter. This enhancement is particularly useful for scenarios where the application logic relies on the client’s IP address, ensuring consistent behavior in both runtime and testing environments. By leveraging mock server parameters, developers can validate IP-dependent functionality during automated tests with greater accuracy and flexibility.
1 parent d703d28 commit e5e5eba

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Client.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public function initRequest(string $method, string $path, array $options = []):
192192
$body = new SwooleStream($content);
193193

194194
$request = new Psr7Request($method, $uri, $headers, $body);
195+
$request->setServerParams($this->getServerParams($method, $uri->getPath()));
195196

196197
return $request->withQueryParams($query)
197198
->withParsedBody($data)
@@ -285,4 +286,20 @@ protected function getStream(string $resource)
285286

286287
return $stream;
287288
}
289+
290+
protected function getServerParams(string $method, string $uri): array
291+
{
292+
return [
293+
'request_method' => $method,
294+
'request_uri' => $uri,
295+
'path_info' => $uri,
296+
'request_time' => time(),
297+
'request_time_float' => microtime(true),
298+
'server_protocol' => 'HTTP/1.1',
299+
'server_port' => 9501,
300+
'remote_port' => 40005,
301+
'remote_addr' => '127.0.0.1',
302+
'master_time' => time(),
303+
];
304+
}
288305
}

0 commit comments

Comments
 (0)