0

我有通过 docker 运行的 Symfony 应用程序。

它在 http://localhost:8000/ 上提供服务。我正在尝试测试我的 API 路由,例如检查它们在提供 JWT 时是否可用,检查状态代码等等。

这是我拥有的示例代码:

class SampleTest extends TestCase
{
    private $client;   
    
    public function setUp(): void
    {
        $this->client = (new HttpClient())::create();
    }
    
    public function testSampleTest(): void
    {
        $this->client->request('GET', 'http://localhost:8000/api/test-json');
    }
}

无论如何我得到这个错误:

Symfony\Component\HttpClient\Exception\TransportException: Couldn't connect to server for "http://localhost:8000/api/test-json".

正如这里提到的,Guzzle 无法向 localhost(端口:80、8000、8080 等)发出 GET 请求,因此无法向 localhost 发出请求。

但是当我在 docker 容器中有应用程序时,我该如何解决呢?Symfony 生态系统中是否有针对这种情况的标准解决方案?

4

0 回答 0