From 907fd65f7d73b88eaafbb4c38330304d5d2ff80d Mon Sep 17 00:00:00 2001 From: Krzysztof Kubacki Date: Mon, 17 Sep 2018 16:20:10 +0200 Subject: [PATCH] Test a plain text response content --- behat.yml | 1 + features/response_content_plain_text.feature | 26 +++++++ src/Response/ResponseContext.php | 79 ++++++++++++++++++++ www/index.php | 9 +++ 4 files changed, 115 insertions(+) create mode 100644 features/response_content_plain_text.feature create mode 100644 src/Response/ResponseContext.php diff --git a/behat.yml b/behat.yml index 7369d02..ef21dac 100644 --- a/behat.yml +++ b/behat.yml @@ -4,6 +4,7 @@ default: contexts: - FeatureContext - Ubirak\RestApiBehatExtension\RestApiContext + - Ubirak\RestApiBehatExtension\Response\ResponseContext - Ubirak\RestApiBehatExtension\Json\JsonContext: jsonSchemaBaseUrl: '%paths.base%/features/bootstrap' diff --git a/features/response_content_plain_text.feature b/features/response_content_plain_text.feature new file mode 100644 index 0000000..2c9768e --- /dev/null +++ b/features/response_content_plain_text.feature @@ -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 diff --git a/src/Response/ResponseContext.php b/src/Response/ResponseContext.php new file mode 100644 index 0000000..212c08f --- /dev/null +++ b/src/Response/ResponseContext.php @@ -0,0 +1,79 @@ +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(); + } +} diff --git a/www/index.php b/www/index.php index f06490c..4fdf7d6 100644 --- a/www/index.php +++ b/www/index.php @@ -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'; @@ -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();