From 492ffc03e78458357210d692f36a0cd74a888825 Mon Sep 17 00:00:00 2001 From: Nils Gajsek Date: Mon, 23 Oct 2017 09:43:10 +0200 Subject: [PATCH] #68 Part of post raw data. --- Curl.php | 19 ++++++++++++++++++ README.md | 22 +++++++++++++++++++++ tests/functional/httpMockCest.php | 32 +++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/Curl.php b/Curl.php index 6d034f7..dfc4060 100644 --- a/Curl.php +++ b/Curl.php @@ -273,6 +273,25 @@ public function setPostParams($params) } + /** + * Set raw post data allows you to post any data format. + * + * @param mixed $data + * @return $this + */ + public function setRawPostData($data) + { + + $this->setOption( + CURLOPT_POSTFIELDS, + $data + ); + + //return self + return $this; + } + + /** * Set get params * diff --git a/README.md b/README.md index 6bb5a5f..fed47a0 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,25 @@ $response = $curl->setPostParams([ ->post('http://example.com/'); ``` +```php +// POST RAW JSON +$curl = new curl\Curl(); +$response = $curl->setRawPostData( + json_encode[ + 'key' => 'value', + 'secondKey' => 'secondValue' + ]) + ->post('http://example.com/'); +``` + +```php +// POST RAW XML +$curl = new curl\Curl(); +$response = $curl->setRawPostData('Test') + ->post('http://example.com/'); +``` + + ```php // POST with special headers $curl = new curl\Curl(); @@ -152,6 +171,9 @@ Testing Changelog ------------ +##### Release 1.2.1 - Changelog +- Added `setRawPostData([mixed]) [this]` which allows you to post any data format. + ##### Release 1.2.0 - Changelog - Added `unsetHeader([string header]) [this]` helper which allows you to unset one specific header. - Added `setHeader([string header, string value]) [this]` helper which allows you to set one specific header. diff --git a/tests/functional/httpMockCest.php b/tests/functional/httpMockCest.php index 7c234fe..9a30d49 100644 --- a/tests/functional/httpMockCest.php +++ b/tests/functional/httpMockCest.php @@ -870,4 +870,36 @@ public function setMultipleHeadersAndSingleHeaderAndUnsetOneTillTestGetHeader(\F $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json'); $I->assertEquals($this->_curl->getRequestHeader('custom-type'), null); } + + /** + * Try set a single header, multiple header and unset one header param and check if getRequestHeader() does return it + * @param \FunctionalTester $I + */ + public function setRawPostDataTest (\FunctionalTester $I) + { + //Init + $this->_curl->reset(); + $params = [ + 'key' => 'value', + 'secondKey' => 'secondValue' + ]; + + $I->expectARequestToRemoteServiceWithAResponse( + $expectation = Phiremock::on( + A::postRequest()->andUrl(Is::equalTo('/test/params/post')) + ->andBody(Is::equalTo(json_encode($params))) + ->andHeader('Content-Type', Is::equalTo('application/json')) + )->then( + Respond::withStatusCode(200) + ) + ); + + $this->_curl->setRawPostData(json_encode($params)) + ->setHeader('Content-Type', 'application/json') + ->post($this->_endPoint . '/test/params/post'); + + //check for value + $I->assertEquals($this->_curl->getRequestHeader('Content-Type'), 'application/json'); + $I->assertEquals($this->_curl->getRequestHeader('custom-type'), null); + } }