Test endpoint with custom HTTP method in laravel

Viewed 138

I am creating HTTP tests for an existing laravel application as described in the docs. The tests look something like this:

public function testSomething() 
{
    $this->get('/something')->assertOk();
}

The app has some endpoints that use the HTTP SEARCH method, declared like this:

Route::addRoute('SEARCH', '', 'SomeController@index');

However, the test class doesn't have a search('/something') method.

Is there any way to test a route with custom HTTP methods?

1 Answers

Laravel implement shortcut method for the most used http verb but not all. So if you look closer at the $this->get('...') it's implemented like that:
In \Illuminate\Foundation\Testing\Concerns\MakesHttpRequests

     public function get($uri, array $headers = [])
        {
            $server = $this->transformHeadersToServerVars($headers);
            $cookies = $this->prepareCookiesForRequest();
    
            return $this->call('GET', $uri, [], $cookies, [], $server);
        }

So for your use case, you can call

$this->call('SEARCH', '/something', [], $cookies, [], $server)->assertOk();
Related