1

我需要验证我的身份验证是否正常工作,所以我想我会更改当前的测试以查找 401,而不是 302。我写的是:

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());
}

内容类型是 json,所以响应类型应该是 ajax,但是:

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 [

是我在响应对象的转储中看到的。

auth 中间件是这样设置的:

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);
}

$request->ajax()返回错误。我需要它是真实的并改为执行 401。

想法?

4

0 回答 0