Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
Merge pull request #36 from moltin/post-data-fix
Browse files Browse the repository at this point in the history
Allow Empty POST data
  • Loading branch information
anthonysterling committed Nov 4, 2015
2 parents 0922ece + c0848d2 commit 18720c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Request/CURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CURL implements \Moltin\SDK\RequestInterface
public $header;

protected $curl;
protected $options = array();

public function setup($url, $method, $post = array(), $token = null)
{
Expand All @@ -36,9 +37,7 @@ public function setup($url, $method, $post = array(), $token = null)
$this->curl = curl_init();
$this->url = $url;
$this->method = $method;

// Add request settings
curl_setopt_array($this->curl, array(
$this->options = array(
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HEADER => false,
Expand All @@ -48,15 +47,16 @@ public function setup($url, $method, $post = array(), $token = null)
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 40,
CURLINFO_HEADER_OUT => true,
));
);

if ('POST' == $method) {
$this->options[CURLOPT_POST] = true;
}

// Add post
if (!empty($post)) {
$post = $this->toFormattedPostData($post, $_FILES);

// Assign to curl
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $post);
$this->options[CURLOPT_POSTFIELDS] = $post;
}

// Add auth header
Expand All @@ -78,7 +78,7 @@ public function setup($url, $method, $post = array(), $token = null)
$headers[] = 'X-Moltin-Session: '.session_id();

// Set headers
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
$this->options[CURLOPT_HTTPHEADER] = $headers;
}

/**
Expand Down Expand Up @@ -115,6 +115,7 @@ protected function generateInlineArray($value, $key = '', $index = '') {
public function make()
{
// Make request
curl_setopt_array($this->curl, $this->options);
$result = curl_exec($this->curl);
$this->code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
$this->time = curl_getinfo($this->curl, CURLINFO_TOTAL_TIME);
Expand Down
18 changes: 18 additions & 0 deletions tests/Request/CURLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ public function test_inline_arrays_to_formatted_post_data($data, $expected) {
$this->assertEquals($expected, $result);
}

public function test_that_empty_post_data_creates_post_request()
{
$request = $this->newCurlInstance();
$request->setup('/checkout/payment/authorize', 'POST');
$options = \PHPUnit_Framework_Assert::readAttribute($request, 'options');
$this->assertArrayHasKey(CURLOPT_POST, $options);
$this->assertEquals(true, $options[CURLOPT_POST]);

$request = $this->newCurlInstance();
$post = array('foo' => 'bar');
$request->setup('/checkout/payment/authorize', 'POST', $post);
$options = \PHPUnit_Framework_Assert::readAttribute($request, 'options');
$this->assertArrayHasKey(CURLOPT_POST, $options);
$this->assertArrayHasKey(CURLOPT_POSTFIELDS, $options);
$this->assertEquals(true, $options[CURLOPT_POST]);
$this->assertEquals($post, $options[CURLOPT_POSTFIELDS]);
}

public function getOrderInlineArrayPostDataProvider()
{
$dataOrder = array(
Expand Down

0 comments on commit 18720c7

Please sign in to comment.