Laravel - HTTP Client - Inspecting Requests

When faking responses, you may occasionally wish to inspect the requests the client receives in order to make sure your application is sending the correct data or headers. You may accomplish this by calling the Http::assertSent method after calling Http::fake.

The assertSent method accepts a closure which will receive an Illuminate\Http\Client\Request instance and should return a boolean value indicating if the request matches your expectations. In order for the test to pass, at least one request must have been issued matching the given expectations:

    
    use Illuminate\Http\Client\Request;
    use Illuminate\Support\Facades\Http;
    
    Http::fake();
    
    Http::withHeaders([
        'X-First' => 'foo',
    ])->post('http://example.com/users', [
        'name' => 'Taylor',
        'role' => 'Developer',
    ]);
    
    Http::assertSent(function (Request $request) {
        return $request->hasHeader('X-First', 'foo') &&
               $request->url() == 'http://example.com/users' &&
               $request['name'] == 'Taylor' &&
               $request['role'] == 'Developer';
    });
	

If needed, you may assert that a specific request was not sent using the assertNotSent method:

    
    use Illuminate\Http\Client\Request;
    use Illuminate\Support\Facades\Http;
    
    Http::fake();
    
    Http::post('http://example.com/users', [
        'name' => 'Taylor',
        'role' => 'Developer',
    ]);
    
    Http::assertNotSent(function (Request $request) {
        return $request->url() === 'http://example.com/posts';
    });
	

Or, you may use the assertNothingSent method to assert that no requests were sent during the test:

    
    Http::fake();
    
    Http::assertNothingSent();