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..c5897272 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"