diff --git a/src/Request/CURL.php b/src/Request/CURL.php index 1f69d11..7ab3ae6 100644 --- a/src/Request/CURL.php +++ b/src/Request/CURL.php @@ -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) { @@ -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, @@ -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 @@ -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; } /** @@ -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); diff --git a/tests/Request/CURLTest.php b/tests/Request/CURLTest.php index aace6a1..c7b0357 100644 --- a/tests/Request/CURLTest.php +++ b/tests/Request/CURLTest.php @@ -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(