From a33dc12a2e287f6e03695b465570948b488abde3 Mon Sep 17 00:00:00 2001 From: "tien.xuan.vo" Date: Mon, 23 Oct 2023 09:36:55 +0700 Subject: [PATCH] chore: add example for header matchers --- .../src/Service/HttpClientService.php | 2 +- .../consumer/tests/Service/MatchersTest.php | 4 ++- .../matchersConsumer-matchersProvider.json | 29 +++++++++++++++++-- example/matchers/provider/public/index.php | 7 ++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/example/matchers/consumer/src/Service/HttpClientService.php b/example/matchers/consumer/src/Service/HttpClientService.php index 8b857165..b3fb1a73 100644 --- a/example/matchers/consumer/src/Service/HttpClientService.php +++ b/example/matchers/consumer/src/Service/HttpClientService.php @@ -20,7 +20,7 @@ public function __construct(string $baseUri) public function sendRequest(): ResponseInterface { return $this->httpClient->get("{$this->baseUri}/matchers", [ - 'headers' => ['Accept' => 'application/json'], + 'headers' => ['Accept' => 'application/json', 'Theme' => 'light'], 'query' => 'pages=2&pages=3&locales[]=fr-BE&locales[]=ru-RU', 'http_errors' => false, ]); diff --git a/example/matchers/consumer/tests/Service/MatchersTest.php b/example/matchers/consumer/tests/Service/MatchersTest.php index 4cf76940..fa9f61e7 100644 --- a/example/matchers/consumer/tests/Service/MatchersTest.php +++ b/example/matchers/consumer/tests/Service/MatchersTest.php @@ -34,12 +34,14 @@ public function testGetMatchers(): void json_encode($this->matcher->regex(['en-US', 'en-AU'], '^[a-z]{2}-[A-Z]{2}$')), ], ]) - ->addHeader('Accept', 'application/json'); + ->addHeader('Accept', 'application/json') + ->addHeader('Theme', $this->matcher->regex('dark', 'light|dark')); // arrayContains, eachKey, eachValue matchers are not working with headers $response = new ProviderResponse(); $response ->setStatus($this->matcher->statusCode(HttpStatus::SERVER_ERROR, 512)) ->addHeader('Content-Type', 'application/json') + ->addHeader('X-Powered-By', $this->matcher->string('PHP')) ->setBody([ 'like' => $this->matcher->like(['key' => 'value']), 'likeNull' => $this->matcher->like(null), diff --git a/example/matchers/pacts/matchersConsumer-matchersProvider.json b/example/matchers/pacts/matchersConsumer-matchersProvider.json index 6f7b91bc..12478be0 100644 --- a/example/matchers/pacts/matchersConsumer-matchersProvider.json +++ b/example/matchers/pacts/matchersConsumer-matchersProvider.json @@ -15,10 +15,23 @@ "headers": { "Accept": [ "application/json" + ], + "Theme": [ + "dark" ] }, "matchingRules": { - "header": {}, + "header": { + "Theme": { + "combine": "AND", + "matchers": [ + { + "match": "regex", + "regex": "light|dark" + } + ] + } + }, "path": { "combine": "AND", "matchers": [ @@ -149,6 +162,9 @@ "headers": { "Content-Type": [ "application/json" + ], + "X-Powered-By": [ + "PHP" ] }, "matchingRules": { @@ -544,7 +560,16 @@ ] } }, - "header": {}, + "header": { + "X-Powered-By": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + } + }, "status": { "$": { "combine": "AND", diff --git a/example/matchers/provider/public/index.php b/example/matchers/provider/public/index.php index 03fac97f..b33364bf 100644 --- a/example/matchers/provider/public/index.php +++ b/example/matchers/provider/public/index.php @@ -79,7 +79,12 @@ return $response ->withHeader('Content-Type', 'application/json') - ->withStatus(503); + ->withStatus(503) + ->withHeader('X-Powered-By', [ + 'PHP', + 'Nginx', + 'Slim', + ]); }); $app->post('/pact-change-state', function (Request $request, Response $response) {