-
Notifications
You must be signed in to change notification settings - Fork 87
Testing APIs
If you wish to test your APIs, Integrated can assist you!
Please note that, temporarily, the following method calls are exclusive to the Laravel extension. This will change soon.
Make a GET request to the given URI.
$this->get('/some-endpoint')
->seeJson();
Make a POST request to the given URI, and pass through any optional parameters.
$this->post('/some-endpoint', ['name' => 'joe']);
Make a PUT request to the given URI, and pass through the applicable data.
$this->put('/some-endpoint', ['name' => 'joe']);
Make a PATCH request to the given URI, and pass through the applicable data.
$this->patch('/some-endpoint', ['name' => 'joe']);
Make a DELETE request to the given URI.
$this->delete('/posts/1');
Assert that the last response is JSON (and not HTML, a string, etc.).
$this->get('/api/posts')
->seeJson();
$this->get('/api/posts')
->seeStatusCode(200);
Ensure that the status code from the previous request matches what you provide.
Assert that the returned JSON from the last response is equal to the provided JSON or array. If an array is provided, Integrated will json_encode
it for you automatically to perform the comparison.
$this->get('/api/posts')
->seeJsonEquals(['title' => 'Foo', 'body' => 'Bar']);
$this->get('/api/posts')
->seeJsonContains(['title' => 'Foo']);
Assert that the returned JSON from the last response matches the provided array.
$this->withHeaders(['Accept' => 'application/json'])
->get('/api/posts');
Adds the given headers to the request. It will merge with any headers that have already been set. This has to be called before you make the request.
Trying to make a POST request, but keep getting a VerifyCsrfToken
error? While this may be resolved in a future release of Laravel 5, in the meantime, update app/Http/Middleware/VerifyCsrfToken.php
, like so:
public function handle($request, Closure $next)
{
if (app()->environment() == 'testing') {
return $next($request);
}
return parent::handle($request, $next);
}
That should do it!