Skip to content

Commit

Permalink
Allow request IP retrieval during tests via server parameters (#7187)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
marcoskubis authored Dec 12, 2024
1 parent d703d28 commit e5e5eba
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public function initRequest(string $method, string $path, array $options = []):
$body = new SwooleStream($content);

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

return $request->withQueryParams($query)
->withParsedBody($data)
Expand Down Expand Up @@ -285,4 +286,20 @@ protected function getStream(string $resource)

return $stream;
}

protected function getServerParams(string $method, string $uri): array
{
return [
'request_method' => $method,
'request_uri' => $uri,
'path_info' => $uri,
'request_time' => time(),
'request_time_float' => microtime(true),
'server_protocol' => 'HTTP/1.1',
'server_port' => 9501,
'remote_port' => 40005,
'remote_addr' => '127.0.0.1',
'master_time' => time(),
];
}
}

0 comments on commit e5e5eba

Please sign in to comment.