From d10177603ebe8dc45f60e8ea27211039c823bc96 Mon Sep 17 00:00:00 2001 From: Aleksey Razbakov Date: Thu, 24 Aug 2017 15:07:49 +0200 Subject: [PATCH 1/4] Fix set content headers CONTENT_* are not prefixed with HTTP_ in PHP when building $_SERVER Code taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader Closes #217 --- src/HttpCall/Request/Goutte.php | 11 ++++++++++- tests/features/rest.feature | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/HttpCall/Request/Goutte.php b/src/HttpCall/Request/Goutte.php index 82863cf9..61a3b394 100644 --- a/src/HttpCall/Request/Goutte.php +++ b/src/HttpCall/Request/Goutte.php @@ -22,7 +22,16 @@ public function send($method, $url, $parameters = [], $files = [], $content = nu public function setHttpHeader($name, $value) { - $name = strtoupper("http_$name"); + /* taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader */ + $contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true); + $name = str_replace('-', '_', strtoupper($name)); + + // CONTENT_* are not prefixed with HTTP_ in PHP when building $_SERVER + if (!isset($contentHeaders[$name])) { + $name = 'HTTP_' . $name; + } + /* taken from Behat\Mink\Driver\BrowserKitDriver::setRequestHeader */ + $this->requestHeaders[$name] = $value; } diff --git a/tests/features/rest.feature b/tests/features/rest.feature index 9ba869d6..1d825003 100644 --- a/tests/features/rest.feature +++ b/tests/features/rest.feature @@ -81,3 +81,20 @@ Feature: Testing RESTContext """ Congratulations, you've correctly set up your apache environment. """ + + Scenario: Set content headers in POST request + When I add "Content-Type" header equal to "xxx" + When I send a "POST" request to "rest/index.php" with body: + """ + {"name": "test"} + """ + Then the response should contain ">CONTENT_TYPE : xxx" + Then the response should contain ">HTTP_CONTENT_TYPE : xxx" + + Scenario: Content header is clear in different scenario + When I send a "POST" request to "rest/index.php" with body: + """ + {"name": "test"} + """ + Then the response should not contain ">CONTENT_TYPE : xxx" + Then the response should not contain ">HTTP_CONTENT_TYPE : xxx" From 3d70f4c1399045c7e69e7a48ea325989e0071067 Mon Sep 17 00:00:00 2001 From: Aleksey Razbakov Date: Thu, 24 Aug 2017 17:56:53 +0200 Subject: [PATCH 2/4] Use symfony2 session in behat tests in order to test Goutte Client --- behat.yml.dist | 2 -- 1 file changed, 2 deletions(-) diff --git a/behat.yml.dist b/behat.yml.dist index 5dae0a79..53930230 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -23,8 +23,6 @@ default: selenium2: ~ browser_name: 'chrome' sessions: - default: - goutte: ~ symfony2: goutte: ~ Behatch\Extension: ~ From 2f900d6ab62baa3b8fbf4b09ff55fb58721fd70c Mon Sep 17 00:00:00 2001 From: Aleksey Razbakov Date: Thu, 24 Aug 2017 17:58:22 +0200 Subject: [PATCH 3/4] Add more verbose output to assert exceptions - fix function name theHeaderShouldContain (was theHeaderShouldBeContains) - add $actual to assert exception to simplify debugging --- src/Context/RestContext.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Context/RestContext.php b/src/Context/RestContext.php index a3942ede..1487b69b 100644 --- a/src/Context/RestContext.php +++ b/src/Context/RestContext.php @@ -87,7 +87,7 @@ public function theResponseShouldBeEqualTo(PyStringNode $expected) { $expected = str_replace('\\"', '"', $expected); $actual = $this->request->getContent(); - $message = "The string '$expected' is not equal to the response of the current page"; + $message = "Actual response is '$actual', but expected '$expected'"; $this->assertEquals($expected, $actual, $message); } @@ -99,7 +99,7 @@ public function theResponseShouldBeEqualTo(PyStringNode $expected) public function theResponseShouldBeEmpty() { $actual = $this->request->getContent(); - $message = 'The response of the current page is not empty'; + $message = "The response of the current page is not empty, it is: $actual"; $this->assertTrue(null === $actual || "" === $actual, $message); } @@ -112,7 +112,7 @@ public function theHeaderShouldBeEqualTo($name, $value) { $actual = $this->request->getHttpHeader($name); $this->assertEquals(strtolower($value), strtolower($actual), - "The header '$name' is equal to '$actual'" + "The header '$name' should be equal to '$value', but it is: '$actual'" ); } @@ -131,15 +131,25 @@ public function theHeaderShouldNotBeEqualTo($name, $value) { } } + public function theHeaderShouldBeContains($name, $value) + { + trigger_error( + sprintf('The %s function is deprecated since version 3.1 and will be removed in 4.0. Use the %s::theHeaderShouldContain function instead.', __METHOD__, __CLASS__), + E_USER_DEPRECATED + ); + $this->theHeaderShouldContain($name, $value); + } + /** * Checks, whether the header name contains the given text * * @Then the header :name should contain :value */ - public function theHeaderShouldBeContains($name, $value) + public function theHeaderShouldContain($name, $value) { - $this->assertContains($value, $this->request->getHttpHeader($name), - "The header '$name' doesn't contain '$value'" + $actual = $this->request->getHttpHeader($name); + $this->assertContains($value, $actual, + "The header '$name' should contain value '$value', but actual value is '$actual'" ); } @@ -207,7 +217,7 @@ public function theResponseShouldBeEncodedIn($encoding) throw new \Exception("The response is not encoded in $encoding"); } - $this->theHeaderShouldBeContains('Content-Type', "charset=$encoding"); + $this->theHeaderShouldContain('Content-Type', "charset=$encoding"); } /** From 40c23466da926793d2761747ea86e68a0c3774c8 Mon Sep 17 00:00:00 2001 From: Aleksey Razbakov Date: Fri, 25 Aug 2017 14:54:13 +0200 Subject: [PATCH 4/4] fix behat configuration #220 --- behat.yml.dist | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/behat.yml.dist b/behat.yml.dist index 53930230..140bf0a6 100644 --- a/behat.yml.dist +++ b/behat.yml.dist @@ -16,18 +16,20 @@ default: - behatch:context:table - behatch:context:xml extensions: - Behat\MinkExtension\ServiceContainer\MinkExtension: + Behat\MinkExtension: base_url: 'http://localhost:8080' files_path: 'tests/fixtures/files' goutte: ~ selenium2: ~ browser_name: 'chrome' sessions: + default: + goutte: ~ symfony2: goutte: ~ Behatch\Extension: ~ symfony2: extensions: - Behat\MinkExtension\ServiceContainer\MinkExtension: + Behat\MinkExtension: default_session: symfony2