From fb241f689894e29e90847abbb4c8492a6e481b4d Mon Sep 17 00:00:00 2001 From: thibaultCha Date: Thu, 5 Feb 2015 20:49:06 -0800 Subject: [PATCH 1/3] Native 'setMashapeKey' method for X-Mashape-Key header --- README.md | 12 ++++++++++-- src/Unirest/Request.php | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 80a4b4f..f417b46 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,13 @@ $response = Unirest\Request::post("http://httpbin.org/post", $headers, $body); ### Authentication -Passing a username, password *(optional)*, defaults to Basic Authentication: +First, if you are using [Mashape][mashape-url]: +```php +// Mashape auth +Unirest\Request::setMashapeKey(''); +``` + +Otherwise, passing a username, password *(optional)*, defaults to Basic Authentication: ```php // basic auth @@ -279,7 +285,9 @@ By default is `true`. ---- -Made with ♥ from the [Mashape](https://www.mashape.com/) team +Made with ♥ from the [Mashape][mashape-url] team + +[mashape-url]: https://www.mashape.com/ [license-url]: https://github.com/Mashape/unirest-php/blob/master/LICENSE diff --git a/src/Unirest/Request.php b/src/Unirest/Request.php index dca202b..97a6630 100644 --- a/src/Unirest/Request.php +++ b/src/Unirest/Request.php @@ -87,6 +87,20 @@ class Request return self::$defaultHeaders[$name] = $value; } + /** + * Set a Mashape key to send on every request as a header + * Obtain your Mashape key by browsing one of your Mashape applications on https://www.mashape.com + * + * Note: Mashape provides 2 keys for each application: a 'Testing' and a 'Production' one. + * Be aware of which key you are using and do not share your Production key. + * + * @param string $key Mashape key + */ + public static function setMashapeKey($key) + { + return self::$defaultHeaders['X-Mashape-Key'] = $key; + } + /** * Clear all the default headers */ From 491064f40b45b3ad78310dfe6e3aa90f3145e20f Mon Sep 17 00:00:00 2001 From: thibaultCha Date: Thu, 5 Feb 2015 21:09:01 -0800 Subject: [PATCH 2/3] Add test for setMashapeKey --- tests/Unirest/RequestTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Unirest/RequestTest.php b/tests/Unirest/RequestTest.php index 44d1024..ffb36b2 100644 --- a/tests/Unirest/RequestTest.php +++ b/tests/Unirest/RequestTest.php @@ -64,6 +64,32 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase $this->assertFalse(array_key_exists('Hello', $properties)); } + public function testSetMashapeKey() + { + Unirest\Request::setMashapeKey('abcd'); + $response = Unirest\Request::get('http://httpbin.org/get'); + + $this->assertEquals(200, $response->code); + $headers = $response->body->headers; + $properties = get_object_vars($headers); + $this->assertTrue(array_key_exists('X-Mashape-Key', $properties)); + $this->assertEquals('abcd', $headers->{'X-Mashape-Key'}); + $response = Unirest\Request::get('http://httpbin.org/get'); + + $this->assertEquals(200, $response->code); + $headers = $response->body->headers; + $properties = get_object_vars($headers); + $this->assertTrue(array_key_exists('X-Mashape-Key', $properties)); + $this->assertEquals('abcd', $headers->{'X-Mashape-Key'}); + Unirest\Request::clearDefaultHeaders(); + $response = Unirest\Request::get('http://httpbin.org/get'); + + $this->assertEquals(200, $response->code); + $headers = $response->body->headers; + $properties = get_object_vars($headers); + $this->assertFalse(array_key_exists('X-Mashape-Key', $properties)); + } + public function testGzip() { $response = Unirest\Request::get('http://httpbin.org/gzip'); From 8b47e051baaed073c09fa36f2a0a68846b9910ea Mon Sep 17 00:00:00 2001 From: thibaultCha Date: Thu, 5 Feb 2015 23:02:12 -0800 Subject: [PATCH 3/3] Set Mashape Key by calling setDefaultHeader --- src/Unirest/Request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Unirest/Request.php b/src/Unirest/Request.php index 97a6630..f926363 100644 --- a/src/Unirest/Request.php +++ b/src/Unirest/Request.php @@ -98,7 +98,7 @@ class Request */ public static function setMashapeKey($key) { - return self::$defaultHeaders['X-Mashape-Key'] = $key; + return self::defaultHeader('X-Mashape-Key', $key); } /**