Laravel TestCase not sending Authorization headers (JWT Token)

前端 未结 4 1080
故里飘歌
故里飘歌 2021-01-18 02:46

Summary

We are writing unit tests to test the creation and invalidation of JWT tokens and receiving a \"The token could not be parsed from the reque

4条回答
  •  星月不相逢
    2021-01-18 03:21

    There are two ways to send a token to our API, If header is not working simply append to your URL. Example:

     http://api.mysite.com/me?token={yourtokenhere}
    

    So you could do this:

    In your TestCase

    public function signIn($data=['email'=>'youremail@yahoo.com', 'password'=>'password'])
    {
        $this->post('/auth/signin', $data);
        $content = json_decode($this->response->getContent());
    
        $this->assertObjectHasAttribute('token', $content, 'Token does not exists');
        $this->token = $content->token;
    
        return $this;
     }
    

    This will grab and store a token in $this->token so you can use it to make a request

    Then later in your tests

    public function testResticted()
    {
        $this->signIn();
        $this->call('GET', '/restricted/page', ['token'=>$this->token], $cookies = [], $files = [], $server = []);
        dump($this->response);
     }
    

提交回复
热议问题