diff --git a/README.md b/README.md index dd89e3c..85b8399 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ Curl::setConnectTimeout($seconds) Curl::setCookie($key, $value) Curl::setCookieFile($cookie_file) Curl::setCookieJar($cookie_jar) +Curl::setCookieString($string) Curl::setDefaultJsonDecoder() Curl::setDefaultTimeout() Curl::setDefaultUserAgent() diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index b1d1087..aae4472 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -713,6 +713,17 @@ class Curl $this->setOpt(CURLOPT_CONNECTTIMEOUT, $seconds); } + /** + * Set Cookie String + * + * @access public + * @param $string + */ + public function setCookieString($string) + { + return $this->setOpt(CURLOPT_COOKIE, $string); + } + /** * Set Cookie File * diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 0e38938..3844d45 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -706,7 +706,7 @@ class CurlTest extends PHPUnit_Framework_TestCase } } - public function testCookies() + public function testSetCookie() { $test = new Test(); $test->curl->setCookie('mycookie', 'yum'); @@ -715,7 +715,7 @@ class CurlTest extends PHPUnit_Framework_TestCase ))); } - public function testCookieEncodingSpace() + public function testSetCookieEncodingSpace() { $curl = new Curl(); $curl->setCookie('cookie', 'Om nom nom nom'); @@ -727,7 +727,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('cookie=Om%20nom%20nom%20nom', $options[CURLOPT_COOKIE]); } - public function testMultipleCookies() + public function testSetMultipleCookies() { $curl = new Curl(); $curl->setCookie('cookie', 'Om nom nom nom'); @@ -740,7 +740,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('cookie=Om%20nom%20nom%20nom; foo=bar', $options[CURLOPT_COOKIE]); } - public function testCookieEncodingColon() + public function testSetCookieEncodingColon() { $curl = new Curl(); $curl->setCookie('JSESSIONID', '0000wd-PcsB3bZ-KzYGAqm_rKlm:17925chrl'); @@ -752,6 +752,21 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('JSESSIONID=0000wd-PcsB3bZ-KzYGAqm_rKlm:17925chrl', $options[CURLOPT_COOKIE]); } + public function testSetCookieString() + { + $cookie_string = 'fruit=apple; color=red'; + + $test = new Test(); + $test->curl->setCookieString($cookie_string); + + $reflectionClass = new ReflectionClass('\Curl\Curl'); + $reflectionProperty = $reflectionClass->getProperty('options'); + $reflectionProperty->setAccessible(true); + $options = $reflectionProperty->getValue($test->curl); + $this->assertEquals($cookie_string, $options[CURLOPT_COOKIE]); + $this->assertEquals('fruit=apple&color=red', $test->server('cookie', 'GET')); + } + public function testCookieFile() { $cookie_file = dirname(__FILE__) . '/cookies.txt'; diff --git a/tests/PHPCurlClass/server.php b/tests/PHPCurlClass/server.php index da7ea02..124f165 100644 --- a/tests/PHPCurlClass/server.php +++ b/tests/PHPCurlClass/server.php @@ -299,6 +299,12 @@ $data_mapping = array( if (!empty($test)) { $data = $data_mapping[$test]; - $value = isset($data[$key]) ? $data[$key] : ''; + if (empty($key)) { + // Return all values when a key is not specified. + $value = http_build_query($data); + } else { + // Return individual value when a key is specified. + $value = isset($data[$key]) ? $data[$key] : ''; + } echo $value; }