gpt4 book ai didi

php - Pact for PHP - 来自模拟服务器的 404 响应代码

转载 作者:行者123 更新时间:2023-11-28 21:19:19 26 4
gpt4 key购买 nike

我尝试使用示例配置为 PHP 配置 Pact。我的问题是我可以运行 mockServer,但我发出的每个请求都会返回 404 响应。当然,我像在 GitHub 自述文件中一样设置了所有内容。不过,我知道服务器是可见的(本地主机配置),但无法注册路由。

代码示例:

class PactTest extends \Tests\BaseTestCases\V2TestCase
{

/** @var MockServerConfig */
private $config;

public function setUp()
{
// Create your basic configuration. The host and port will need to match
// whatever your Http Service will be using to access the providers data.
$this->config = new MockServerConfig();
$this->config->setHost('localhost');
$this->config->setPort(7200);
$this->config->setConsumer('someConsumer');
$this->config->setProvider('someProvider');
$this->config->setHealthCheckTimeout(60);
$this->config->setCors(true);

// Instantiate the mock server object with the config. This can be any
// instance of MockServerConfigInterface.
$server = new MockServer($this->config);

// Create the process.
$server->start();

// Stop the process.
$server->stop();
}

public function testSimple()
{
$matcher = new Matcher();

// Create your expected request from the consumer.
$request = new ConsumerRequest();
$request
->setMethod('GET')
->setPath('/test/abc')
->addHeader('Content-Type', 'application/json');

// Create your expected response from the provider.
$response = new ProviderResponse();
$response
->setStatus(200)
->addHeader('Content-Type', 'application/json;charset=utf-8')
->setBody([
'message' => $matcher->term('Hello, Bob', '(Hello, )[A-Za-z]')
]);

// Create a configuration that reflects the server that was started. You can
// create a custom MockServerConfigInterface if needed. This configuration
// is the same that is used via the PactTestListener and uses environment variables.
$builder = new InteractionBuilder($this->config);
$builder
->given('a thing exists')
->uponReceiving('a get request to /test/abc')
->with($request)
->willRespondWith($response); // This has to be last. This is what makes an API request to the Mock Server to set the interaction.

$service = new HttpClientService($this->config->getBaseUri()); // Pass in the URL to the Mock Server.
$result = $service->getTestAbc(); // Make the real API request against the Mock Server.

$builder->verify();

self::assertEquals('Hello, Bob', $result); // Make your assertions.
}

getTestAbc() 是:

public function getTestAbc(): string
{
$uri = $this->baseUri;
$response = $this->httpClient->get("{$uri->getHost()}/test/abc", [
'headers' => ['Content-Type' => 'application/json']
]);
$body = $response->getBody();
$object = \json_decode($body);

return $object->message;
}

我做错了什么?

最佳答案

您正在设置中停止模拟服务器。您应该在 tearDown 中的测试后停止服务器。我注意到这是手册中的代码,它可能具有误导性,但我认为它的目的是作为一个如何手动启动/停止模拟服务器的示例。

关于php - Pact for PHP - 来自模拟服务器的 404 响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53944082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com