HTTP Tests

Laravel Test HTTP Tests

Laravel http test cheet sheet

Response Assertions

Assert Method Example
assertCookie $response->assertCookie($cookieName, $value = null);
assertCookieExpired $response->assertCookieExpired($cookieName);
assertCookieNotExpired $response->assertCookieNotExpired($cookieName);
assertCookieMissing $response->assertCookieMissing($cookieName);
assertCreated $response->assertCreated();
assertDontSee $response->assertDontSee($value, $escaped = true);
assertDontSeeText $response->assertDontSeeText($value, $escaped = true);
assertDownload $response->assertDownload();, $response->assertDownload('image.jpg');
assertExactJson $response->assertExactJson(array $data);
assertForbidden $response->assertForbidden();
assertHeader $response->assertHeader($headerName, $value = null);
assertHeaderMissing $response->assertHeaderMissing($headerName);
assertJson $response->assertJson(array $data, $strict = false);
assertJsonCount $response->assertJsonCount($count, $key = null);
assertJsonFragment $response->assertJsonFragment(['name' => 'Taylor Otwell']);
assertJsonMissing $response->assertJsonMissing(array $data);
assertJsonMissingExact $response->assertJsonMissingExact(array $data);
assertJsonMissingValidationErrors $response->assertJsonMissingValidationErrors($keys);
assertJsonPath $response->assertJsonPath($path, $expectedValue);, $response->assertJsonPath('user.name', 'Steve Schoger');
assertJsonMissingPath $response->assertJsonMissingPath($path);
assertJsonStructure $response->assertJsonStructure(array $structure);
assertJsonValidationErrors $response->assertJsonValidationErrors(array $data, $responseKey = 'errors');
assertJsonValidationErrorFor $response->assertJsonValidationErrorFor(string $key, $responseKey = 'errors');
assertLocation $response->assertLocation($uri);
assertContent $response->assertContent($value);
assertNoContent $response->assertNoContent($status = 204);
assertStreamedContent $response->assertStreamedContent($value);
assertNotFound $response->assertNotFound();
assertOk $response->assertOk();
assertPlainCookie $response->assertPlainCookie($cookieName, $value = null);
assertRedirect $response->assertRedirect($uri);
assertRedirectContains $response->assertRedirectContains($string);
assertRedirectToRoute $response->assertRedirectToRoute($name = null, $parameters = []);
assertRedirectToSignedRoute $response->assertRedirectToSignedRoute($name = null, $parameters = []);
assertSee $response->assertSee($value, $escaped = true);
assertSeeInOrder $response->assertSeeInOrder(array $values, $escaped = true);
assertSeeText $response->assertSeeText($value, $escaped = true);
assertSeeTextInOrder $response->assertSeeTextInOrder(array $values, $escaped = true);
assertSessionHas $response->assertSessionHas($key, $value = null);
assertSessionHasInput $response->assertSessionHasInput($key, $value = null);
assertSessionHasAll $response->assertSessionHasAll(array $data);
assertSessionHasErrors $response->assertSessionHasErrors(array $keys, $format = null, $errorBag = 'default');
assertSessionHasErrorsIn $response->assertSessionHasErrorsIn($errorBag, $keys = [], $format = null);
assertSessionHasNoErrors $response->assertSessionHasNoErrors();
assertSessionDoesntHaveErrors $response->assertSessionDoesntHaveErrors($keys = [], $format = null, $errorBag = 'default');
assertSessionMissing $response->assertSessionMissing($key);
assertStatus $response->assertStatus($code);
assertSuccessful $response->assertSuccessful();
assertUnauthorized $response->assertUnauthorized();
assertUnprocessable $response->assertUnprocessable();
assertValid $response->assertValid(['name', 'email']);
assertInvalid $response->assertInvalid(['name', 'email']);
assertViewHas $response->assertViewHas($key, $value = null);
assertViewHasAll $response->assertViewHasAll(array $data);
assertViewIs $response->assertViewIs($value);
assertViewMissing $response->assertViewMissing($key);

Authentication Assertions

Assert Method Example
assertAuthenticated $this->assertAuthenticated($guard = null);
assertGuest $this->assertGuest($guard = null);
assertAuthenticatedAs $this->assertAuthenticatedAs($user, $guard = null);

Customizing Request Headers

$response = $this->withHeaders([
    'X-Header' => 'Value',
])->post('/user', ['name' => 'Sally']);

$response->assertStatus(201);
$response = $this->withCookie('color', 'blue')->get('/');
 
$response = $this->withCookies([
    'color' => 'blue',
    'name' => 'Taylor',
])->get('/');

Session / Authentication

$response = $this->withSession(['banned' => false])->get('/');

$user = User::factory()->create();
 
$response = $this->actingAs($user)
    ->withSession(['banned' => false])
    ->get('/');

Debug

Debugging Responses

class ExampleTest extends TestCase
{
    public function test_basic_test()
    {
        $response = $this->get('/');
 
        $response->dumpHeaders();
        $response->dumpSession();
        $response->dump();
        $response->dd();
    }
}

Exception Handling

$response = $this->withoutExceptionHandling()->get('/');
$response = $this->withoutDeprecationHandling()->get('/');

Reference


Testing JSON APIs

Laravel Test HTTP Tests: Testing JSON APIs

Testing File Uploads

Laravel Test HTTP Tests: Testing File Uploads

Laravel Passport API Auth Test

Laravel Test HTTP Tests: Laravel Passport API Auth Test