Lumen-framework: Content empty on extended (child) Requests while testing

Created on 20 Dec 2017  路  7Comments  路  Source: laravel/lumen-framework

  • Lumen Version: 5.5.2
  • PHP Version: 7.0
  • Database Driver & Version:

Description:

While doing a request through curl or postman, extended Request (extending Illuminate Request) content is dynamically loaded in $request->getContent() method from the input stream. But in case of test case, its empty string. So, no content is loaded and request validation fails.

Steps To Reproduce:

  • Create a Request class (TestRequest) extending Illuminate\Http\Request
  • Add validation on TestRequest to expect name as request content field
  • Inject TestRequest in a controller method as argument
  • Add a route to this controller method
  • Create a test case where defined route is called passing content as json
    Ex: $this->json('GET', '/route', ['name' => 'test']);
  • Validation fails (even though name is passed as content)

Most helpful comment

same problem with me

here is my test functions body

public function testLogin()
    {

        $this->json('POST', 'v1/auth/login', [
            'login' => 'moeen-basra',
            'password' => 'secret',
        ])
            ->seeStatusCode(200)
            ->seeJson(['token' => '*']);
    }

in controller when I try to access $request->all(); is empty.

All 7 comments

I have faced the same issue. Looks like framework design problem.

@apaarmann Have you solved this?

Having same issue. curl call returns proper JSON response but API call returns info in the public $original as a class object and the public $content is '{}'

Main difference in the testing is the API call pulls from Eloquent DB entry and phptest pulls from Temporary Memory entries.

same problem with me

here is my test functions body

public function testLogin()
    {

        $this->json('POST', 'v1/auth/login', [
            'login' => 'moeen-basra',
            'password' => 'secret',
        ])
            ->seeStatusCode(200)
            ->seeJson(['token' => '*']);
    }

in controller when I try to access $request->all(); is empty.

Has anyone found a workaround for this?

My problem was I was using the magic $_GET to retrieve params in the controller. Changing to request->all() solved this.

class MyGetController extends Controller { public function myGetControllerFunction(Request $request) { //$my_params = $_GET; // doesn't work with unit-test $my_params = $request->all(); //this works ...

For anyone else that stumbles upon this... I found this in the Lumen docs:

Form requests are not supported by Lumen. If you would like to use form requests, you should use the full Laravel framework.

I was attempting to use a subclass of Request to handle authorization and validation, but apparently that doesn't work in lumen. Instead, it looks like they expect that to be in the controller using $this->validate() and $this->authorize().

Form requests are indeed not supported in Lumen. You should use the Validator class directly in your controller.

Was this page helpful?
0 / 5 - 0 ratings