From 7844adb6f4c7424fa1bc0347d7d9eaf0662e2ca7 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Thu, 29 Nov 2018 21:24:24 +0100 Subject: [PATCH 1/2] add regex matcher for headers --- src/Context/RestContext.php | 31 +++++++++++++++++++++++++++++-- tests/features/rest.feature | 2 ++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Context/RestContext.php b/src/Context/RestContext.php index dd1d21f8..27f24078 100644 --- a/src/Context/RestContext.php +++ b/src/Context/RestContext.php @@ -2,10 +2,10 @@ namespace Behatch\Context; +use Behat\Gherkin\Node\PyStringNode; +use Behat\Gherkin\Node\TableNode; use Behat\Mink\Exception\ExpectationException; use Behatch\HttpCall\Request; -use Behat\Gherkin\Node\TableNode; -use Behat\Gherkin\Node\PyStringNode; class RestContext extends BaseContext { @@ -181,6 +181,33 @@ protected function theHeaderShouldExist($name) return $this->request->getHttpHeader($name); } + /** + * @Then the header :name should match :regex + */ + public function theHeaderShouldMatch($name, $regex) + { + $actual = $this->request->getHttpHeader($name); + + $this->assertEquals( + 1, + preg_match($regex, $actual), + "The header '$name' should match '$regex', but it is: '$actual'" + ); + } + + /** + * @Then the header :name should not match :regex + */ + public function theHeaderShouldNotMatch($name, $regex) + { + $this->not( + function () use ($name, $regex) { + $this->theHeaderShouldMatch($name, $regex); + }, + "The header '$name' should not match '$regex'" + ); + } + /** * Checks, that the response header expire is in the future * diff --git a/tests/features/rest.feature b/tests/features/rest.feature index 39d79538..0beeaa7c 100644 --- a/tests/features/rest.feature +++ b/tests/features/rest.feature @@ -6,6 +6,8 @@ Feature: Testing RESTContext And the header "Content-Type" should be equal to "text/html; charset=UTF-8" And the header "Content-Type" should not be equal to "x-test/no-such-type" And the header "Content-Type" should not contain "text/json" + And the header "Content-Type" should match "/^text\/html; [a-zA-Z=-]+/" + And the header "Content-Type" should not match "/^no-such-type$/" And the header "xxx" should not exist And the response should expire in the future And the response should be encoded in "UTF-8" From 7d33a8109d8eff2ffe6e25cc5a8f3260ee4d7e0e Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Fri, 30 Nov 2018 14:22:34 +0100 Subject: [PATCH 2/2] Use @ instead of / --- tests/features/rest.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/features/rest.feature b/tests/features/rest.feature index 0beeaa7c..c5897272 100644 --- a/tests/features/rest.feature +++ b/tests/features/rest.feature @@ -6,7 +6,7 @@ Feature: Testing RESTContext And the header "Content-Type" should be equal to "text/html; charset=UTF-8" And the header "Content-Type" should not be equal to "x-test/no-such-type" And the header "Content-Type" should not contain "text/json" - And the header "Content-Type" should match "/^text\/html; [a-zA-Z=-]+/" + And the header "Content-Type" should match "@^text/html; [a-zA-Z=-]+@" And the header "Content-Type" should not match "/^no-such-type$/" And the header "xxx" should not exist And the response should expire in the future