diff --git a/MIGRATION.md b/MIGRATION.md index 3c3c9e6..2d5e932 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -37,6 +37,12 @@ A migration map of the step definitions available in v2 to v3. | `Then I :can visit :path with HTTP credentials :user :pass` | `Given the basic authentication with the username :username and the password :password` | | `When I visit :path then the final URL should be :alias` | Removed. Use `When I visit :path` and `Then the path should be :path` | |   | | +| **[`ResponseTrait`](src/ResponseTrait.php) ([example](tests/behat/features/response.feature))** | | +| `Then response contains header :name` | `Then the response should contain the header :header_name` | +| `Then response does not contain header :name` | `Then the response should not contain the header :name` | +| `Then response header :name contains :value` | `Then the response header :name should contain the value :value` | +| `Then response header :name does not contain :value` | `Then the response header :name should not contain the value :value` | +|   | | | **[`RoleTrait`](src/RoleTrait.php) ([example](tests/behat/features/role.feature))** | | | `Given role :name with permissions :permissions` | `Given the role :role with the permissions :permissions` | | `Given roles:` | `Given the following roles:` | diff --git a/README.md b/README.md index 1a6b4b5..0c121f0 100644 --- a/README.md +++ b/README.md @@ -192,10 +192,10 @@ For migration from v2 to v3, see [MIGRATION.md](MIGRATION.md). | `Given the basic authentication with the username :username and the password :password` | Set basic authentication for the current session. | |   | | | **[`ResponseTrait`](src/ResponseTrait.php) ([example](tests/behat/features/response.feature))** | | -| `Then response contains header :name` | Assert that the response contains a header with the specified name. | -| `Then response does not contain header :name` | Assert that the response does not contain a header with the specified name. | -| `Then response header :name contains :value` | Assert that the response header contains the specified value. | -| `Then response header :name does not contain :value` | Assert that the response header does not contain the specified value. | +| `Then the response should contain the header :header_name` | Assert that the response contains a header with the specified name. | +| `Then the response should not contain the header :name` | Assert that the response does not contain a header with the specified name. | +| `Then the response header :name should contain the value :value` | Assert that the response header contains the specified value. | +| `Then the response header :name should not contain the value :value` | Assert that the response header does not contain the specified value. | |   | | | **[`RoleTrait`](src/RoleTrait.php) ([example](tests/behat/features/role.feature))** | | | `Given the role :role with the permissions :permissions` | Create a single role with the specified permissions. | diff --git a/src/ResponseTrait.php b/src/ResponseTrait.php index a7fa8fb..f753613 100644 --- a/src/ResponseTrait.php +++ b/src/ResponseTrait.php @@ -17,62 +17,72 @@ trait ResponseTrait { * Assert that a response contains a header with specified name. * * @code - * Then response contains header "Connection" + * Then the response should contain the header "Connection" * @endcode * - * @Then response contains header :name + * @Then the response should contain the header :header_name */ - public function responseAssertContainsHeader(string $name): void { - $header = $this->getSession()->getResponseHeader($name); + public function responseAssertContainsHeader(string $header_name): void { + $header = $this->getSession()->getResponseHeader($header_name); if (!$header) { - throw new \Exception(sprintf('Response does not contain header %s', $name)); + throw new \Exception(sprintf('The response does not contain the header "%s".', $header_name)); } } /** - * Assert that a response does not contain a header with specified name. + * Assert that a response does not contain a header with a specified name. * * @code - * Then response does not contain header "Connection" + * Then the response should not contain the header "Connection" * @endcode * - * @Then response does not contain header :name + * @Then the response should not contain the header :header_name */ - public function responseAssertNotContainsHeader(string $name): void { - $header = $this->getSession()->getResponseHeader($name); + public function responseAssertNotContainsHeader(string $header_name): void { + $header = $this->getSession()->getResponseHeader($header_name); if ($header) { - throw new \Exception(sprintf('Response contains header %s, but should not', $name)); + throw new \Exception(sprintf('The response contains the header "%s", but should not.', $header_name)); } } /** - * Assert that a response contains a header with specified name and value. + * Assert that a response contains a header with a specified name and value. * * @code - * Then response header "Connection" contains "Keep-Alive" + * Then the response header "Connection" should contain the value "Keep-Alive" * @endcode * - * @Then response header :name contains :value + * @Then the response header :header_name should contain the value :header_value */ - public function responseAssertHeaderContains(string $name, string $value): void { - $this->responseAssertContainsHeader($name); - $this->assertSession()->responseHeaderContains($name, $value); + public function responseAssertHeaderContains(string $header_name, string $header_value): void { + $header = $this->getSession()->getResponseHeader($header_name); + + if (!$header) { + throw new \RuntimeException(sprintf('The response does not contain the header "%s".', $header_name)); + } + + $this->assertSession()->responseHeaderContains($header_name, $header_value); } /** - * Assert a response does not contain a header with specified name and value. + * Assert a response does not contain a header with a specified name and value. * * @code - * Then response header "Connection" does not contain "Keep-Alive" + * Then the response header "Connection" should not contain the value "Keep-Alive" * @endcode * - * @Then response header :name does not contain :value + * @Then the response header :header_name should not contain the value :header_value */ - public function responseAssertHeaderNotContains(string $name, string $value): void { - $this->responseAssertContainsHeader($name); - $this->assertSession()->responseHeaderNotContains($name, $value); + public function responseAssertHeaderNotContains(string $header_name, string $header_value): void { + $header = $this->getSession()->getResponseHeader($header_name); + + if (!$header) { + throw new \RuntimeException(sprintf('The response does not contain the header "%s".', $header_name)); + } + + $this->assertSession()->responseHeaderNotContains($header_name, $header_value); } } diff --git a/tests/behat/features/response.feature b/tests/behat/features/response.feature index 9743fad..de2599a 100644 --- a/tests/behat/features/response.feature +++ b/tests/behat/features/response.feature @@ -1,8 +1,101 @@ Feature: Check that ResponseTrait works - Scenario: Assert header response + Scenario: Assert "Then the response should contain the header :header_name" works Given I go to "/" - And response contains header "Content-Type" - And response does not contain header "NotExistingHeader" - And response header "Content-Type" contains "text/html; charset=utf-8" - And response header "Content-Type" does not contain "NotExistingHeaderValue" + Then the response should contain the header "Content-Type" + + @trait:ResponseTrait + Scenario: Assert that negative assertion for "Then the response should contain the header :header_name" fails with an error + Given some behat configuration + And scenario steps: + """ + Given I go to "/" + Then the response should contain the header "NonExistingHeader" + """ + When I run "behat --no-colors" + Then it should fail with an error: + """ + The response does not contain the header "NonExistingHeader". + """ + + Scenario: Assert "Then the response should not contain the header :header_name" works + Given I go to "/" + Then the response should not contain the header "NonExistingHeader" + + @trait:ResponseTrait + Scenario: Assert that negative assertion for "Then the response should not contain the header :header_name" fails with an error + Given some behat configuration + And scenario steps: + """ + Given I go to "/" + Then the response should not contain the header "Content-Type" + """ + When I run "behat --no-colors" + Then it should fail with an error: + """ + The response contains the header "Content-Type", but should not. + """ + + Scenario: Assert "Then the response header :header_name should contain the value :header_value" works + Given I go to "/" + Then the response header "Content-Type" should contain the value "text/html; charset=utf-8" + + @trait:ResponseTrait + Scenario: Assert that negative assertion for "Then the response header :header_name should contain the value :header_value" fails with an exception for missing header + Given some behat configuration + And scenario steps: + """ + Given I go to "/" + Then the response header "NonExistingHeader" should contain the value "text/html; charset=utf-8" + """ + When I run "behat --no-colors" + Then it should fail with an exception: + """ + The response does not contain the header "NonExistingHeader". + """ + + @trait:ResponseTrait + Scenario: Assert that negative assertion for "Then the response header :header_name should contain the value :header_value" fails with an error for invalid header value + Given some behat configuration + And scenario steps: + """ + Given I go to "/" + Then the response header "Content-Type" should contain the value "nonexistingvalue" + """ + When I run "behat --no-colors" + Then it should fail with a "Behat\Mink\Exception\ExpectationException" exception: + """ + The text "nonexistingvalue" was not found anywhere in the "Content-Type" response header. + """ + + Scenario: Assert "Then the response header :header_name should not contain the value :header_value" works + Given I go to "/" + Then the response header "Content-Type" should not contain the value "nonexistingvalue" + + @trait:ResponseTrait + Scenario: Assert that negative assertion for "Then the response header :header_name should not contain the value :header_value" fails with an exception for missing header + Given some behat configuration + And scenario steps: + """ + Given I go to "/" + Then the response header "NonExistingHeader" should not contain the value "nonexistingvalue" + """ + When I run "behat --no-colors" + Then it should fail with an exception: + """ + The response does not contain the header "NonExistingHeader". + """ + + @trait:ResponseTrait + Scenario: Assert that negative assertion for "Then the response header :header_name should not contain the value :header_value" fails with an error for invalid header value + Given some behat configuration + And scenario steps: + """ + Given I go to "/" + Then the response header "Content-Type" should not contain the value "text/html; charset=utf-8" + """ + When I run "behat --no-colors" + Then it should fail with a "Behat\Mink\Exception\ExpectationException" exception: + """ + The text "text/html; charset=utf-8" was found in the "Content-Type" response header, but it should not. + """