Api: When running WithoutMiddleware on unit tests NotFoundHttpExceptions are thrown on each request

Created on 11 Aug 2015  路  2Comments  路  Source: dingo/api

Using version 0.10.0 of dingo/api, I built up an API and unit tested all the endpoints and everything tested out ok. I then added in the the ['middleware' => 'api.auth'... part to my routes and hand checked the routes...again, everything checked out ok. My tests were throwing 401 errors because I didn't have authentication set up, which also worked as expected. At that point, I added in the WithoutMiddleware as directed from http://laravel.com/docs/5.1/testing#disabling-middleware to disable the middleware (see below).

use Illuminate\Foundation\Testing\WithoutMiddleware;

class BankTest extends TestCase {
    use WithoutMiddleware;

At that point, every endpoint called in my tests threw a \Symfony\Component\HttpKernel\Exception\NotFoundHttpException. Is there something I need to do to use WithoutMiddleware in my testing?

Most helpful comment

Ahhh...that makes sense. For what it's worth, I got around it by doing the following in my routes file:

$middleware = [ ];
if ( !App::runningUnitTests() ) {
    $middleware[] = 'api.auth';
}

$api->version( 'v1', function ( $api )  use ($middleware){
    $api->get( '/users/me',
        ['middleware' => $middleware,
        'uses' => '\App\Http\Controllers\UserController@currentUser'
     ]); // Get the current user
});

It seems to handle both the scenarios of unit testing and regular http requests.

All 2 comments

Dingo uses middleware to capture requests that target the API and handles them appropriately. So you kind of need to leave middleware enabled. Otherwise, like you've seen, you'll get 404's.

Ahhh...that makes sense. For what it's worth, I got around it by doing the following in my routes file:

$middleware = [ ];
if ( !App::runningUnitTests() ) {
    $middleware[] = 'api.auth';
}

$api->version( 'v1', function ( $api )  use ($middleware){
    $api->get( '/users/me',
        ['middleware' => $middleware,
        'uses' => '\App\Http\Controllers\UserController@currentUser'
     ]); // Get the current user
});

It seems to handle both the scenarios of unit testing and regular http requests.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pedrolari picture pedrolari  路  3Comments

cristiammercado picture cristiammercado  路  3Comments

adrian-fjellberg picture adrian-fjellberg  路  4Comments

BartHuis picture BartHuis  路  3Comments

HTMHell picture HTMHell  路  4Comments