【问题标题】:Test basic auth测试基本身份验证
【发布时间】:2015-11-04 07:44:26
【问题描述】:

我想测试我的基本身份验证保护页面。 unauthorization 的测试工作正常。但是我在授权登录方面很挣扎,因为我不知道如何在测试中设置标题。

我找不到提示,如何在$this->call() 上设置标题。我能找到的唯一信息是:

$this->call($method, $uri, $parameters, $cookies, $files, $server, $content);

并且缺少标题。

如何在 laravel 上轻松测试基本身份验证。具体:如何设置测试请求的基本认证头?

我目前拥有的:

class ExampleTest extends TestCase {
    public function test401UnauthorizedOnMe() { 
        $response = $this->call('GET', '/api/me');
        $this->assertResponseStatus( 401);
    }

    public function testCorrectLoginOnMe() { 
        // http://shortrecipes.blogspot.de/2009/12/testing-basic-http-authentication-using.html
        //send header with correct user and password i.e.
        ////YWRtaW46YWRtaW4xMg== is equal to base64_encode( "admin:admin12")
        $this->request->setHeader( 'Authorization','Basic YWRtaW46YWRtaW4xMg==');
        $response = $this->call('GET', '/api/me');
        $this->assertResponseStatus(200);
    }
}

我尝试了$this->$request->setHeader();,但这样我只得到一个错误:

1) ExampleTest::testCorrectLoginOnMe
ErrorException: Undefined property: ExampleTest::$request

【问题讨论】:

  • 你试过 $this->call('GET', '/api/me',[],[],[], ['Authorization' => 'Basic YWRtaW46YWRtaW4xMg=='] ,[]); ?
  • 不起作用。我收到了Failed asserting that 401 matches expected 200

标签: php unit-testing laravel-5 phpunit basic-authentication


【解决方案1】:

使用HTTP authentication with PHP 找到了解决方案。这可以在$this->call()$server 参数中使用。

这是我的工作功能:

public function testCorrectLoginOnMe() {
    // call( $method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    $this->call('GET', '/api/me', [], [], [], ['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => 'admin12']);
    $this->assertResponseStatus( 200 );
}

【讨论】:

    【解决方案2】:

    基本身份验证通常通过标题键“授权”来实现。为方便起见,我的基本 TestCase 类中有以下方法:

    protected function withBasicAuth(User $user, $password = 'password'): self
    {
        return $this->withHeaders([
            'Authorization' => 'Basic '. base64_encode("{$user->email}:{$password}")
        ]);
    }
    

    然后在我的任何测试用例中,我都可以使用通过基本身份验证的用户运行 HTTP 测试,如下所示:

    $user = User::factory()->create();
    $this->withBasicAuth($user)
        ->get('/');
        ->assertStatus(Response::HTTP_OK);
    

    注意:出厂时创建的用户默认密码为“password”。

    【讨论】:

      【解决方案3】:
          $encoded_details = base64_encode('admin:admin12');
          $headers =  [
              'HTTP_Authorization' => 'Basic '. $encoded_details
          ];
          $response = $this->withHeaders($headers)->json('GET', '/api/me');
      

      只是另一个对我有用的解决方案

      【讨论】:

        【解决方案4】:
        protected function basicAuthenticate($detailsEncoded, $user, $password): self
        {
            $_SERVER['HTTP_AUTHORIZATION'] = $detailsEncoded;
            $_SERVER['PHP_AUTH_USER'] = $user;
            $_SERVER['PHP_AUTH_PW'] = $password;
        
            return $this;
        }
        
        
        $response = $this->basicAuthenticate($detailsEncoded, $user, $password)->get($url);
        $response->assertStatus(200);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-20
          • 2011-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-04
          相关资源
          最近更新 更多