Reputation: 41
How can I mock an HTTP request in order to write some tests about the data or STH like this?
public function serve_request() {
// Check request method
if ( $_SERVER['REQUEST_METHOD'] !== 'GET' ) {
$this->send_response( 405, 'Invalid method: ' . $_SERVER['REQUEST_METHOD'] );
die;
}
For instance, how should I test the request method? I've been confused for a little time. I also tried to curl the HTTP request but i couldn't access the data. Any propositions is welcomed.
Upvotes: 4
Views: 1481
Reputation: 43700
In your test you can directly modify $_SERVER to be different values. So you then you would be able to test the different values. So you could do $_SERVER['REQUEST_METHOD'] = 'foo'
in your test before execution.
However because you have die
in this function, this will halt the execution of phpunit. In order to test this part of the function, the die
command would need to be moved to a different location. As it renders that part of the code untestable with phpunit.
Upvotes: 2