如何在php单元测试用例中设置自定义标头

时间:2016-10-13 08:48:01

标签: phpunit custom-headers

如何在PHPUnit测试用例中将令牌作为标题的一部分与请求一起发送。我尝试了很多方法,但没有一种方法不起作用。

1) const TOKEN = 'XXXXXX';
public function testCreateTest()
    {
      $response  = $this->call('POST','/test'.static::TOKEN,['code' => 'sally','sort_order'=>'test'],[],[], ['HTTP_Authorization' => 'token'],[]);
        $this->assertSame(500, $response->getStatusCode());
    }

2)public function testCreateTest()
    {
      $response  = $this->call('POST','/test',[], [], [],$headers = ['token' => static::TOKEN], $cookies = [], $files = [], $server = []);
        $this->assertSame(500, $response->getStatusCode());
    }

当我尝试打印请求时,不会附加标题。当我运行测试用例时,输出看起来像这样。

Cache-Control: no-cache
Content-Type:  application/json
Date:          Thu, 13 Oct 2016 07:38:25 GMT
Set-Cookie: XSRF-TOKEN=eyJpdiI6Ik4zdTZWbHZyXC8ybFV0MUs4R0UwNUl3PT0iLCJ2YWx1ZSI6IkFWb2cxUmtFdmljbXpxRkpcL2wwUHZyckNobVNrQYyI6IjdkY2FhMTliNjVmMDEwN2ZhNjI3ZjRmNDQxNDZmNmYyMTRkY2E4Yzg1NzY3MGY2OTEwNWY5MDA5ZmJlNDY3MDgifQ%3D%3D; expires=Thu, 13-Oct-2016 09:38:25 GMT; path=/

{"message":"Unauthorized"}

请有人帮帮我.. 提前谢谢。

0 个答案:

没有答案