Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test a plain text response content #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ default:
contexts:
- FeatureContext
- Ubirak\RestApiBehatExtension\RestApiContext
- Ubirak\RestApiBehatExtension\Response\ResponseContext
- Ubirak\RestApiBehatExtension\Json\JsonContext:
jsonSchemaBaseUrl: '%paths.base%/features/bootstrap'

Expand Down
26 changes: 26 additions & 0 deletions features/response_content_plain_text.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Feature: Response content value testing
In order to test my API
As a developper
I want to be able to test response content

Scenario: Testing if a plain text value of response content is desirable
Given I add "content-type" header equal to "application/json"
When I send a POST request to "response_content_plain_text_inspection" with body:
"""
{
"plain_text_value_i_want_to_have_in_response_body_content" : "b947c77c-ba52-11e8-9b2d-000000000000"
}
"""
And the response content should not be empty
And the response content should be equal to "b947c77c-ba52-11e8-9b2d-000000000000"
And the response content should not be equal to "plain text value i have not sent in payload"

Scenario: Testing if a plain text value of response content is empty
Given I add "content-type" header equal to "application/json"
When I send a POST request to "response_content_plain_text_inspection" with body:
"""
{
"plain_text_value_i_want_to_have_in_response_body_content" : ""
}
"""
And the response content should be empty
79 changes: 79 additions & 0 deletions src/Response/ResponseContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
namespace Ubirak\RestApiBehatExtension\Response;

use Ubirak\RestApiBehatExtension\Rest\RestApiBrowser;
use mageekguy\atoum\asserter;
use Behat\Behat\Context\Context;

class ResponseContext implements Context
{

private $asserter;
private $restApiBrowser;

public function __construct(RestApiBrowser $restApiBrowser)
{
$this->restApiBrowser = $restApiBrowser;
$this->asserter = new asserter\generator();
}

/**
* @Then the response content should not be empty
*/
public function theResponseContentShouldNotBeEmpty()
{
$content = (string) $this->getResponse()->getBody();
try {
$this->asserter->string($content)->isNotEmpty();
} catch (\Exception $e) {
throw new Rest\WrongResponseExpectation($e->getMessage(), $this->restApiBrowser->getRequest(), $this->getResponse(), $e);
}
}

/**
* @Then the response content should be empty
*/
public function theResponseContentShouldBeEmpty()
{
$content = (string) $this->getResponse()->getBody();
try {
$this->asserter->string($content)->isEmpty();
} catch (\Exception $e) {
throw new Rest\WrongResponseExpectation($e->getMessage(), $this->restApiBrowser->getRequest(), $this->getResponse(), $e);
}
}

/**
* @Then the response content should be equal to :expectedValue
*/
public function theResponseContentShouldBeEqualTo($expectedContent)
{
$content = (string) $this->getResponse()->getBody();
try {
$this->asserter->variable($content)->isEqualTo($expectedContent);
} catch (\Exception $e) {
throw new Rest\WrongResponseExpectation($e->getMessage(), $this->restApiBrowser->getRequest(), $this->getResponse(), $e);
}
}

/**
* @Then the response content should not be equal to :undesirableContent
*/
public function theResponseContentShouldNotBeEqualTo($undesirableContent)
{
$content = (string) $this->getResponse()->getBody();
try {
$this->asserter->variable($content)->isNotEqualTo($undesirableContent);
} catch (\Exception $e) {
throw new Rest\WrongResponseExpectation($e->getMessage(), $this->restApiBrowser->getRequest(), $this->getResponse(), $e);
}
}

/**
* @return ResponseInterface
*/
private function getResponse()
{
return $this->restApiBrowser->getResponse();
}
}
9 changes: 9 additions & 0 deletions www/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

require_once __DIR__.'/../vendor/autoload.php';

Expand Down Expand Up @@ -90,4 +91,12 @@ function (Request $request) {
}
);

$app->match(
'response_content_plain_text_inspection',
function (Request $request) {
$data = json_decode($request->getContent(), true);
return new Response($data['plain_text_value_i_want_to_have_in_response_body_content'], 200);
}
);

$app->run();