Laravel Tutorials

Simplified HTTP Response Mocking in Laravel Tests

Published
Simplified HTTP Response Mocking in Laravel Tests image

Laravel streamlines testing HTTP interactions with its elegant shorthand syntax for HTTP response fakes. This approach substantially reduces code verbosity while making your test mocks more intuitive.

The basic implementation offers multiple response type shortcuts:

use Illuminate\Support\Facades\Http;

Http::fake([
    'google.com' => 'Hello World',
    'github.com' => ['foo' => 'bar'],
    'forge.laravel.com' => 204,
]);

This syntax works beautifully in comprehensive testing scenarios:

class ApiIntegrationTest extends TestCase
{
    public function test_service_communication()
    {
        Http::fake([
            // String responses
            'api.notifications.com/*' => 'Message sent',
            
            // Array responses (converted to JSON)
            'api.products.com/*' => [
                'products' => [
                    ['id' => 1, 'name' => 'Laptop'],
                    ['id' => 2, 'name' => 'Phone']
                ]
            ],
            
            // Status code responses
            'api.status.com/check' => 200,
            'api.deprecated.com/*' => 410,
            
            // Different response types for related endpoints
            'api.orders.com/active' => ['status' => 'processing'],
            'api.orders.com/error' => 400,
            'api.orders.com/message' => 'System unavailable'
        ]);

        // Test with assertions
        $response = Http::get('api.notifications.com/send');
        $this->assertEquals('Message sent', $response->body());

        $products = Http::get('api.products.com/list');
        $this->assertCount(2, $products['products']);

        $status = Http::get('api.status.com/check');
        $this->assertTrue($status->successful());
    }
}

This shorthand syntax significantly improves test maintainability by reducing the cognitive load when reading tests, allowing you to focus on the business logic rather than HTTP mocking details.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support image

PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support

Read article
Insert Middleware Before or After Existing Laravel Middleware image

Insert Middleware Before or After Existing Laravel Middleware

Read article
Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys image

Laravel Herd 2: Coding Agents, Workspaces, and Cloud Deploys

Read article
Strict Request Input Casting in Laravel image

Strict Request Input Casting in Laravel

Read article
Prevent Duplicate File Uploads With Laravel Storage Dedupler image

Prevent Duplicate File Uploads With Laravel Storage Dedupler

Read article
Slow Job Alerts and Replica Commit IDs in Laravel 13.40 image

Slow Job Alerts and Replica Commit IDs in Laravel 13.40

Read article