I created a Middleware which should redirect if in the Url has a Parameter like 'redirect=http://example.de'. It is necessary that I use $_SERVER['QUERY_STRING'] to geht the Query. I am not allowed to use it from the $request Parameter. I wrote the Middleware and everything works fine. I failed at the Unit Testing, because I was not able to mock the request so that the $_SERVER['QUERY_STRING'] Variable is set. Long Story short: I don't get a Value in $_SERVER['QUERY_STRING'] when I mock the request. So I was searching for solutions and found this Question. I implemented this code like this:
public function testMyMiddleware(String $url) {
$redirect = new MyMiddleware();
$environment = Environment::mock([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => 'http://ip/middle?param=123&redirect=https%3A%2F%2Fgoogle.de%2F',
'QUERY_STRING'=> 'param=124&redirect=' . $url]
);
$request = Request::createFromEnvironment($environment);
$response = new Response();
$mock = function (ServerRequestInterface $req, ResponseInterface $res) {
return $req;
};
$response = $redirect($request, $response, $mock);
$this->assertSame($response->getHeader('Location'), [urldecode($url)]);
$this->assertSame($response->getStatusCode(), 302);
}
Unfortunately I still got the same Problem. Also found this, but it didn't help either.
Aucun commentaire:
Enregistrer un commentaire