Merge pull request #68 from thibaultCha/feature/set-mashape-key

Native 'setMashapeKey' method for X-Mashape-Key header
This commit is contained in:
Ahmad Nassri
2015-02-10 20:16:30 -08:00
3 changed files with 50 additions and 2 deletions

View File

@@ -112,7 +112,13 @@ $response = Unirest\Request::post("http://httpbin.org/post", $headers, $body);
### Authentication ### Authentication
Passing a username, password *(optional)*, defaults to Basic Authentication: First, if you are using [Mashape][mashape-url]:
```php
// Mashape auth
Unirest\Request::setMashapeKey('<mashape_key>');
```
Otherwise, passing a username, password *(optional)*, defaults to Basic Authentication:
```php ```php
// basic auth // basic auth
@@ -279,7 +285,9 @@ By default is `true`.
---- ----
Made with &#9829; from the [Mashape](https://www.mashape.com/) team Made with &#9829; from the [Mashape][mashape-url] team
[mashape-url]: https://www.mashape.com/
[license-url]: https://github.com/Mashape/unirest-php/blob/master/LICENSE [license-url]: https://github.com/Mashape/unirest-php/blob/master/LICENSE

View File

@@ -87,6 +87,20 @@ class Request
return self::$defaultHeaders[$name] = $value; 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::defaultHeader('X-Mashape-Key', $key);
}
/** /**
* Clear all the default headers * Clear all the default headers
*/ */

View File

@@ -64,6 +64,32 @@ class UnirestRequestTest extends \PHPUnit_Framework_TestCase
$this->assertFalse(array_key_exists('Hello', $properties)); $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() public function testGzip()
{ {
$response = Unirest\Request::get('http://httpbin.org/gzip'); $response = Unirest\Request::get('http://httpbin.org/gzip');