Skip to content

Commit

Permalink
#68 Part of post raw data.
Browse files Browse the repository at this point in the history
  • Loading branch information
linslin committed Oct 23, 2017
1 parent 1401439 commit 492ffc0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('<?xml version="1.0" encoding="UTF-8"?><someNode>Test</someNode>')
->post('http://example.com/');
```


```php
// POST with special headers
$curl = new curl\Curl();
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions tests/functional/httpMockCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 492ffc0

Please sign in to comment.