Skip to content

Commit

Permalink
[#296] Updated ResponseTrait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Nov 3, 2024
1 parent df5b7b7 commit 9f6a31a
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 32 deletions.
6 changes: 6 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:` |
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
56 changes: 33 additions & 23 deletions src/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
103 changes: 98 additions & 5 deletions tests/behat/features/response.feature
Original file line number Diff line number Diff line change
@@ -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.
"""

0 comments on commit 9f6a31a

Please sign in to comment.