如何在guzzle中添加HTTP_CTVFNTUSERNAME

时间:2017-07-20 15:37:26

标签: php phpunit guzzle

我正在尝试在PHPUnit中创建一个请求,但我不确定如何在请求中传递这些变量

use PHPUnit\Framework\TestCase;
use CCP\Models\Services;
use CCP\Models\ServicesUsers;
use GuzzleHttp\Client;

class ServicesTest extends TestCase {

    protected $id;
    protected $client;

    protected function setUp(){
        $this->client = new Client([
                'base_uri' => 'http://10.97.109.103:8080',
        ]);
    }

    public function testViewServiceUsers(){
        $response = $this->client->get('/api/user/services/owners/id/269/format/xml',[
                'headers' => [
                        'Accept' => 'application/xml',

                ]
        ]);
        $this->assertEquals(200, $response->getStatusCode());
        print_r($response->getBody()->getContents());
    }

这给我一个错误的应用程序错误,提到HTTP_CTVFNTUSERNAME缺失。

我可以看到php SERVER变量没有任何定义。

更新

$response = $this->client->get("/api/user/services/owners/id/{$id}/format/xml",[
                'headers' => [
                        'Accept' => 'application/xml',
                ],
                //'Authorization' => ['Basic '.$credentials]
                'CTVFNTDOMAIN'=>'vf-root',
                'CTVFAPPLICATIONACCESS'=>'ALL,EROOM,IDM,VF_USER,SURVEY',

                'CTVFNTUSERNAME' => 'UddinS2',
        ]);
        $this->assertEquals(200, $response->getStatusCode());
        $responseXml = \simplexml_load_string($response->getBody()->getContents(), "SimpleXMLElement", LIBXML_NOCDATA);
        print_r($_SERVER);
        $this->assertEquals('false', (string) $responseXml->header->error, "Error has been found");
        $this->assertEquals(1, (int) $responseXml->body->recordset->metadata->num_rows , "More than number of rows expected");
        $this->assertEquals($id, $responseXml->body->recordset->record->SERVICE_ID, "Record ID does not match");

$_SERVER的print_r没有显示我设置的任何HTTP_标头。

 Array
(
    [USER] => wwwrun
    [HOME] => /opt/SP/wwwrun/home
    [ORACLE_HOME] => /usr/lib/oracle/12.1/client64/lib
    [SCRIPT_NAME] => /index-api.php
    [REQUEST_URI] => /api/user/services/owners/id/4126/format/xml
    [QUERY_STRING] => url=user/services/owners/id/4126/format/xml
    [REQUEST_METHOD] => GET
    [SERVER_PROTOCOL] => HTTP/1.1
    [GATEWAY_INTERFACE] => CGI/1.1
    [REDIRECT_QUERY_STRING] => url=user/services/owners/id/4126/format/xml
    [REDIRECT_URL] => /api/user/services/owners/id/4126/format/xml
    [REMOTE_PORT] => 57159
    [SCRIPT_FILENAME] => /var/httpd/ccp-test/ccp-test.domain.com/docs/index-api.php
    [SERVER_ADMIN] => slswebserver@domain.com
    [CONTEXT_DOCUMENT_ROOT] => /var/SP/httpd/ccp-test/ccp-test.domain.com/docs
    [CONTEXT_PREFIX] =>
    [REQUEST_SCHEME] => http
    [DOCUMENT_ROOT] => /var/SP/httpd/ccp-test/ccp-test.domain.com/docs
    [REMOTE_ADDR] => x
    [SERVER_PORT] => 80
    [SERVER_ADDR] => x
    [SERVER_NAME] => ccp-test.domain.com
    [SERVER_SOFTWARE] => Apache
    [SERVER_SIGNATURE] =>
    [LD_LIBRARY_PATH] => /opt/SP/apache-2.4/lib
    [PATH] => /opt/SP/php7/bin:/opt/SP/php7/sbin:/opt/SP/php7/bin:/opt/SP/php7/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/networker/bin:/opt/networker/sbin:/opt/VRTSvcs/bin:/opt/VRTS/bin
    [HTTP_ACCEPT] => application/xml
    [HTTP_HOST] => x:8080
    [HTTP_USER_AGENT] => GuzzleHttp/6.2.1 curl/7.29.0 PHP/7.0.17
    [REDIRECT_STATUS] => 200
    [FCGI_ROLE] => RESPONDER
    [PHP_SELF] => /index-api.php
    [REQUEST_TIME_FLOAT] => 1500890812.0252
    [REQUEST_TIME] => 1500890812
    [argv] => Array
        (
            [0] => url=user/services/owners/id/4126/format/xml
        )

    [argc] => 1
)

1 个答案:

答案 0 :(得分:2)

您已经设置了Accept标题。只需添加任何遗漏的:

$this->client->get('/api/user/services/owners/id/269/format/xml', [
    'headers' => [
        'Accept' => 'application/xml',
        'CTVFNTUSERNAME' => 'some username',
    ]
]);

您的应用程序提到HTTP_CTVFNTUSERNAME,因为这就是PHP在$_SERVER全局变量中存储标题名称的方式。实际标头没有HTTP_前缀。