Skip to content

Commit

Permalink
Merge pull request #109 from imbo/empty-body-step
Browse files Browse the repository at this point in the history
Add step to assert that the response body is empty
  • Loading branch information
christeredvartsen committed Nov 15, 2021
2 parents 2f6dcfd + 511136f commit db2b39c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/guide/verify-server-response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ Assert that the value of the ``:header`` response header matches the regular exp

For more information regarding regular expressions and the usage of modifiers, `refer to the PHP manual <http://php.net/pcre>`_.

Then the response body is empty
-------------------------------

Assert that the response body is empty.

Then the response body is an empty JSON object
----------------------------------------------

Expand Down
7 changes: 7 additions & 0 deletions features/bootstrap/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@
return $response->withHeader('Content-Type', 'application/json');
});

/**
* Return a response with an empty body
*/
$app->get('/empty', function (Request $request, Response $response): Response {
return $response->withStatus(204);
});

/**
* Return a response with 403 Forbidden
*/
Expand Down
19 changes: 19 additions & 0 deletions features/verify-response-failures.feature
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,25 @@ Feature: Assertion steps can fail
]". (Imbo\BehatApiExtension\Exception\AssertionFailedException)
"""

Scenario: Assert response body is empty failure
Given a file named "features/assert-response-body-is-empty.feature" with:
"""
Feature: Make request and assert response body is empty
Scenario: Make request
Given the request body is:
'''
content
'''
When I request "/echo"
Then the response body is empty
"""
When I run "behat features/assert-response-body-is-empty.feature"
Then it should fail
And the output should contain:
"""
Expected response body to be empty, got "content". (Imbo\BehatApiExtension\Exception\AssertionFailedException)
"""

Scenario: Assert response body JSON array length failure
Given a file named "features/assert-response-body-json-array-length.feature" with:
"""
Expand Down
17 changes: 17 additions & 0 deletions features/verify-response.feature
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ Feature: Test Then steps
3 steps (3 passed)
"""

Scenario: Use Then step to verify responses with empty body
Given a file named "features/thens-empty-response-body.feature" with:
"""
Feature: Test for empty response body
Scenario: Assert that the response body is empty
When I request "/empty"
Then the response body is empty
"""
When I run "behat features/thens-empty-response-body.feature"
Then it should pass with:
"""
..
1 scenario (1 passed)
2 steps (2 passed)
"""

Scenario: Use Then steps to verify responses with numerical array as root
Given a file named "features/response-with-numerical-array.feature" with:
"""
Expand Down
24 changes: 24 additions & 0 deletions src/Context/ApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,30 @@ public function assertResponseBodyIsAnEmptyJsonArray(): bool
return true;
}

/**
* Assert that the response body is empty
*
* @throws AssertionFailedException
*
* @Then the response body is empty
*/
public function assertResponseBodyIsEmpty(): bool
{
if (!$this->response) {
throw new RuntimeException($this->missingResponseError);
}

$body = (string) $this->response->getBody();

try {
Assertion::noContent($body, sprintf('Expected response body to be empty, got "%s".', $body));
} catch (AssertionFailure $e) {
throw new AssertionFailedException($e->getMessage());
}

return true;
}

/**
* Assert that the response body contains an array with a specific length
*
Expand Down
32 changes: 32 additions & 0 deletions tests/Context/ApiContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,38 @@ public function testCanAssertThatTheResponseIsAnEmptyObject(): void
$this->assertTrue($this->context->assertResponseBodyIsAnEmptyJsonObject());
}

/**
* @covers ::assertResponseBodyIsEmpty
*/
public function testCanAssertThatTheResponseBodyIsEmpty(): void
{
$this->mockHandler->append(new Response(204));
$this->context->requestPath('/some/path');
$this->assertTrue($this->context->assertResponseBodyIsEmpty());
}

/**
* @covers ::assertResponseBodyIsEmpty
*/
public function testCanAssertThatTheResponseBodyIsEmptyCanFail(): void
{
$this->mockHandler->append(new Response(200, [], 'some content'));
$this->context->requestPath('/some/path');
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage('Expected response body to be empty, got "some content".');
$this->assertTrue($this->context->assertResponseBodyIsEmpty());
}

/**
* @covers ::assertResponseBodyIsEmpty
*/
public function testAssertThatTheResponseBodyIsEmptyThrowsExceptionOnMissingResponse(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('The request has not been made yet, so no response object exists.');
$this->context->assertResponseBodyIsEmpty();
}

/**
* @dataProvider getResponseBodyArrays
* @covers ::assertResponseBodyJsonArrayLength
Expand Down

0 comments on commit db2b39c

Please sign in to comment.