Looking to hire Laravel developers? Try LaraJobs

laravel-creates-http-requests maintained by wmjasonward

Description
Trait for creating http Request objects in test cases
Author
Last update
2021/03/11 21:56 (dev-main)
License
Links
Downloads
73

Comments
comments powered by Disqus

laravel-creates-http-requests

Trait for creating http Request objects in test cases, works in conjunction with Laravel's MakesHttpRequests trait.

Useful for directly testing a Laravel middleware handler.

Here's a contrived example of how to use it.


use Tests\TestCase;
use WmJasonWard\Laravel\Testing\CreatesHttpRequests;

class MyMiddlewareHandlerTest extends TestCase
{
    use CreatesHttpRequests;
    
    public function test_my_middleware_handler ()
    {
        $middleware = new MyMiddleware();

        $request = $this->createGetRequest('/', [
            'Authorization' => 'Basic dJdpbGlvOnBhc3N3c3Jk',
            ]);

        $r = $middleware->handle($request, function($response) {
            return null;
        });

        $this->assertNull($r);
    }
}

Using the Request class directly may suffice as well:

 use Illuminate\Http\Request;

 $request = Request::create('/');