Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 8中的功能测试使用withHeader设置主机无效?_Laravel_Symfony_Laravel 8_Symfony Http Foundation - Fatal编程技术网

Laravel 8中的功能测试使用withHeader设置主机无效?

Laravel 8中的功能测试使用withHeader设置主机无效?,laravel,symfony,laravel-8,symfony-http-foundation,Laravel,Symfony,Laravel 8,Symfony Http Foundation,由于我的项目部署了多个域名,因此需要测试的API接口使用API.example.test域名作为入口 在功能测试中使用$this->get('/v1/ping')将请求www.example.Test,我希望用header('Host',config('domain.api\u domain')设置$this->统一在设置中在ApiFeatureBaseTestCase中自动向API请求与API相关的测试。示例。测试进入 然而,在实践中,我发现这是无效的。通过跟踪代码,我发现了两个可能导致主机设

由于我的项目部署了多个域名,因此需要测试的API接口使用
API.example.test
域名作为入口

在功能测试中使用
$this->get('/v1/ping')
将请求
www.example.Test
,我希望用header('Host',config('domain.api\u domain')设置
$this->
统一在
设置中
ApiFeatureBaseTestCase中
自动向
API请求与API相关的测试。示例。测试
进入

然而,在实践中,我发现这是无效的。通过跟踪代码,我发现了两个可能导致主机设置无效的代码:

第一名(拉拉维尔):

在Laravel框架中编写代码,将参数$uri='/v1/ping'传递到$this->prepareUrlForRequest($uri)中,并获得默认主机的完整Url,返回值为http://www.example.test/v1/ping

第二名(Symfony):

在Symfony HttpFoundation组件的代码中,将首先使用
$uri
中的解析,即将出现的
主机
在标头中被覆盖为默认的
主机

上述两个代码最终导致
主机
I设置(由
withHeader
设置)失败。显然,在这个代码逻辑中,Symfony HttpFoundation组件在冲突中选择的主机不能被认为是错误的,但当我将这个问题提交给Laravel Framework时,这个问题已经解决了

我不知道这个问题是
bug
还是
功能

最后,我很抱歉我的问题打断了大家的时间,但如果对这个问题有结论,请告诉我什么更合适

我目前的方法是
$this->get($this->api\u base\u url./v1/ping')
,但我认为这并不优雅

第三季度!!一,

代码示例
//文件:config/domain.php
返回[
“api_域”=>“api.example.test”,
“web_域”=>“www.example.test”,
];
//文件:routes/demo.php
路由::域(配置('domain.api_domain'))
->中间件(['auth:api','api.sign.check'])
->名称空间($this->namespace)
->组(函数(){
路由::get('/v1/ping',函数(){
返回“thisapiv1”;
});
});
路由::域(配置('domain.web\u domain'))
->中间件(“web”)
->名称空间($this->namespace)
->组(base_path('routes/web.php');
//文件:tests/ApiFeatureBaseTestCase.php
名称空间测试;
类ApiFeatureBaseTestCase扩展了TestCase
{
受保护的函数设置():void
{
父::设置();
$this->withHeader('Host',config('domain.api_domain');
}
}
//文件:tests/Feature/ApiPingTest.php
名称空间测试\功能;
使用Tests\APEFeatureBaseTestCase;
类ApiPingTest扩展了ApiFeatureBaseTestCase
{
公共函数testPing()
{
$this->get('/v1/ping');
}
}

您能在
ApiFeatureBaseTestCase
类上创建包装器方法吗

public function get($uri, array $headers = [])
{
    return parent::get(config('domain.api_domain') . $uri, $headers);
}
然后在您的
ApiPingTest
课程中:

public function testPing()
{
    $this->get('/v1/ping');
}

谢谢这也是一个很好的建议,但是。。。我还需要定义其他函数,如
get
post
getJson
postJson
(如果我要使用它的话)