jeudi 1 octobre 2015

Passing Custom Header Type in Laravel Tests

I need to validate that my authentication is working properly, So I thought I would change a current test that looks for 401, instead of 302. What I wrote is:

public function it_should_not_let_you_access_the_company_end_point_when_not_logged_in() {
    $response = $this->get('api/v1/company/', [],[],[],['Content-Type' => 'application/json']);
    dd($response->response);
    $this->assertEquals('302', $response->getStatusCode());
}

The content type is json so the response type should be ajax, how ever:

Illuminate\Http\RedirectResponse {#1066
  #request: Illuminate\Http\Request {#963
    #json: null
    #userResolver: Closure {#945
      class: "Illuminate\Auth\AuthServiceProvider"
      this: Illuminate\Auth\AuthServiceProvider {#68 …}
      use: array:1 [

Is what I see in a dump of the response object.

The auth middle ware is set up as such:

public function handle($request, Closure $next)
{
    if ($this->auth->guest())
    {
        if ($request->ajax())
        {
            return response('Unauthorized.', 401);
        }

        return redirect()->guest('auth/login');
    }

    return $next($request);
}

But the $request->ajax() returns false. I need it to be true and to do the 401 instead.

Ideas?

Aucun commentaire:

Enregistrer un commentaire