基础代码
This commit is contained in:
+87
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_Auth_Basic extends PHPUnit_Framework_TestCase {
|
||||
public static function transportProvider() {
|
||||
$transports = array(
|
||||
array('Requests_Transport_fsockopen'),
|
||||
array('Requests_Transport_cURL'),
|
||||
);
|
||||
return $transports;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
*/
|
||||
public function testUsingArray($transport) {
|
||||
if (!call_user_func(array($transport, 'test'))) {
|
||||
$this->markTestSkipped($transport . ' is not available');
|
||||
return;
|
||||
}
|
||||
|
||||
$options = array(
|
||||
'auth' => array('user', 'passwd'),
|
||||
'transport' => $transport,
|
||||
);
|
||||
$request = Requests::get(httpbin('/basic-auth/user/passwd'), array(), $options);
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body);
|
||||
$this->assertEquals(true, $result->authenticated);
|
||||
$this->assertEquals('user', $result->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
*/
|
||||
public function testUsingInstantiation($transport) {
|
||||
if (!call_user_func(array($transport, 'test'))) {
|
||||
$this->markTestSkipped($transport . ' is not available');
|
||||
return;
|
||||
}
|
||||
|
||||
$options = array(
|
||||
'auth' => new Requests_Auth_Basic(array('user', 'passwd')),
|
||||
'transport' => $transport,
|
||||
);
|
||||
$request = Requests::get(httpbin('/basic-auth/user/passwd'), array(), $options);
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body);
|
||||
$this->assertEquals(true, $result->authenticated);
|
||||
$this->assertEquals('user', $result->user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
*/
|
||||
public function testPOSTUsingInstantiation($transport) {
|
||||
if (!call_user_func(array($transport, 'test'))) {
|
||||
$this->markTestSkipped($transport . ' is not available');
|
||||
return;
|
||||
}
|
||||
|
||||
$options = array(
|
||||
'auth' => new Requests_Auth_Basic(array('user', 'passwd')),
|
||||
'transport' => $transport,
|
||||
);
|
||||
$data = 'test';
|
||||
$request = Requests::post(httpbin('/post'), array(), $data, $options);
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body);
|
||||
|
||||
$auth = $result->headers->Authorization;
|
||||
$auth = explode(' ', $auth);
|
||||
|
||||
$this->assertEquals(base64_encode('user:passwd'), $auth[1]);
|
||||
$this->assertEquals('test', $result->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testMissingPassword() {
|
||||
$auth = new Requests_Auth_Basic(array('user'));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_ChunkedDecoding extends PHPUnit_Framework_TestCase {
|
||||
public static function chunkedProvider() {
|
||||
return array(
|
||||
array(
|
||||
"25\r\nThis is the data in the first chunk\r\n\r\n1A\r\nand this is the second one\r\n0\r\n",
|
||||
"This is the data in the first chunk\r\nand this is the second one"
|
||||
),
|
||||
array(
|
||||
"02\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0\r\nnothing\n",
|
||||
"abra\ncadabra"
|
||||
),
|
||||
array(
|
||||
"02\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n",
|
||||
"abra\ncadabra\nall we got\n"
|
||||
),
|
||||
array(
|
||||
"02;foo=bar;hello=world\r\nab\r\n04;foo=baz\r\nra\nc\r\n06;justfoo\r\nadabra\r\n0c\r\n\nall we got\n",
|
||||
"abra\ncadabra\nall we got\n"
|
||||
),
|
||||
array(
|
||||
"02;foo=\"quoted value\"\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n",
|
||||
"abra\ncadabra\nall we got\n"
|
||||
),
|
||||
array(
|
||||
"02;foo-bar=baz\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n",
|
||||
"abra\ncadabra\nall we got\n"
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider chunkedProvider
|
||||
*/
|
||||
public function testChunked($body, $expected){
|
||||
$transport = new MockTransport();
|
||||
$transport->body = $body;
|
||||
$transport->chunked = true;
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
|
||||
$this->assertEquals($expected, $response->body);
|
||||
}
|
||||
|
||||
public static function notChunkedProvider() {
|
||||
return array(
|
||||
'invalid chunk size' => array( 'Hello! This is a non-chunked response!' ),
|
||||
'invalid chunk extension' => array( '1BNot chunked\r\nLooks chunked but it is not\r\n' ),
|
||||
'unquoted chunk-ext-val with space' => array( "02;foo=unquoted with space\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n" ),
|
||||
'unquoted chunk-ext-val with forbidden character' => array( "02;foo={unquoted}\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n" ),
|
||||
'invalid chunk-ext-name' => array( "02;{foo}=bar\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n" ),
|
||||
'incomplete quote for chunk-ext-value' => array( "02;foo=\"no end quote\r\nab\r\n04\r\nra\nc\r\n06\r\nadabra\r\n0c\r\n\nall we got\n" ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Response says it's chunked, but actually isn't
|
||||
* @dataProvider notChunkedProvider
|
||||
*/
|
||||
public function testNotActuallyChunked($body) {
|
||||
$transport = new MockTransport();
|
||||
$transport->body = $body;
|
||||
$transport->chunked = true;
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
|
||||
$this->assertEquals($transport->body, $response->body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Response says it's chunked and starts looking like it is, but turns out
|
||||
* that they're lying to us
|
||||
*/
|
||||
public function testMixedChunkiness() {
|
||||
$transport = new MockTransport();
|
||||
$transport->body = "02\r\nab\r\nNot actually chunked!";
|
||||
$transport->chunked = true;
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
$this->assertEquals($transport->body, $response->body);
|
||||
}
|
||||
}
|
||||
+642
@@ -0,0 +1,642 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_Cookies extends PHPUnit_Framework_TestCase {
|
||||
public function testBasicCookie() {
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue');
|
||||
|
||||
$this->assertEquals('requests-testcookie', $cookie->name);
|
||||
$this->assertEquals('testvalue', $cookie->value);
|
||||
$this->assertEquals('testvalue', (string) $cookie);
|
||||
|
||||
$this->assertEquals('requests-testcookie=testvalue', $cookie->format_for_header());
|
||||
$this->assertEquals('requests-testcookie=testvalue', $cookie->format_for_set_cookie());
|
||||
}
|
||||
|
||||
public function testCookieWithAttributes() {
|
||||
$attributes = array(
|
||||
'httponly',
|
||||
'path' => '/'
|
||||
);
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
|
||||
|
||||
$this->assertEquals('requests-testcookie=testvalue', $cookie->format_for_header());
|
||||
$this->assertEquals('requests-testcookie=testvalue; httponly; path=/', $cookie->format_for_set_cookie());
|
||||
}
|
||||
|
||||
public function testEmptyCookieName() {
|
||||
$cookie = Requests_Cookie::parse('test');
|
||||
$this->assertEquals('', $cookie->name);
|
||||
$this->assertEquals('test', $cookie->value);
|
||||
}
|
||||
|
||||
public function testEmptyAttributes() {
|
||||
$cookie = Requests_Cookie::parse('foo=bar; HttpOnly');
|
||||
$this->assertTrue($cookie->attributes['httponly']);
|
||||
}
|
||||
|
||||
public function testCookieJarSetter() {
|
||||
$jar1 = new Requests_Cookie_Jar();
|
||||
$jar1['requests-testcookie'] = 'testvalue';
|
||||
|
||||
$jar2 = new Requests_Cookie_Jar(array(
|
||||
'requests-testcookie' => 'testvalue',
|
||||
));
|
||||
$this->assertEquals($jar1, $jar2);
|
||||
}
|
||||
|
||||
public function testCookieJarUnsetter() {
|
||||
$jar = new Requests_Cookie_Jar();
|
||||
$jar['requests-testcookie'] = 'testvalue';
|
||||
|
||||
$this->assertEquals('testvalue', $jar['requests-testcookie']);
|
||||
|
||||
unset($jar['requests-testcookie']);
|
||||
$this->assertEmpty($jar['requests-testcookie']);
|
||||
$this->assertFalse(isset($jar['requests-testcookie']));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testCookieJarAsList() {
|
||||
$cookies = new Requests_Cookie_Jar();
|
||||
$cookies[] = 'requests-testcookie1=testvalue1';
|
||||
}
|
||||
|
||||
public function testCookieJarIterator() {
|
||||
$cookies = array(
|
||||
'requests-testcookie1' => 'testvalue1',
|
||||
'requests-testcookie2' => 'testvalue2',
|
||||
);
|
||||
$jar = new Requests_Cookie_Jar($cookies);
|
||||
|
||||
foreach ($jar as $key => $value) {
|
||||
$this->assertEquals($cookies[$key], $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function testReceivingCookies() {
|
||||
$options = array(
|
||||
'follow_redirects' => false,
|
||||
);
|
||||
$url = httpbin('/cookies/set?requests-testcookie=testvalue');
|
||||
|
||||
$response = Requests::get($url, array(), $options);
|
||||
|
||||
$cookie = $response->cookies['requests-testcookie'];
|
||||
$this->assertNotEmpty( $cookie );
|
||||
$this->assertEquals( 'testvalue', $cookie->value );
|
||||
}
|
||||
|
||||
public function testPersistenceOnRedirect() {
|
||||
$options = array(
|
||||
'follow_redirects' => true,
|
||||
);
|
||||
$url = httpbin('/cookies/set?requests-testcookie=testvalue');
|
||||
|
||||
$response = Requests::get($url, array(), $options);
|
||||
|
||||
$cookie = $response->cookies['requests-testcookie'];
|
||||
$this->assertNotEmpty( $cookie );
|
||||
$this->assertEquals( 'testvalue', $cookie->value );
|
||||
}
|
||||
|
||||
protected function setCookieRequest($cookies) {
|
||||
$options = array(
|
||||
'cookies' => $cookies,
|
||||
);
|
||||
$response = Requests::get(httpbin('/cookies/set'), array(), $options);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertInternalType('array', $data);
|
||||
$this->assertArrayHasKey('cookies', $data);
|
||||
return $data['cookies'];
|
||||
}
|
||||
|
||||
public function testSendingCookie() {
|
||||
$cookies = array(
|
||||
'requests-testcookie1' => 'testvalue1',
|
||||
);
|
||||
|
||||
$data = $this->setCookieRequest($cookies);
|
||||
|
||||
$this->assertArrayHasKey('requests-testcookie1', $data);
|
||||
$this->assertEquals('testvalue1', $data['requests-testcookie1']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testSendingCookie
|
||||
*/
|
||||
public function testCookieExpiration() {
|
||||
$options = array(
|
||||
'follow_redirects' => true,
|
||||
);
|
||||
$url = httpbin('/cookies/set/testcookie/testvalue');
|
||||
$url .= '?expiry=1';
|
||||
|
||||
$response = Requests::get($url, array(), $options);
|
||||
$response->throw_for_status();
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertEmpty($data['cookies']);
|
||||
}
|
||||
|
||||
public function testSendingCookieWithJar() {
|
||||
$cookies = new Requests_Cookie_Jar(array(
|
||||
'requests-testcookie1' => 'testvalue1',
|
||||
));
|
||||
$data = $this->setCookieRequest($cookies);
|
||||
|
||||
$this->assertArrayHasKey('requests-testcookie1', $data);
|
||||
$this->assertEquals('testvalue1', $data['requests-testcookie1']);
|
||||
}
|
||||
|
||||
public function testSendingMultipleCookies() {
|
||||
$cookies = array(
|
||||
'requests-testcookie1' => 'testvalue1',
|
||||
'requests-testcookie2' => 'testvalue2',
|
||||
);
|
||||
$data = $this->setCookieRequest($cookies);
|
||||
|
||||
$this->assertArrayHasKey('requests-testcookie1', $data);
|
||||
$this->assertEquals('testvalue1', $data['requests-testcookie1']);
|
||||
|
||||
$this->assertArrayHasKey('requests-testcookie2', $data);
|
||||
$this->assertEquals('testvalue2', $data['requests-testcookie2']);
|
||||
}
|
||||
|
||||
public function testSendingMultipleCookiesWithJar() {
|
||||
$cookies = new Requests_Cookie_Jar(array(
|
||||
'requests-testcookie1' => 'testvalue1',
|
||||
'requests-testcookie2' => 'testvalue2',
|
||||
));
|
||||
$data = $this->setCookieRequest($cookies);
|
||||
|
||||
$this->assertArrayHasKey('requests-testcookie1', $data);
|
||||
$this->assertEquals('testvalue1', $data['requests-testcookie1']);
|
||||
|
||||
$this->assertArrayHasKey('requests-testcookie2', $data);
|
||||
$this->assertEquals('testvalue2', $data['requests-testcookie2']);
|
||||
}
|
||||
|
||||
public function testSendingPrebakedCookie() {
|
||||
$cookies = new Requests_Cookie_Jar(array(
|
||||
new Requests_Cookie('requests-testcookie', 'testvalue'),
|
||||
));
|
||||
$data = $this->setCookieRequest($cookies);
|
||||
|
||||
$this->assertArrayHasKey('requests-testcookie', $data);
|
||||
$this->assertEquals('testvalue', $data['requests-testcookie']);
|
||||
}
|
||||
|
||||
public function domainMatchProvider() {
|
||||
return array(
|
||||
array('example.com', 'example.com', true, true),
|
||||
array('example.com', 'www.example.com', false, true),
|
||||
array('example.com', 'example.net', false, false),
|
||||
|
||||
// Leading period
|
||||
array('.example.com', 'example.com', true, true),
|
||||
array('.example.com', 'www.example.com', false, true),
|
||||
array('.example.com', 'example.net', false, false),
|
||||
|
||||
// Prefix, but not subdomain
|
||||
array('example.com', 'notexample.com', false, false),
|
||||
array('example.com', 'notexample.net', false, false),
|
||||
|
||||
// Reject IP address prefixes
|
||||
array('127.0.0.1', '127.0.0.1', true, true),
|
||||
array('127.0.0.1', 'abc.127.0.0.1', false, false),
|
||||
array('127.0.0.1', 'example.com', false, false),
|
||||
|
||||
// Check that we're checking the actual length
|
||||
array('127.com', 'test.127.com', false, true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider domainMatchProvider
|
||||
*/
|
||||
public function testDomainExactMatch($original, $check, $matches, $domain_matches) {
|
||||
$attributes = new Requests_Utility_CaseInsensitiveDictionary();
|
||||
$attributes['domain'] = $original;
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
|
||||
$this->assertEquals($matches, $cookie->domain_matches($check));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider domainMatchProvider
|
||||
*/
|
||||
public function testDomainMatch($original, $check, $matches, $domain_matches) {
|
||||
$attributes = new Requests_Utility_CaseInsensitiveDictionary();
|
||||
$attributes['domain'] = $original;
|
||||
$flags = array(
|
||||
'host-only' => false
|
||||
);
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
|
||||
$this->assertEquals($domain_matches, $cookie->domain_matches($check));
|
||||
}
|
||||
|
||||
public function pathMatchProvider() {
|
||||
return array(
|
||||
array('/', '', true),
|
||||
array('/', '/', true),
|
||||
|
||||
array('/', '/test', true),
|
||||
array('/', '/test/', true),
|
||||
|
||||
array('/test', '/', false),
|
||||
array('/test', '/test', true),
|
||||
array('/test', '/testing', false),
|
||||
array('/test', '/test/', true),
|
||||
array('/test', '/test/ing', true),
|
||||
array('/test', '/test/ing/', true),
|
||||
|
||||
array('/test/', '/test/', true),
|
||||
array('/test/', '/', false),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider pathMatchProvider
|
||||
*/
|
||||
public function testPathMatch($original, $check, $matches) {
|
||||
$attributes = new Requests_Utility_CaseInsensitiveDictionary();
|
||||
$attributes['path'] = $original;
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
|
||||
$this->assertEquals($matches, $cookie->path_matches($check));
|
||||
}
|
||||
|
||||
public function urlMatchProvider() {
|
||||
return array(
|
||||
// Domain handling
|
||||
array( 'example.com', '/', 'http://example.com/', true, true ),
|
||||
array( 'example.com', '/', 'http://www.example.com/', false, true ),
|
||||
array( 'example.com', '/', 'http://example.net/', false, false ),
|
||||
array( 'example.com', '/', 'http://www.example.net/', false, false ),
|
||||
|
||||
// /test
|
||||
array( 'example.com', '/test', 'http://example.com/', false, false ),
|
||||
array( 'example.com', '/test', 'http://www.example.com/', false, false ),
|
||||
|
||||
array( 'example.com', '/test', 'http://example.com/test', true, true ),
|
||||
array( 'example.com', '/test', 'http://www.example.com/test', false, true ),
|
||||
|
||||
array( 'example.com', '/test', 'http://example.com/testing', false, false ),
|
||||
array( 'example.com', '/test', 'http://www.example.com/testing', false, false ),
|
||||
|
||||
array( 'example.com', '/test', 'http://example.com/test/', true, true ),
|
||||
array( 'example.com', '/test', 'http://www.example.com/test/', false, true ),
|
||||
|
||||
// /test/
|
||||
array( 'example.com', '/test/', 'http://example.com/', false, false ),
|
||||
array( 'example.com', '/test/', 'http://www.example.com/', false, false ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testDomainExactMatch
|
||||
* @depends testPathMatch
|
||||
* @dataProvider urlMatchProvider
|
||||
*/
|
||||
public function testUrlExactMatch($domain, $path, $check, $matches, $domain_matches) {
|
||||
$attributes = new Requests_Utility_CaseInsensitiveDictionary();
|
||||
$attributes['domain'] = $domain;
|
||||
$attributes['path'] = $path;
|
||||
$check = new Requests_IRI($check);
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes);
|
||||
$this->assertEquals($matches, $cookie->uri_matches($check));
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testDomainMatch
|
||||
* @depends testPathMatch
|
||||
* @dataProvider urlMatchProvider
|
||||
*/
|
||||
public function testUrlMatch($domain, $path, $check, $matches, $domain_matches) {
|
||||
$attributes = new Requests_Utility_CaseInsensitiveDictionary();
|
||||
$attributes['domain'] = $domain;
|
||||
$attributes['path'] = $path;
|
||||
$flags = array(
|
||||
'host-only' => false
|
||||
);
|
||||
$check = new Requests_IRI($check);
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
|
||||
$this->assertEquals($domain_matches, $cookie->uri_matches($check));
|
||||
}
|
||||
|
||||
public function testUrlMatchSecure() {
|
||||
$attributes = new Requests_Utility_CaseInsensitiveDictionary();
|
||||
$attributes['domain'] = 'example.com';
|
||||
$attributes['path'] = '/';
|
||||
$attributes['secure'] = true;
|
||||
$flags = array(
|
||||
'host-only' => false,
|
||||
);
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue', $attributes, $flags);
|
||||
|
||||
$this->assertTrue($cookie->uri_matches(new Requests_IRI('https://example.com/')));
|
||||
$this->assertFalse($cookie->uri_matches(new Requests_IRI('http://example.com/')));
|
||||
|
||||
// Double-check host-only
|
||||
$this->assertTrue($cookie->uri_matches(new Requests_IRI('https://www.example.com/')));
|
||||
$this->assertFalse($cookie->uri_matches(new Requests_IRI('http://www.example.com/')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually set cookies without a domain/path set should always be valid
|
||||
*
|
||||
* Cookies parsed from headers internally in Requests will always have a
|
||||
* domain/path set, but those created manually will not. Manual cookies
|
||||
* should be regarded as "global" cookies (that is, set for `.`)
|
||||
*/
|
||||
public function testUrlMatchManuallySet() {
|
||||
$cookie = new Requests_Cookie('requests-testcookie', 'testvalue');
|
||||
$this->assertTrue($cookie->domain_matches('example.com'));
|
||||
$this->assertTrue($cookie->domain_matches('example.net'));
|
||||
$this->assertTrue($cookie->path_matches('/'));
|
||||
$this->assertTrue($cookie->path_matches('/test'));
|
||||
$this->assertTrue($cookie->path_matches('/test/'));
|
||||
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/')));
|
||||
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/test')));
|
||||
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.com/test/')));
|
||||
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/')));
|
||||
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/test')));
|
||||
$this->assertTrue($cookie->uri_matches(new Requests_IRI('http://example.net/test/')));
|
||||
}
|
||||
|
||||
public static function parseResultProvider() {
|
||||
return array(
|
||||
// Basic parsing
|
||||
array(
|
||||
'foo=bar',
|
||||
array( 'name' => 'foo', 'value' => 'bar' ),
|
||||
),
|
||||
array(
|
||||
'bar',
|
||||
array( 'name' => '', 'value' => 'bar' ),
|
||||
),
|
||||
|
||||
// Expiration
|
||||
// RFC 822, updated by RFC 1123
|
||||
array(
|
||||
'foo=bar; Expires=Thu, 5-Dec-2013 04:50:12 GMT',
|
||||
array( 'expired' => true ),
|
||||
array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2013 ) ),
|
||||
),
|
||||
array(
|
||||
'foo=bar; Expires=Fri, 5-Dec-2014 04:50:12 GMT',
|
||||
array( 'expired' => false ),
|
||||
array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2014 ) ),
|
||||
),
|
||||
// RFC 850, obsoleted by RFC 1036
|
||||
array(
|
||||
'foo=bar; Expires=Thursday, 5-Dec-2013 04:50:12 GMT',
|
||||
array( 'expired' => true ),
|
||||
array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2013 ) ),
|
||||
),
|
||||
array(
|
||||
'foo=bar; Expires=Friday, 5-Dec-2014 04:50:12 GMT',
|
||||
array( 'expired' => false ),
|
||||
array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2014 ) ),
|
||||
),
|
||||
// asctime()
|
||||
array(
|
||||
'foo=bar; Expires=Thu Dec 5 04:50:12 2013',
|
||||
array( 'expired' => true ),
|
||||
array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2013 ) ),
|
||||
),
|
||||
array(
|
||||
'foo=bar; Expires=Fri Dec 5 04:50:12 2014',
|
||||
array( 'expired' => false ),
|
||||
array( 'expires' => gmmktime( 4, 50, 12, 12, 5, 2014 ) ),
|
||||
),
|
||||
array(
|
||||
// Invalid
|
||||
'foo=bar; Expires=never',
|
||||
array(),
|
||||
array( 'expires' => null ),
|
||||
),
|
||||
|
||||
// Max-Age
|
||||
array(
|
||||
'foo=bar; Max-Age=10',
|
||||
array( 'expired' => false ),
|
||||
array( 'max-age' => gmmktime( 0, 0, 10, 1, 1, 2014 ) ),
|
||||
),
|
||||
array(
|
||||
'foo=bar; Max-Age=3660',
|
||||
array( 'expired' => false ),
|
||||
array( 'max-age' => gmmktime( 1, 1, 0, 1, 1, 2014 ) ),
|
||||
),
|
||||
array(
|
||||
'foo=bar; Max-Age=0',
|
||||
array( 'expired' => true ),
|
||||
array( 'max-age' => 0 ),
|
||||
),
|
||||
array(
|
||||
'foo=bar; Max-Age=-1000',
|
||||
array( 'expired' => true ),
|
||||
array( 'max-age' => 0 ),
|
||||
),
|
||||
array(
|
||||
// Invalid (non-digit character)
|
||||
'foo=bar; Max-Age=1e6',
|
||||
array( 'expired' => false ),
|
||||
array( 'max-age' => null ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
protected function check_parsed_cookie($cookie, $expected, $expected_attributes, $expected_flags = array()) {
|
||||
if (isset($expected['name'])) {
|
||||
$this->assertEquals($expected['name'], $cookie->name);
|
||||
}
|
||||
if (isset($expected['value'])) {
|
||||
$this->assertEquals($expected['value'], $cookie->value);
|
||||
}
|
||||
if (isset($expected['expired'])) {
|
||||
$this->assertEquals($expected['expired'], $cookie->is_expired());
|
||||
}
|
||||
if (isset($expected_attributes)) {
|
||||
foreach ($expected_attributes as $attr_key => $attr_val) {
|
||||
$this->assertEquals($attr_val, $cookie->attributes[$attr_key], "$attr_key should match supplied");
|
||||
}
|
||||
}
|
||||
if (isset($expected_flags)) {
|
||||
foreach ($expected_flags as $flag_key => $flag_val) {
|
||||
$this->assertEquals($flag_val, $cookie->flags[$flag_key], "$flag_key should match supplied");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider parseResultProvider
|
||||
*/
|
||||
public function testParsingHeader($header, $expected, $expected_attributes = array(), $expected_flags = array()) {
|
||||
// Set the reference time to 2014-01-01 00:00:00
|
||||
$reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );
|
||||
|
||||
$cookie = Requests_Cookie::parse($header, null, $reference_time);
|
||||
$this->check_parsed_cookie($cookie, $expected, $expected_attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Double-normalizes the cookie data to ensure we catch any issues there
|
||||
*
|
||||
* @dataProvider parseResultProvider
|
||||
*/
|
||||
public function testParsingHeaderDouble($header, $expected, $expected_attributes = array(), $expected_flags = array()) {
|
||||
// Set the reference time to 2014-01-01 00:00:00
|
||||
$reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );
|
||||
|
||||
$cookie = Requests_Cookie::parse($header, null, $reference_time);
|
||||
|
||||
// Normalize the value again
|
||||
$cookie->normalize();
|
||||
|
||||
$this->check_parsed_cookie($cookie, $expected, $expected_attributes, $expected_flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider parseResultProvider
|
||||
*/
|
||||
public function testParsingHeaderObject($header, $expected, $expected_attributes = array(), $expected_flags = array()) {
|
||||
$headers = new Requests_Response_Headers();
|
||||
$headers['Set-Cookie'] = $header;
|
||||
|
||||
// Set the reference time to 2014-01-01 00:00:00
|
||||
$reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );
|
||||
|
||||
$parsed = Requests_Cookie::parse_from_headers($headers, null, $reference_time);
|
||||
$this->assertCount(1, $parsed);
|
||||
|
||||
$cookie = reset($parsed);
|
||||
$this->check_parsed_cookie($cookie, $expected, $expected_attributes);
|
||||
}
|
||||
|
||||
public function parseFromHeadersProvider() {
|
||||
return array(
|
||||
# Varying origin path
|
||||
array(
|
||||
'name=value',
|
||||
'http://example.com/',
|
||||
array(),
|
||||
array( 'path' => '/' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
array(
|
||||
'name=value',
|
||||
'http://example.com/test',
|
||||
array(),
|
||||
array( 'path' => '/' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
array(
|
||||
'name=value',
|
||||
'http://example.com/test/',
|
||||
array(),
|
||||
array( 'path' => '/test' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
array(
|
||||
'name=value',
|
||||
'http://example.com/test/abc',
|
||||
array(),
|
||||
array( 'path' => '/test' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
array(
|
||||
'name=value',
|
||||
'http://example.com/test/abc/',
|
||||
array(),
|
||||
array( 'path' => '/test/abc' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
|
||||
# With specified path
|
||||
array(
|
||||
'name=value; path=/',
|
||||
'http://example.com/',
|
||||
array(),
|
||||
array( 'path' => '/' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
array(
|
||||
'name=value; path=/test',
|
||||
'http://example.com/',
|
||||
array(),
|
||||
array( 'path' => '/test' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
array(
|
||||
'name=value; path=/test/',
|
||||
'http://example.com/',
|
||||
array(),
|
||||
array( 'path' => '/test/' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
|
||||
# Invalid path
|
||||
array(
|
||||
'name=value; path=yolo',
|
||||
'http://example.com/',
|
||||
array(),
|
||||
array( 'path' => '/' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
array(
|
||||
'name=value; path=yolo',
|
||||
'http://example.com/test/',
|
||||
array(),
|
||||
array( 'path' => '/test' ),
|
||||
array( 'host-only' => true ),
|
||||
),
|
||||
|
||||
# Cross-origin cookies, reject!
|
||||
array(
|
||||
'name=value; domain=example.org',
|
||||
'http://example.com/',
|
||||
array( 'invalid' => false ),
|
||||
),
|
||||
|
||||
# Subdomain cookies
|
||||
array(
|
||||
'name=value; domain=test.example.com',
|
||||
'http://test.example.com/',
|
||||
array(),
|
||||
array( 'domain' => 'test.example.com' ),
|
||||
array( 'host-only' => false )
|
||||
),
|
||||
array(
|
||||
'name=value; domain=example.com',
|
||||
'http://test.example.com/',
|
||||
array(),
|
||||
array( 'domain' => 'example.com' ),
|
||||
array( 'host-only' => false )
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider parseFromHeadersProvider
|
||||
*/
|
||||
public function testParsingHeaderWithOrigin($header, $origin, $expected, $expected_attributes = array(), $expected_flags = array()) {
|
||||
$origin = new Requests_IRI($origin);
|
||||
$headers = new Requests_Response_Headers();
|
||||
$headers['Set-Cookie'] = $header;
|
||||
|
||||
// Set the reference time to 2014-01-01 00:00:00
|
||||
$reference_time = gmmktime( 0, 0, 0, 1, 1, 2014 );
|
||||
|
||||
$parsed = Requests_Cookie::parse_from_headers($headers, $origin, $reference_time);
|
||||
if (isset($expected['invalid'])) {
|
||||
$this->assertCount(0, $parsed);
|
||||
return;
|
||||
}
|
||||
$this->assertCount(1, $parsed);
|
||||
|
||||
$cookie = reset($parsed);
|
||||
$this->check_parsed_cookie($cookie, $expected, $expected_attributes, $expected_flags);
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
class RequestsTests_Encoding extends PHPUnit_Framework_TestCase {
|
||||
protected static function mapData($type, $data) {
|
||||
$real_data = array();
|
||||
foreach ($data as $value) {
|
||||
$key = $type . ': ' . $value[0];
|
||||
$real_data[$key] = $value;
|
||||
}
|
||||
return $real_data;
|
||||
}
|
||||
|
||||
public static function gzipData() {
|
||||
return array(
|
||||
array(
|
||||
'foobar',
|
||||
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xcb\xcf\x4f\x4a"
|
||||
. "\x2c\x02\x00\x95\x1f\xf6\x9e\x06\x00\x00\x00",
|
||||
),
|
||||
array(
|
||||
'Requests for PHP',
|
||||
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x0b\x4a\x2d\x2c\x4d"
|
||||
. "\x2d\x2e\x29\x56\x48\xcb\x2f\x52\x08\xf0\x08\x00\x00\x58\x35"
|
||||
. "\x18\x17\x10\x00\x00\x00",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public static function deflateData() {
|
||||
return array(
|
||||
array(
|
||||
'foobar',
|
||||
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x78\x9c\x4b\xcb\xcf"
|
||||
. "\x4f\x4a\x2c\x02\x00\x08\xab\x02\x7a"
|
||||
),
|
||||
array(
|
||||
'Requests for PHP',
|
||||
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x78\x9c\x0b\x4a\x2d"
|
||||
. "\x2c\x4d\x2d\x2e\x29\x56\x48\xcb\x2f\x52\x08\xf0\x08\x00\x00"
|
||||
. "\x34\x68\x05\xcc"
|
||||
)
|
||||
);
|
||||
}
|
||||
public static function deflateWithoutHeadersData() {
|
||||
return array(
|
||||
array(
|
||||
'foobar',
|
||||
"\x78\x9c\x4b\xcb\xcf\x4f\x4a\x2c\x02\x00\x08\xab\x02\x7a"
|
||||
),
|
||||
array(
|
||||
'Requests for PHP',
|
||||
"\x78\x9c\x0b\x4a\x2d\x2c\x4d\x2d\x2e\x29\x56\x48\xcb\x2f\x52"
|
||||
. "\x08\xf0\x08\x00\x00\x34\x68\x05\xcc"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function encodedData() {
|
||||
$datasets = array();
|
||||
$datasets['gzip'] = self::gzipData();
|
||||
$datasets['deflate'] = self::deflateData();
|
||||
$datasets['deflate without zlib headers'] = self::deflateWithoutHeadersData();
|
||||
|
||||
$data = array();
|
||||
foreach ($datasets as $key => $set) {
|
||||
$real_set = self::mapData($key, $set);
|
||||
$data = array_merge($data, $real_set);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider encodedData
|
||||
*/
|
||||
public function testDecompress($original, $encoded) {
|
||||
$decoded = Requests::decompress($encoded);
|
||||
$this->assertEquals($original, $decoded);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider encodedData
|
||||
*/
|
||||
public function testCompatibleInflate($original, $encoded) {
|
||||
$decoded = Requests::compatible_gzinflate($encoded);
|
||||
$this->assertEquals($original, $decoded);
|
||||
}
|
||||
|
||||
protected function bin2hex($field) {
|
||||
$field = bin2hex($field);
|
||||
$field = chunk_split($field,2,"\\x");
|
||||
$field = "\\x" . substr($field,0,-2);
|
||||
return $field;
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_IDNAEncoder extends PHPUnit_Framework_TestCase {
|
||||
public static function specExamples() {
|
||||
return array(
|
||||
array(
|
||||
"\xe4\xbb\x96\xe4\xbb\xac\xe4\xb8\xba\xe4\xbb\x80\xe4\xb9\x88\xe4\xb8\x8d\xe8\xaf\xb4\xe4\xb8\xad\xe6\x96\x87",
|
||||
"xn--ihqwcrb4cv8a8dqg056pqjye"
|
||||
),
|
||||
array(
|
||||
"\x33\xe5\xb9\xb4\x42\xe7\xb5\x84\xe9\x87\x91\xe5\x85\xab\xe5\x85\x88\xe7\x94\x9f",
|
||||
"xn--3B-ww4c5e180e575a65lsy2b",
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider specExamples
|
||||
*/
|
||||
public function testEncoding($data, $expected) {
|
||||
$result = Requests_IDNAEncoder::encode($data);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testASCIITooLong() {
|
||||
$data = str_repeat("abcd", 20);
|
||||
$result = Requests_IDNAEncoder::encode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testEncodedTooLong() {
|
||||
$data = str_repeat("\xe4\xbb\x96", 60);
|
||||
$result = Requests_IDNAEncoder::encode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testAlreadyPrefixed() {
|
||||
$result = Requests_IDNAEncoder::encode("xn--\xe4\xbb\x96");
|
||||
}
|
||||
|
||||
public function testASCIICharacter() {
|
||||
$result = Requests_IDNAEncoder::encode("a");
|
||||
$this->assertEquals('a', $result);
|
||||
}
|
||||
|
||||
public function testTwoByteCharacter() {
|
||||
$result = Requests_IDNAEncoder::encode("\xc2\xb6"); // Pilcrow character
|
||||
$this->assertEquals('xn--tba', $result);
|
||||
}
|
||||
|
||||
public function testThreeByteCharacter() {
|
||||
$result = Requests_IDNAEncoder::encode("\xe2\x82\xac"); // Euro symbol
|
||||
$this->assertEquals('xn--lzg', $result);
|
||||
}
|
||||
|
||||
public function testFourByteCharacter() {
|
||||
$result = Requests_IDNAEncoder::encode("\xf0\xa4\xad\xa2"); // Chinese symbol?
|
||||
$this->assertEquals('xn--ww6j', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testFiveByteCharacter() {
|
||||
$result = Requests_IDNAEncoder::encode("\xfb\xb6\xb6\xb6\xb6");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testSixByteCharacter() {
|
||||
$result = Requests_IDNAEncoder::encode("\xfd\xb6\xb6\xb6\xb6\xb6");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testInvalidASCIICharacterWithMultibyte() {
|
||||
$result = Requests_IDNAEncoder::encode("\0\xc2\xb6");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testUnfinishedMultibyte() {
|
||||
$result = Requests_IDNAEncoder::encode("\xc2");
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testPartialMultibyte() {
|
||||
$result = Requests_IDNAEncoder::encode("\xc2\xc2\xb6");
|
||||
}
|
||||
}
|
||||
Vendored
+413
@@ -0,0 +1,413 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* IRI test cases
|
||||
*
|
||||
* Copyright (c) 2008-2010 Geoffrey Sneddon.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the SimplePie Team nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @package IRI
|
||||
* @author Geoffrey Sneddon
|
||||
* @copyright 2008-2010 Geoffrey Sneddon
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php
|
||||
* @link http://hg.gsnedders.com/iri/
|
||||
*
|
||||
*/
|
||||
|
||||
class RequestsTest_IRI extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public static function rfc3986_tests()
|
||||
{
|
||||
return array(
|
||||
// Normal
|
||||
array('g:h', 'g:h'),
|
||||
array('g', 'http://a/b/c/g'),
|
||||
array('./g', 'http://a/b/c/g'),
|
||||
array('g/', 'http://a/b/c/g/'),
|
||||
array('/g', 'http://a/g'),
|
||||
array('//g', 'http://g/'),
|
||||
array('?y', 'http://a/b/c/d;p?y'),
|
||||
array('g?y', 'http://a/b/c/g?y'),
|
||||
array('#s', 'http://a/b/c/d;p?q#s'),
|
||||
array('g#s', 'http://a/b/c/g#s'),
|
||||
array('g?y#s', 'http://a/b/c/g?y#s'),
|
||||
array(';x', 'http://a/b/c/;x'),
|
||||
array('g;x', 'http://a/b/c/g;x'),
|
||||
array('g;x?y#s', 'http://a/b/c/g;x?y#s'),
|
||||
array('', 'http://a/b/c/d;p?q'),
|
||||
array('.', 'http://a/b/c/'),
|
||||
array('./', 'http://a/b/c/'),
|
||||
array('..', 'http://a/b/'),
|
||||
array('../', 'http://a/b/'),
|
||||
array('../g', 'http://a/b/g'),
|
||||
array('../..', 'http://a/'),
|
||||
array('../../', 'http://a/'),
|
||||
array('../../g', 'http://a/g'),
|
||||
// Abnormal
|
||||
array('../../../g', 'http://a/g'),
|
||||
array('../../../../g', 'http://a/g'),
|
||||
array('/./g', 'http://a/g'),
|
||||
array('/../g', 'http://a/g'),
|
||||
array('g.', 'http://a/b/c/g.'),
|
||||
array('.g', 'http://a/b/c/.g'),
|
||||
array('g..', 'http://a/b/c/g..'),
|
||||
array('..g', 'http://a/b/c/..g'),
|
||||
array('./../g', 'http://a/b/g'),
|
||||
array('./g/.', 'http://a/b/c/g/'),
|
||||
array('g/./h', 'http://a/b/c/g/h'),
|
||||
array('g/../h', 'http://a/b/c/h'),
|
||||
array('g;x=1/./y', 'http://a/b/c/g;x=1/y'),
|
||||
array('g;x=1/../y', 'http://a/b/c/y'),
|
||||
array('g?y/./x', 'http://a/b/c/g?y/./x'),
|
||||
array('g?y/../x', 'http://a/b/c/g?y/../x'),
|
||||
array('g#s/./x', 'http://a/b/c/g#s/./x'),
|
||||
array('g#s/../x', 'http://a/b/c/g#s/../x'),
|
||||
array('http:g', 'http:g'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider rfc3986_tests
|
||||
*/
|
||||
public function testStringRFC3986($relative, $expected)
|
||||
{
|
||||
$base = new Requests_IRI('http://a/b/c/d;p?q');
|
||||
$this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)->iri);
|
||||
$this->assertEquals($expected, (string) Requests_IRI::absolutize($base, $relative));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider rfc3986_tests
|
||||
*/
|
||||
public function testBothStringRFC3986($relative, $expected)
|
||||
{
|
||||
$base = 'http://a/b/c/d;p?q';
|
||||
$this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)->iri);
|
||||
$this->assertEquals($expected, (string) Requests_IRI::absolutize($base, $relative));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider rfc3986_tests
|
||||
*/
|
||||
public function testObjectRFC3986($relative, $expected)
|
||||
{
|
||||
$base = new Requests_IRI('http://a/b/c/d;p?q');
|
||||
$expected = new Requests_IRI($expected);
|
||||
$this->assertEquals($expected, Requests_IRI::absolutize($base, $relative));
|
||||
}
|
||||
|
||||
public static function sp_tests()
|
||||
{
|
||||
return array(
|
||||
array('http://a/b/c/d', 'f%0o', 'http://a/b/c/f%250o'),
|
||||
array('http://a/b/', 'c', 'http://a/b/c'),
|
||||
array('http://a/', 'b', 'http://a/b'),
|
||||
array('http://a/', '/b', 'http://a/b'),
|
||||
array('http://a/b', 'c', 'http://a/c'),
|
||||
array('http://a/b/', "c\x0Ad", 'http://a/b/c%0Ad'),
|
||||
array('http://a/b/', "c\x0A\x0B", 'http://a/b/c%0A%0B'),
|
||||
array('http://a/b/c', '//0', 'http://0/'),
|
||||
array('http://a/b/c', '0', 'http://a/b/0'),
|
||||
array('http://a/b/c', '?0', 'http://a/b/c?0'),
|
||||
array('http://a/b/c', '#0', 'http://a/b/c#0'),
|
||||
array('http://0/b/c', 'd', 'http://0/b/d'),
|
||||
array('http://a/b/c?0', 'd', 'http://a/b/d'),
|
||||
array('http://a/b/c#0', 'd', 'http://a/b/d'),
|
||||
array('http://example.com', '//example.net', 'http://example.net/'),
|
||||
array('http:g', 'a', 'http:a'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider sp_tests
|
||||
*/
|
||||
public function testStringSP($base, $relative, $expected)
|
||||
{
|
||||
$base = new Requests_IRI($base);
|
||||
$this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)->iri);
|
||||
$this->assertEquals($expected, (string) Requests_IRI::absolutize($base, $relative));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider sp_tests
|
||||
*/
|
||||
public function testObjectSP($base, $relative, $expected)
|
||||
{
|
||||
$base = new Requests_IRI($base);
|
||||
$expected = new Requests_IRI($expected);
|
||||
$this->assertEquals($expected, Requests_IRI::absolutize($base, $relative));
|
||||
}
|
||||
|
||||
public static function absolutize_tests()
|
||||
{
|
||||
return array(
|
||||
array('http://example.com/', 'foo/111:bar', 'http://example.com/foo/111:bar'),
|
||||
array('http://example.com/#foo', '', 'http://example.com/'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider absolutize_tests
|
||||
*/
|
||||
public function testAbsolutizeString($base, $relative, $expected)
|
||||
{
|
||||
$base = new Requests_IRI($base);
|
||||
$this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)->iri);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider absolutize_tests
|
||||
*/
|
||||
public function testAbsolutizeObject($base, $relative, $expected)
|
||||
{
|
||||
$base = new Requests_IRI($base);
|
||||
$expected = new Requests_IRI($expected);
|
||||
$this->assertEquals($expected, Requests_IRI::absolutize($base, $relative));
|
||||
}
|
||||
|
||||
public static function normalization_tests()
|
||||
{
|
||||
return array(
|
||||
array('example://a/b/c/%7Bfoo%7D', 'example://a/b/c/%7Bfoo%7D'),
|
||||
array('eXAMPLE://a/./b/../b/%63/%7bfoo%7d', 'example://a/b/c/%7Bfoo%7D'),
|
||||
array('example://%61/', 'example://a/'),
|
||||
array('example://%41/', 'example://a/'),
|
||||
array('example://A/', 'example://a/'),
|
||||
array('example://a/', 'example://a/'),
|
||||
array('example://%25A/', 'example://%25a/'),
|
||||
array('HTTP://EXAMPLE.com/', 'http://example.com/'),
|
||||
array('http://example.com/', 'http://example.com/'),
|
||||
array('http://example.com:', 'http://example.com/'),
|
||||
array('http://example.com:80', 'http://example.com/'),
|
||||
array('http://@example.com', 'http://@example.com/'),
|
||||
array('http://', 'http:///'),
|
||||
array('http://example.com?', 'http://example.com/?'),
|
||||
array('http://example.com#', 'http://example.com/#'),
|
||||
array('https://example.com/', 'https://example.com/'),
|
||||
array('https://example.com:', 'https://example.com/'),
|
||||
array('https://@example.com', 'https://@example.com/'),
|
||||
array('https://example.com?', 'https://example.com/?'),
|
||||
array('https://example.com#', 'https://example.com/#'),
|
||||
array('file://localhost/foobar', 'file:/foobar'),
|
||||
array('http://[0:0:0:0:0:0:0:1]', 'http://[::1]/'),
|
||||
array('http://[2001:db8:85a3:0000:0000:8a2e:370:7334]', 'http://[2001:db8:85a3::8a2e:370:7334]/'),
|
||||
array('http://[0:0:0:0:0:ffff:c0a8:a01]', 'http://[::ffff:c0a8:a01]/'),
|
||||
array('http://[ffff:0:0:0:0:0:0:0]', 'http://[ffff::]/'),
|
||||
array('http://[::ffff:192.0.2.128]', 'http://[::ffff:192.0.2.128]/'),
|
||||
array('http://[invalid]', 'http:'),
|
||||
array('http://[0:0:0:0:0:0:0:1]:', 'http://[::1]/'),
|
||||
array('http://[0:0:0:0:0:0:0:1]:80', 'http://[::1]/'),
|
||||
array('http://[0:0:0:0:0:0:0:1]:1234', 'http://[::1]:1234/'),
|
||||
// Punycode decoding helps with normalisation of IRIs, but is not
|
||||
// needed for URIs, so we don't really care about it for Requests
|
||||
//array('http://xn--tdali-d8a8w.lv', 'http://tūdaliņ.lv/'),
|
||||
//array('http://t%C5%ABdali%C5%86.lv', 'http://tūdaliņ.lv/'),
|
||||
array('http://Aa@example.com', 'http://Aa@example.com/'),
|
||||
array('http://example.com?Aa', 'http://example.com/?Aa'),
|
||||
array('http://example.com/Aa', 'http://example.com/Aa'),
|
||||
array('http://example.com#Aa', 'http://example.com/#Aa'),
|
||||
array('http://[0:0:0:0:0:0:0:0]', 'http://[::]/'),
|
||||
array('http:.', 'http:'),
|
||||
array('http:..', 'http:'),
|
||||
array('http:./', 'http:'),
|
||||
array('http:../', 'http:'),
|
||||
array('http://example.com/%3A', 'http://example.com/%3A'),
|
||||
array('http://example.com/:', 'http://example.com/:'),
|
||||
array('http://example.com/%C2', 'http://example.com/%C2'),
|
||||
array('http://example.com/%C2a', 'http://example.com/%C2a'),
|
||||
array('http://example.com/%C2%00', 'http://example.com/%C2%00'),
|
||||
array('http://example.com/%C3%A9', 'http://example.com/é'),
|
||||
array('http://example.com/%C3%A9%00', 'http://example.com/é%00'),
|
||||
array('http://example.com/%C3%A9cole', 'http://example.com/école'),
|
||||
array('http://example.com/%FF', 'http://example.com/%FF'),
|
||||
array("http://example.com/\xF3\xB0\x80\x80", 'http://example.com/%F3%B0%80%80'),
|
||||
array("http://example.com/\xF3\xB0\x80\x80%00", 'http://example.com/%F3%B0%80%80%00'),
|
||||
array("http://example.com/\xF3\xB0\x80\x80a", 'http://example.com/%F3%B0%80%80a'),
|
||||
array("http://example.com?\xF3\xB0\x80\x80", "http://example.com/?\xF3\xB0\x80\x80"),
|
||||
array("http://example.com?\xF3\xB0\x80\x80%00", "http://example.com/?\xF3\xB0\x80\x80%00"),
|
||||
array("http://example.com?\xF3\xB0\x80\x80a", "http://example.com/?\xF3\xB0\x80\x80a"),
|
||||
array("http://example.com/\xEE\x80\x80", 'http://example.com/%EE%80%80'),
|
||||
array("http://example.com/\xEE\x80\x80%00", 'http://example.com/%EE%80%80%00'),
|
||||
array("http://example.com/\xEE\x80\x80a", 'http://example.com/%EE%80%80a'),
|
||||
array("http://example.com?\xEE\x80\x80", "http://example.com/?\xEE\x80\x80"),
|
||||
array("http://example.com?\xEE\x80\x80%00", "http://example.com/?\xEE\x80\x80%00"),
|
||||
array("http://example.com?\xEE\x80\x80a", "http://example.com/?\xEE\x80\x80a"),
|
||||
array("http://example.com/\xC2", 'http://example.com/%C2'),
|
||||
array("http://example.com/\xC2a", 'http://example.com/%C2a'),
|
||||
array("http://example.com/\xC2\x00", 'http://example.com/%C2%00'),
|
||||
array("http://example.com/\xC3\xA9", 'http://example.com/é'),
|
||||
array("http://example.com/\xC3\xA9\x00", 'http://example.com/é%00'),
|
||||
array("http://example.com/\xC3\xA9cole", 'http://example.com/école'),
|
||||
array("http://example.com/\xFF", 'http://example.com/%FF'),
|
||||
array("http://example.com/\xFF%00", 'http://example.com/%FF%00'),
|
||||
array("http://example.com/\xFFa", 'http://example.com/%FFa'),
|
||||
array('http://example.com/%61', 'http://example.com/a'),
|
||||
array('http://example.com?%26', 'http://example.com/?%26'),
|
||||
array('http://example.com?%61', 'http://example.com/?a'),
|
||||
array('///', '///'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider normalization_tests
|
||||
*/
|
||||
public function testStringNormalization($input, $output)
|
||||
{
|
||||
$input = new Requests_IRI($input);
|
||||
$this->assertEquals($output, $input->iri);
|
||||
$this->assertEquals($output, (string) $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider normalization_tests
|
||||
*/
|
||||
public function testObjectNormalization($input, $output)
|
||||
{
|
||||
$input = new Requests_IRI($input);
|
||||
$output = new Requests_IRI($output);
|
||||
$this->assertEquals($output, $input);
|
||||
}
|
||||
|
||||
public static function equivalence_tests()
|
||||
{
|
||||
return array(
|
||||
array('http://É.com', 'http://%C3%89.com'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider equivalence_tests
|
||||
*/
|
||||
public function testObjectEquivalence($input, $output)
|
||||
{
|
||||
$input = new Requests_IRI($input);
|
||||
$output = new Requests_IRI($output);
|
||||
$this->assertEquals($output, $input);
|
||||
}
|
||||
|
||||
public static function not_equivalence_tests()
|
||||
{
|
||||
return array(
|
||||
array('http://example.com/foo/bar', 'http://example.com/foo%2Fbar'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider not_equivalence_tests
|
||||
*/
|
||||
public function testObjectNotEquivalence($input, $output)
|
||||
{
|
||||
$input = new Requests_IRI($input);
|
||||
$output = new Requests_IRI($output);
|
||||
$this->assertNotEquals($output, $input);
|
||||
}
|
||||
|
||||
public function testInvalidAbsolutizeBase()
|
||||
{
|
||||
$this->assertFalse(Requests_IRI::absolutize('://not a URL', '../'));
|
||||
}
|
||||
|
||||
public function testFullGamut()
|
||||
{
|
||||
$iri = new Requests_IRI();
|
||||
$iri->scheme = 'http';
|
||||
$iri->userinfo = 'user:password';
|
||||
$iri->host = 'example.com';
|
||||
$iri->path = '/test/';
|
||||
$iri->fragment = 'test';
|
||||
|
||||
$this->assertEquals('http', $iri->scheme);
|
||||
$this->assertEquals('user:password', $iri->userinfo);
|
||||
$this->assertEquals('example.com', $iri->host);
|
||||
$this->assertEquals(80, $iri->port);
|
||||
$this->assertEquals('/test/', $iri->path);
|
||||
$this->assertEquals('test', $iri->fragment);
|
||||
}
|
||||
|
||||
public function testReadAliased()
|
||||
{
|
||||
$iri = new Requests_IRI();
|
||||
$iri->scheme = 'http';
|
||||
$iri->userinfo = 'user:password';
|
||||
$iri->host = 'example.com';
|
||||
$iri->path = '/test/';
|
||||
$iri->fragment = 'test';
|
||||
|
||||
$this->assertEquals('http', $iri->ischeme);
|
||||
$this->assertEquals('user:password', $iri->iuserinfo);
|
||||
$this->assertEquals('example.com', $iri->ihost);
|
||||
$this->assertEquals(80, $iri->iport);
|
||||
$this->assertEquals('/test/', $iri->ipath);
|
||||
$this->assertEquals('test', $iri->ifragment);
|
||||
}
|
||||
|
||||
public function testWriteAliased()
|
||||
{
|
||||
$iri = new Requests_IRI();
|
||||
$iri->scheme = 'http';
|
||||
$iri->iuserinfo = 'user:password';
|
||||
$iri->ihost = 'example.com';
|
||||
$iri->ipath = '/test/';
|
||||
$iri->ifragment = 'test';
|
||||
|
||||
$this->assertEquals('http', $iri->scheme);
|
||||
$this->assertEquals('user:password', $iri->userinfo);
|
||||
$this->assertEquals('example.com', $iri->host);
|
||||
$this->assertEquals(80, $iri->port);
|
||||
$this->assertEquals('/test/', $iri->path);
|
||||
$this->assertEquals('test', $iri->fragment);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException PHPUnit_Framework_Error_Notice
|
||||
*/
|
||||
public function testNonexistantProperty()
|
||||
{
|
||||
$iri = new Requests_IRI();
|
||||
$this->assertFalse(isset($iri->nonexistant_prop));
|
||||
$should_fail = $iri->nonexistant_prop;
|
||||
}
|
||||
|
||||
public function testBlankHost()
|
||||
{
|
||||
$iri = new Requests_IRI('http://example.com/a/?b=c#d');
|
||||
$iri->host = null;
|
||||
|
||||
$this->assertEquals(null, $iri->host);
|
||||
$this->assertEquals('http:/a/?b=c#d', (string) $iri);
|
||||
}
|
||||
|
||||
public function testBadPort()
|
||||
{
|
||||
$iri = new Requests_IRI();
|
||||
$iri->port = 'example';
|
||||
|
||||
$this->assertEquals(null, $iri->port);
|
||||
}
|
||||
}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_Proxy_HTTP extends PHPUnit_Framework_TestCase {
|
||||
protected function checkProxyAvailable($type = '') {
|
||||
switch ($type) {
|
||||
case 'auth':
|
||||
$has_proxy = defined('REQUESTS_HTTP_PROXY_AUTH') && REQUESTS_HTTP_PROXY_AUTH;
|
||||
break;
|
||||
|
||||
default:
|
||||
$has_proxy = defined('REQUESTS_HTTP_PROXY') && REQUESTS_HTTP_PROXY;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$has_proxy) {
|
||||
$this->markTestSkipped('Proxy not available');
|
||||
}
|
||||
}
|
||||
|
||||
public function transportProvider() {
|
||||
return array(
|
||||
array('Requests_Transport_cURL'),
|
||||
array('Requests_Transport_fsockopen'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
*/
|
||||
public function testConnectWithString($transport) {
|
||||
$this->checkProxyAvailable();
|
||||
|
||||
$options = array(
|
||||
'proxy' => REQUESTS_HTTP_PROXY,
|
||||
'transport' => $transport,
|
||||
);
|
||||
$response = Requests::get(httpbin('/get'), array(), $options);
|
||||
$this->assertEquals('http', $response->headers['x-requests-proxied']);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertEquals('http', $data['headers']['x-requests-proxy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
*/
|
||||
public function testConnectWithArray($transport) {
|
||||
$this->checkProxyAvailable();
|
||||
|
||||
$options = array(
|
||||
'proxy' => array(REQUESTS_HTTP_PROXY),
|
||||
'transport' => $transport,
|
||||
);
|
||||
$response = Requests::get(httpbin('/get'), array(), $options);
|
||||
$this->assertEquals('http', $response->headers['x-requests-proxied']);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertEquals('http', $data['headers']['x-requests-proxy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testConnectInvalidParameters($transport) {
|
||||
$this->checkProxyAvailable();
|
||||
|
||||
$options = array(
|
||||
'proxy' => array(REQUESTS_HTTP_PROXY, 'testuser', 'password', 'something'),
|
||||
'transport' => $transport,
|
||||
);
|
||||
$response = Requests::get(httpbin('/get'), array(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
*/
|
||||
public function testConnectWithInstance($transport) {
|
||||
$this->checkProxyAvailable();
|
||||
|
||||
$options = array(
|
||||
'proxy' => new Requests_Proxy_HTTP(REQUESTS_HTTP_PROXY),
|
||||
'transport' => $transport,
|
||||
);
|
||||
$response = Requests::get(httpbin('/get'), array(), $options);
|
||||
$this->assertEquals('http', $response->headers['x-requests-proxied']);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertEquals('http', $data['headers']['x-requests-proxy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
*/
|
||||
public function testConnectWithAuth($transport) {
|
||||
$this->checkProxyAvailable('auth');
|
||||
|
||||
$options = array(
|
||||
'proxy' => array(
|
||||
REQUESTS_HTTP_PROXY_AUTH,
|
||||
REQUESTS_HTTP_PROXY_AUTH_USER,
|
||||
REQUESTS_HTTP_PROXY_AUTH_PASS
|
||||
),
|
||||
'transport' => $transport,
|
||||
);
|
||||
$response = Requests::get(httpbin('/get'), array(), $options);
|
||||
$this->assertEquals(200, $response->status_code);
|
||||
$this->assertEquals('http', $response->headers['x-requests-proxied']);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertEquals('http', $data['headers']['x-requests-proxy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transportProvider
|
||||
*/
|
||||
public function testConnectWithInvalidAuth($transport) {
|
||||
$this->checkProxyAvailable('auth');
|
||||
|
||||
$options = array(
|
||||
'proxy' => array(
|
||||
REQUESTS_HTTP_PROXY_AUTH,
|
||||
REQUESTS_HTTP_PROXY_AUTH_USER . '!',
|
||||
REQUESTS_HTTP_PROXY_AUTH_PASS . '!'
|
||||
),
|
||||
'transport' => $transport,
|
||||
);
|
||||
$response = Requests::get(httpbin('/get'), array(), $options);
|
||||
$this->assertEquals(407, $response->status_code);
|
||||
}
|
||||
}
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_Requests extends PHPUnit_Framework_TestCase {
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testInvalidProtocol() {
|
||||
$request = Requests::request('ftp://128.0.0.1/');
|
||||
}
|
||||
|
||||
public function testDefaultTransport() {
|
||||
$request = Requests::get(httpbin('/get'));
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard response header parsing
|
||||
*/
|
||||
public function testHeaderParsing() {
|
||||
$transport = new RawTransport();
|
||||
$transport->data =
|
||||
"HTTP/1.0 200 OK\r\n".
|
||||
"Host: localhost\r\n".
|
||||
"Host: ambiguous\r\n".
|
||||
"Nospace:here\r\n".
|
||||
"Muchspace: there \r\n".
|
||||
"Empty:\r\n".
|
||||
"Empty2: \r\n".
|
||||
"Folded: one\r\n".
|
||||
"\ttwo\r\n".
|
||||
" three\r\n\r\n".
|
||||
"stop\r\n";
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
$expected = new Requests_Response_Headers();
|
||||
$expected['host'] = 'localhost,ambiguous';
|
||||
$expected['nospace'] = 'here';
|
||||
$expected['muchspace'] = 'there';
|
||||
$expected['empty'] = '';
|
||||
$expected['empty2'] = '';
|
||||
$expected['folded'] = 'one two three';
|
||||
foreach ($expected as $key => $value) {
|
||||
$this->assertEquals($value, $response->headers[$key]);
|
||||
}
|
||||
|
||||
foreach ($response->headers as $key => $value) {
|
||||
$this->assertEquals($value, $expected[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
public function testProtocolVersionParsing() {
|
||||
$transport = new RawTransport();
|
||||
$transport->data =
|
||||
"HTTP/1.0 200 OK\r\n".
|
||||
"Host: localhost\r\n\r\n";
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
$this->assertEquals(1.0, $response->protocol_version);
|
||||
}
|
||||
|
||||
public function testRawAccess() {
|
||||
$transport = new RawTransport();
|
||||
$transport->data =
|
||||
"HTTP/1.0 200 OK\r\n".
|
||||
"Host: localhost\r\n\r\n".
|
||||
"Test";
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
$this->assertEquals($transport->data, $response->raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Headers with only \n delimiting should be treated as if they're \r\n
|
||||
*/
|
||||
public function testHeaderOnlyLF() {
|
||||
$transport = new RawTransport();
|
||||
$transport->data = "HTTP/1.0 200 OK\r\nTest: value\nAnother-Test: value\r\n\r\n";
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
$this->assertEquals('value', $response->headers['test']);
|
||||
$this->assertEquals('value', $response->headers['another-test']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that invalid protocols are not accepted
|
||||
*
|
||||
* We do not support HTTP/0.9. If this is really an issue for you, file a
|
||||
* new issue, and update your server/proxy to support a proper protocol.
|
||||
*
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testInvalidProtocolVersion() {
|
||||
$transport = new RawTransport();
|
||||
$transport->data = "HTTP/0.9 200 OK\r\n\r\n<p>Test";
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP/0.9 also appears to use a single CRLF instead of two
|
||||
*
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testSingleCRLFSeparator() {
|
||||
$transport = new RawTransport();
|
||||
$transport->data = "HTTP/0.9 200 OK\r\n<p>Test";
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testInvalidStatus() {
|
||||
$transport = new RawTransport();
|
||||
$transport->data = "HTTP/1.1 OK\r\nTest: value\nAnother-Test: value\r\n\r\nTest";
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
}
|
||||
|
||||
public function test30xWithoutLocation() {
|
||||
$transport = new MockTransport();
|
||||
$transport->code = 302;
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport
|
||||
);
|
||||
$response = Requests::get('http://example.com/', array(), $options);
|
||||
$this->assertEquals(302, $response->status_code);
|
||||
$this->assertEquals(0, $response->redirects);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testTimeoutException() {
|
||||
$options = array('timeout' => 0.5);
|
||||
$response = Requests::get(httpbin('/delay/3'), array(), $options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_Response_Headers extends PHPUnit_Framework_TestCase {
|
||||
public function testArrayAccess() {
|
||||
$headers = new Requests_Response_Headers();
|
||||
$headers['Content-Type'] = 'text/plain';
|
||||
|
||||
$this->assertEquals('text/plain', $headers['Content-Type']);
|
||||
}
|
||||
public function testCaseInsensitiveArrayAccess() {
|
||||
$headers = new Requests_Response_Headers();
|
||||
$headers['Content-Type'] = 'text/plain';
|
||||
|
||||
$this->assertEquals('text/plain', $headers['CONTENT-TYPE']);
|
||||
$this->assertEquals('text/plain', $headers['content-type']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testArrayAccess
|
||||
*/
|
||||
public function testIteration() {
|
||||
$headers = new Requests_Response_Headers();
|
||||
$headers['Content-Type'] = 'text/plain';
|
||||
$headers['Content-Length'] = 10;
|
||||
|
||||
foreach ($headers as $name => $value) {
|
||||
switch (strtolower($name)) {
|
||||
case 'content-type':
|
||||
$this->assertEquals('text/plain', $value);
|
||||
break;
|
||||
case 'content-length':
|
||||
$this->assertEquals(10, $value);
|
||||
break;
|
||||
default:
|
||||
throw new Exception('Invalid name: ' . $name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testInvalidKey() {
|
||||
$headers = new Requests_Response_Headers();
|
||||
$headers[] = 'text/plain';
|
||||
}
|
||||
|
||||
public function testMultipleHeaders() {
|
||||
$headers = new Requests_Response_Headers();
|
||||
$headers['Accept'] = 'text/html;q=1.0';
|
||||
$headers['Accept'] = '*/*;q=0.1';
|
||||
|
||||
$this->assertEquals('text/html;q=1.0,*/*;q=0.1', $headers['Accept']);
|
||||
}
|
||||
}
|
||||
Vendored
+108
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_SSL extends PHPUnit_Framework_TestCase {
|
||||
public static function domainMatchProvider() {
|
||||
return array(
|
||||
array('example.com', 'example.com'),
|
||||
array('test.example.com', 'test.example.com'),
|
||||
array('test.example.com', '*.example.com'),
|
||||
);
|
||||
}
|
||||
|
||||
public static function domainNoMatchProvider() {
|
||||
return array(
|
||||
// Check that we need at least 3 components
|
||||
array('com', '*'),
|
||||
array('example.com', '*.com'),
|
||||
|
||||
// Check that double wildcards don't work
|
||||
array('abc.def.example.com', '*.*.example.com'),
|
||||
|
||||
// Check that we only match with the correct number of components
|
||||
array('abc.def.example.com', 'def.example.com'),
|
||||
array('abc.def.example.com', '*.example.com'),
|
||||
|
||||
// Check that the wildcard only works as the full first component
|
||||
array('abc.def.example.com', 'a*.def.example.com'),
|
||||
|
||||
// Check that wildcards are not allowed for IPs
|
||||
array('192.168.0.1', '*.168.0.1'),
|
||||
array('192.168.0.1', '192.168.0.*'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider domainMatchProvider
|
||||
*/
|
||||
public function testMatch($base, $dnsname) {
|
||||
$this->assertTrue(Requests_SSL::match_domain($base, $dnsname));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider domainNoMatchProvider
|
||||
*/
|
||||
public function testNoMatch($base, $dnsname) {
|
||||
$this->assertFalse(Requests_SSL::match_domain($base, $dnsname));
|
||||
}
|
||||
|
||||
protected function fakeCertificate($dnsname, $with_san = true) {
|
||||
$certificate = array(
|
||||
'subject' => array(
|
||||
'CN' => $dnsname
|
||||
),
|
||||
);
|
||||
|
||||
if ($with_san !== false) {
|
||||
// If SAN is set to true, default it to the dNSName
|
||||
if ($with_san === true) {
|
||||
$with_san = $dnsname;
|
||||
}
|
||||
$certificate['extensions'] = array(
|
||||
'subjectAltName' => 'DNS: ' . $with_san,
|
||||
);
|
||||
}
|
||||
|
||||
return $certificate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider domainMatchProvider
|
||||
*/
|
||||
public function testMatchViaCertificate($base, $dnsname) {
|
||||
$certificate = $this->fakeCertificate($dnsname);
|
||||
$this->assertTrue(Requests_SSL::verify_certificate($base, $certificate));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider domainNoMatchProvider
|
||||
*/
|
||||
public function testNoMatchViaCertificate($base, $dnsname) {
|
||||
$certificate = $this->fakeCertificate($dnsname);
|
||||
$this->assertFalse(Requests_SSL::verify_certificate($base, $certificate));
|
||||
}
|
||||
|
||||
public function testCNFallback() {
|
||||
$certificate = $this->fakeCertificate('example.com', false);
|
||||
$this->assertTrue(Requests_SSL::verify_certificate('example.com', $certificate));
|
||||
}
|
||||
|
||||
public function testInvalidCNFallback() {
|
||||
$certificate = $this->fakeCertificate('example.com', false);
|
||||
$this->assertFalse(Requests_SSL::verify_certificate('example.net', $certificate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a certificate with both CN and SAN fields
|
||||
*
|
||||
* As per RFC2818, if the SAN field exists, we should parse that and ignore
|
||||
* the value of the CN field.
|
||||
*
|
||||
* @link http://tools.ietf.org/html/rfc2818#section-3.1
|
||||
*/
|
||||
public function testIgnoreCNWithSAN() {
|
||||
$certificate = $this->fakeCertificate('example.net', 'example.com');
|
||||
|
||||
$this->assertTrue(Requests_SSL::verify_certificate('example.com', $certificate), 'Checking SAN validation');
|
||||
$this->assertFalse(Requests_SSL::verify_certificate('example.net', $certificate), 'Checking CN non-validation');
|
||||
}
|
||||
}
|
||||
+213
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_Session extends PHPUnit_Framework_TestCase {
|
||||
public function testURLResolution() {
|
||||
$session = new Requests_Session(httpbin('/'));
|
||||
|
||||
// Set the cookies up
|
||||
$response = $session->get('/get');
|
||||
$this->assertTrue($response->success);
|
||||
$this->assertEquals(httpbin('/get'), $response->url);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertNotNull($data);
|
||||
$this->assertArrayHasKey('url', $data);
|
||||
$this->assertEquals(httpbin('/get'), $data['url']);
|
||||
}
|
||||
|
||||
public function testBasicGET() {
|
||||
$session_headers = array(
|
||||
'X-Requests-Session' => 'BasicGET',
|
||||
'X-Requests-Request' => 'notset',
|
||||
);
|
||||
$session = new Requests_Session(httpbin('/'), $session_headers);
|
||||
$response = $session->get('/get', array('X-Requests-Request' => 'GET'));
|
||||
$response->throw_for_status(false);
|
||||
$this->assertEquals(200, $response->status_code);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertArrayHasKey('X-Requests-Session', $data['headers']);
|
||||
$this->assertEquals('BasicGET', $data['headers']['X-Requests-Session']);
|
||||
$this->assertArrayHasKey('X-Requests-Request', $data['headers']);
|
||||
$this->assertEquals('GET', $data['headers']['X-Requests-Request']);
|
||||
}
|
||||
|
||||
public function testBasicHEAD() {
|
||||
$session_headers = array(
|
||||
'X-Requests-Session' => 'BasicHEAD',
|
||||
'X-Requests-Request' => 'notset',
|
||||
);
|
||||
$session = new Requests_Session(httpbin('/'), $session_headers);
|
||||
$response = $session->head('/get', array('X-Requests-Request' => 'HEAD'));
|
||||
$response->throw_for_status(false);
|
||||
$this->assertEquals(200, $response->status_code);
|
||||
}
|
||||
|
||||
public function testBasicDELETE() {
|
||||
$session_headers = array(
|
||||
'X-Requests-Session' => 'BasicDELETE',
|
||||
'X-Requests-Request' => 'notset',
|
||||
);
|
||||
$session = new Requests_Session(httpbin('/'), $session_headers);
|
||||
$response = $session->delete('/delete', array('X-Requests-Request' => 'DELETE'));
|
||||
$response->throw_for_status(false);
|
||||
$this->assertEquals(200, $response->status_code);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertArrayHasKey('X-Requests-Session', $data['headers']);
|
||||
$this->assertEquals('BasicDELETE', $data['headers']['X-Requests-Session']);
|
||||
$this->assertArrayHasKey('X-Requests-Request', $data['headers']);
|
||||
$this->assertEquals('DELETE', $data['headers']['X-Requests-Request']);
|
||||
}
|
||||
|
||||
public function testBasicPOST() {
|
||||
$session_headers = array(
|
||||
'X-Requests-Session' => 'BasicPOST',
|
||||
'X-Requests-Request' => 'notset',
|
||||
);
|
||||
$session = new Requests_Session(httpbin('/'), $session_headers);
|
||||
$response = $session->post('/post', array('X-Requests-Request' => 'POST'), array('postdata' => 'exists'));
|
||||
$response->throw_for_status(false);
|
||||
$this->assertEquals(200, $response->status_code);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertArrayHasKey('X-Requests-Session', $data['headers']);
|
||||
$this->assertEquals('BasicPOST', $data['headers']['X-Requests-Session']);
|
||||
$this->assertArrayHasKey('X-Requests-Request', $data['headers']);
|
||||
$this->assertEquals('POST', $data['headers']['X-Requests-Request']);
|
||||
}
|
||||
|
||||
public function testBasicPUT() {
|
||||
$session_headers = array(
|
||||
'X-Requests-Session' => 'BasicPUT',
|
||||
'X-Requests-Request' => 'notset',
|
||||
);
|
||||
$session = new Requests_Session(httpbin('/'), $session_headers);
|
||||
$response = $session->put('/put', array('X-Requests-Request' => 'PUT'), array('postdata' => 'exists'));
|
||||
$response->throw_for_status(false);
|
||||
$this->assertEquals(200, $response->status_code);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertArrayHasKey('X-Requests-Session', $data['headers']);
|
||||
$this->assertEquals('BasicPUT', $data['headers']['X-Requests-Session']);
|
||||
$this->assertArrayHasKey('X-Requests-Request', $data['headers']);
|
||||
$this->assertEquals('PUT', $data['headers']['X-Requests-Request']);
|
||||
}
|
||||
|
||||
public function testBasicPATCH() {
|
||||
$session_headers = array(
|
||||
'X-Requests-Session' => 'BasicPATCH',
|
||||
'X-Requests-Request' => 'notset',
|
||||
);
|
||||
$session = new Requests_Session(httpbin('/'), $session_headers);
|
||||
$response = $session->patch('/patch', array('X-Requests-Request' => 'PATCH'), array('postdata' => 'exists'));
|
||||
$response->throw_for_status(false);
|
||||
$this->assertEquals(200, $response->status_code);
|
||||
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertArrayHasKey('X-Requests-Session', $data['headers']);
|
||||
$this->assertEquals('BasicPATCH', $data['headers']['X-Requests-Session']);
|
||||
$this->assertArrayHasKey('X-Requests-Request', $data['headers']);
|
||||
$this->assertEquals('PATCH', $data['headers']['X-Requests-Request']);
|
||||
}
|
||||
|
||||
public function testMultiple() {
|
||||
$session = new Requests_Session(httpbin('/'), array('X-Requests-Session' => 'Multiple'));
|
||||
$requests = array(
|
||||
'test1' => array(
|
||||
'url' => httpbin('/get')
|
||||
),
|
||||
'test2' => array(
|
||||
'url' => httpbin('/get')
|
||||
),
|
||||
);
|
||||
$responses = $session->request_multiple($requests);
|
||||
|
||||
// test1
|
||||
$this->assertNotEmpty($responses['test1']);
|
||||
$this->assertInstanceOf('Requests_Response', $responses['test1']);
|
||||
$this->assertEquals(200, $responses['test1']->status_code);
|
||||
|
||||
$result = json_decode($responses['test1']->body, true);
|
||||
$this->assertEquals(httpbin('/get'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
|
||||
// test2
|
||||
$this->assertNotEmpty($responses['test2']);
|
||||
$this->assertInstanceOf('Requests_Response', $responses['test2']);
|
||||
$this->assertEquals(200, $responses['test2']->status_code);
|
||||
|
||||
$result = json_decode($responses['test2']->body, true);
|
||||
$this->assertEquals(httpbin('/get'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
}
|
||||
|
||||
public function testPropertyUsage() {
|
||||
$headers = array(
|
||||
'X-TestHeader' => 'testing',
|
||||
'X-TestHeader2' => 'requests-test'
|
||||
);
|
||||
$data = array(
|
||||
'testdata' => 'value1',
|
||||
'test2' => 'value2',
|
||||
'test3' => array(
|
||||
'foo' => 'bar',
|
||||
'abc' => 'xyz'
|
||||
)
|
||||
);
|
||||
$options = array(
|
||||
'testoption' => 'test',
|
||||
'foo' => 'bar'
|
||||
);
|
||||
|
||||
$session = new Requests_Session('http://example.com/', $headers, $data, $options);
|
||||
$this->assertEquals('http://example.com/', $session->url);
|
||||
$this->assertEquals($headers, $session->headers);
|
||||
$this->assertEquals($data, $session->data);
|
||||
$this->assertEquals($options['testoption'], $session->options['testoption']);
|
||||
|
||||
// Test via property access
|
||||
$this->assertEquals($options['testoption'], $session->testoption);
|
||||
|
||||
// Test setting new property
|
||||
$session->newoption = 'foobar';
|
||||
$options['newoption'] = 'foobar';
|
||||
$this->assertEquals($options['newoption'], $session->options['newoption']);
|
||||
|
||||
// Test unsetting property
|
||||
unset($session->newoption);
|
||||
$this->assertFalse(isset($session->newoption));
|
||||
|
||||
// Update property
|
||||
$session->testoption = 'foobar';
|
||||
$options['testoption'] = 'foobar';
|
||||
$this->assertEquals($options['testoption'], $session->testoption);
|
||||
|
||||
// Test getting invalid property
|
||||
$this->assertNull($session->invalidoption);
|
||||
}
|
||||
|
||||
public function testSharedCookies() {
|
||||
$session = new Requests_Session(httpbin('/'));
|
||||
|
||||
$options = array(
|
||||
'follow_redirects' => false
|
||||
);
|
||||
$response = $session->get('/cookies/set?requests-testcookie=testvalue', array(), $options);
|
||||
$this->assertEquals(302, $response->status_code);
|
||||
|
||||
// Check the cookies
|
||||
$response = $session->get('/cookies');
|
||||
$this->assertTrue($response->success);
|
||||
|
||||
// Check the response
|
||||
$data = json_decode($response->body, true);
|
||||
$this->assertNotNull($data);
|
||||
$this->assertArrayHasKey('cookies', $data);
|
||||
|
||||
$cookies = array(
|
||||
'requests-testcookie' => 'testvalue'
|
||||
);
|
||||
$this->assertEquals($cookies, $data['cookies']);
|
||||
}
|
||||
}
|
||||
+826
@@ -0,0 +1,826 @@
|
||||
<?php
|
||||
|
||||
abstract class RequestsTest_Transport_Base extends PHPUnit_Framework_TestCase {
|
||||
public function setUp() {
|
||||
$callback = array($this->transport, 'test');
|
||||
$supported = call_user_func($callback);
|
||||
|
||||
if (!$supported) {
|
||||
$this->markTestSkipped($this->transport . ' is not available');
|
||||
return;
|
||||
}
|
||||
|
||||
$ssl_supported = call_user_func($callback, array('ssl' => true));
|
||||
if (!$ssl_supported) {
|
||||
$this->skip_https = true;
|
||||
}
|
||||
}
|
||||
protected $skip_https = false;
|
||||
|
||||
protected function getOptions($other = array()) {
|
||||
$options = array(
|
||||
'transport' => $this->transport
|
||||
);
|
||||
$options = array_merge($options, $other);
|
||||
return $options;
|
||||
}
|
||||
|
||||
public function testResponseByteLimit() {
|
||||
$limit = 104;
|
||||
$options = array(
|
||||
'max_bytes' => $limit,
|
||||
);
|
||||
$response = Requests::get(httpbin('/bytes/325'), array(), $this->getOptions($options));
|
||||
$this->assertEquals($limit, strlen($response->body));
|
||||
}
|
||||
|
||||
public function testResponseByteLimitWithFile() {
|
||||
$limit = 300;
|
||||
$options = array(
|
||||
'max_bytes' => $limit,
|
||||
'filename' => tempnam(sys_get_temp_dir(), 'RLT') // RequestsLibraryTest
|
||||
);
|
||||
$response = Requests::get(httpbin('/bytes/482'), array(), $this->getOptions($options));
|
||||
$this->assertEmpty($response->body);
|
||||
$this->assertEquals($limit, filesize($options['filename']));
|
||||
unlink($options['filename']);
|
||||
}
|
||||
|
||||
public function testSimpleGET() {
|
||||
$request = Requests::get(httpbin('/get'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/get'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
}
|
||||
|
||||
public function testGETWithArgs() {
|
||||
$request = Requests::get(httpbin('/get?test=true&test2=test'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/get?test=true&test2=test'), $result['url']);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
|
||||
}
|
||||
|
||||
public function testGETWithData() {
|
||||
$data = array(
|
||||
'test' => 'true',
|
||||
'test2' => 'test',
|
||||
);
|
||||
$request = Requests::request(httpbin('/get'), array(), $data, Requests::GET, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/get?test=true&test2=test'), $result['url']);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
|
||||
}
|
||||
|
||||
public function testGETWithNestedData() {
|
||||
$data = array(
|
||||
'test' => 'true',
|
||||
'test2' => array(
|
||||
'test3' => 'test',
|
||||
'test4' => 'test-too',
|
||||
),
|
||||
);
|
||||
$request = Requests::request(httpbin('/get'), array(), $data, Requests::GET, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/get?test=true&test2%5Btest3%5D=test&test2%5Btest4%5D=test-too'), $result['url']);
|
||||
$this->assertEquals(array('test' => 'true', 'test2[test3]' => 'test', 'test2[test4]' => 'test-too'), $result['args']);
|
||||
}
|
||||
|
||||
public function testGETWithDataAndQuery() {
|
||||
$data = array(
|
||||
'test2' => 'test',
|
||||
);
|
||||
$request = Requests::request(httpbin('/get?test=true'), array(), $data, Requests::GET, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/get?test=true&test2=test'), $result['url']);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
|
||||
}
|
||||
|
||||
public function testGETWithHeaders() {
|
||||
$headers = array(
|
||||
'Requested-At' => time(),
|
||||
);
|
||||
$request = Requests::get(httpbin('/get'), $headers, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals($headers['Requested-At'], $result['headers']['Requested-At']);
|
||||
}
|
||||
|
||||
public function testChunked() {
|
||||
$request = Requests::get(httpbin('/stream/1'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/stream/1'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
}
|
||||
|
||||
public function testHEAD() {
|
||||
$request = Requests::head(httpbin('/get'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
$this->assertEquals('', $request->body);
|
||||
}
|
||||
|
||||
public function testTRACE() {
|
||||
$request = Requests::trace(httpbin('/trace'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
}
|
||||
|
||||
public function testRawPOST() {
|
||||
$data = 'test';
|
||||
$request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals('test', $result['data']);
|
||||
}
|
||||
|
||||
public function testFormPost() {
|
||||
$data = 'test=true&test2=test';
|
||||
$request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
|
||||
}
|
||||
|
||||
public function testPOSTWithArray() {
|
||||
$data = array(
|
||||
'test' => 'true',
|
||||
'test2' => 'test',
|
||||
);
|
||||
$request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
|
||||
}
|
||||
|
||||
public function testPOSTWithNestedData() {
|
||||
$data = array(
|
||||
'test' => 'true',
|
||||
'test2' => array(
|
||||
'test3' => 'test',
|
||||
'test4' => 'test-too',
|
||||
),
|
||||
);
|
||||
$request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(array('test' => 'true', 'test2[test3]' => 'test', 'test2[test4]' => 'test-too'), $result['form']);
|
||||
}
|
||||
|
||||
public function testRawPUT() {
|
||||
$data = 'test';
|
||||
$request = Requests::put(httpbin('/put'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals('test', $result['data']);
|
||||
}
|
||||
|
||||
public function testFormPUT() {
|
||||
$data = 'test=true&test2=test';
|
||||
$request = Requests::put(httpbin('/put'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
|
||||
}
|
||||
|
||||
public function testPUTWithArray() {
|
||||
$data = array(
|
||||
'test' => 'true',
|
||||
'test2' => 'test',
|
||||
);
|
||||
$request = Requests::put(httpbin('/put'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
|
||||
}
|
||||
|
||||
public function testRawPATCH() {
|
||||
$data = 'test';
|
||||
$request = Requests::patch(httpbin('/patch'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals('test', $result['data']);
|
||||
}
|
||||
|
||||
public function testFormPATCH() {
|
||||
$data = 'test=true&test2=test';
|
||||
$request = Requests::patch(httpbin('/patch'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code, $request->body);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
|
||||
}
|
||||
|
||||
public function testPATCHWithArray() {
|
||||
$data = array(
|
||||
'test' => 'true',
|
||||
'test2' => 'test',
|
||||
);
|
||||
$request = Requests::patch(httpbin('/patch'), array(), $data, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
|
||||
}
|
||||
|
||||
public function testOPTIONS() {
|
||||
$request = Requests::options(httpbin('/options'), array(), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
}
|
||||
|
||||
public function testDELETE() {
|
||||
$request = Requests::delete(httpbin('/delete'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/delete'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
}
|
||||
|
||||
public function testDELETEWithData() {
|
||||
$data = array(
|
||||
'test' => 'true',
|
||||
'test2' => 'test',
|
||||
);
|
||||
$request = Requests::request(httpbin('/delete'), array(), $data, Requests::DELETE, $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/delete?test=true&test2=test'), $result['url']);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['args']);
|
||||
}
|
||||
|
||||
public function testLOCK() {
|
||||
$request = Requests::request(httpbin('/lock'), array(), array(), 'LOCK', $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
}
|
||||
|
||||
public function testLOCKWithData() {
|
||||
$data = array(
|
||||
'test' => 'true',
|
||||
'test2' => 'test',
|
||||
);
|
||||
$request = Requests::request(httpbin('/lock'), array(), $data, 'LOCK', $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
|
||||
}
|
||||
|
||||
public function testRedirects() {
|
||||
$request = Requests::get(httpbin('/redirect/6'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$this->assertEquals(6, $request->redirects);
|
||||
}
|
||||
|
||||
public function testRelativeRedirects() {
|
||||
$request = Requests::get(httpbin('/relative-redirect/6'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$this->assertEquals(6, $request->redirects);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
* @todo This should also check that the type is "toomanyredirects"
|
||||
*/
|
||||
public function testTooManyRedirects() {
|
||||
$options = array(
|
||||
'redirects' => 10, // default, but force just in case
|
||||
);
|
||||
$request = Requests::get(httpbin('/redirect/11'), array(), $this->getOptions($options));
|
||||
}
|
||||
|
||||
public static function statusCodeSuccessProvider() {
|
||||
return array(
|
||||
array(200, true),
|
||||
array(201, true),
|
||||
array(202, true),
|
||||
array(203, true),
|
||||
array(204, true),
|
||||
array(205, true),
|
||||
array(206, true),
|
||||
array(300, false),
|
||||
array(301, false),
|
||||
array(302, false),
|
||||
array(303, false),
|
||||
array(304, false),
|
||||
array(305, false),
|
||||
array(306, false),
|
||||
array(307, false),
|
||||
array(400, false),
|
||||
array(401, false),
|
||||
array(402, false),
|
||||
array(403, false),
|
||||
array(404, false),
|
||||
array(405, false),
|
||||
array(406, false),
|
||||
array(407, false),
|
||||
array(408, false),
|
||||
array(409, false),
|
||||
array(410, false),
|
||||
array(411, false),
|
||||
array(412, false),
|
||||
array(413, false),
|
||||
array(414, false),
|
||||
array(415, false),
|
||||
array(416, false),
|
||||
array(417, false),
|
||||
array(418, false), // RFC 2324
|
||||
array(428, false), // RFC 6585
|
||||
array(429, false), // RFC 6585
|
||||
array(431, false), // RFC 6585
|
||||
array(500, false),
|
||||
array(501, false),
|
||||
array(502, false),
|
||||
array(503, false),
|
||||
array(504, false),
|
||||
array(505, false),
|
||||
array(511, false), // RFC 6585
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider statusCodeSuccessProvider
|
||||
*/
|
||||
public function testStatusCode($code, $success) {
|
||||
$transport = new MockTransport();
|
||||
$transport->code = $code;
|
||||
|
||||
$url = sprintf(httpbin('/status/%d'), $code);
|
||||
|
||||
$options = array(
|
||||
'follow_redirects' => false,
|
||||
'transport' => $transport,
|
||||
);
|
||||
$request = Requests::get($url, array(), $options);
|
||||
$this->assertEquals($code, $request->status_code);
|
||||
$this->assertEquals($success, $request->success);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider statusCodeSuccessProvider
|
||||
*/
|
||||
public function testStatusCodeThrow($code, $success) {
|
||||
$transport = new MockTransport();
|
||||
$transport->code = $code;
|
||||
|
||||
$url = sprintf(httpbin('/status/%d'), $code);
|
||||
$options = array(
|
||||
'follow_redirects' => false,
|
||||
'transport' => $transport,
|
||||
);
|
||||
|
||||
if (!$success) {
|
||||
if ($code >= 400) {
|
||||
$this->setExpectedException('Requests_Exception_HTTP_' . $code, '', $code);
|
||||
}
|
||||
elseif ($code >= 300 && $code < 400) {
|
||||
$this->setExpectedException('Requests_Exception');
|
||||
}
|
||||
}
|
||||
$request = Requests::get($url, array(), $options);
|
||||
$request->throw_for_status(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider statusCodeSuccessProvider
|
||||
*/
|
||||
public function testStatusCodeThrowAllowRedirects($code, $success) {
|
||||
$transport = new MockTransport();
|
||||
$transport->code = $code;
|
||||
|
||||
$url = sprintf(httpbin('/status/%d'), $code);
|
||||
$options = array(
|
||||
'follow_redirects' => false,
|
||||
'transport' => $transport,
|
||||
);
|
||||
|
||||
if (!$success) {
|
||||
if ($code >= 400 || $code === 304 || $code === 305 || $code === 306) {
|
||||
$this->setExpectedException('Requests_Exception_HTTP_' . $code, '', $code);
|
||||
}
|
||||
}
|
||||
$request = Requests::get($url, array(), $options);
|
||||
$request->throw_for_status(true);
|
||||
}
|
||||
|
||||
public function testStatusCodeUnknown(){
|
||||
$transport = new MockTransport();
|
||||
$transport->code = 599;
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport,
|
||||
);
|
||||
|
||||
$request = Requests::get(httpbin('/status/599'), array(), $options);
|
||||
$this->assertEquals(599, $request->status_code);
|
||||
$this->assertEquals(false, $request->success);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception_HTTP_Unknown
|
||||
*/
|
||||
public function testStatusCodeThrowUnknown(){
|
||||
$transport = new MockTransport();
|
||||
$transport->code = 599;
|
||||
|
||||
$options = array(
|
||||
'transport' => $transport,
|
||||
);
|
||||
|
||||
$request = Requests::get(httpbin('/status/599'), array(), $options);
|
||||
$request->throw_for_status(true);
|
||||
}
|
||||
|
||||
public function testGzipped() {
|
||||
$request = Requests::get(httpbin('/gzip'), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body);
|
||||
$this->assertEquals(true, $result->gzipped);
|
||||
}
|
||||
|
||||
public function testStreamToFile() {
|
||||
$options = array(
|
||||
'filename' => tempnam(sys_get_temp_dir(), 'RLT') // RequestsLibraryTest
|
||||
);
|
||||
$request = Requests::get(httpbin('/get'), array(), $this->getOptions($options));
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
$this->assertEmpty($request->body);
|
||||
|
||||
$contents = file_get_contents($options['filename']);
|
||||
$result = json_decode($contents, true);
|
||||
$this->assertEquals(httpbin('/get'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
|
||||
unlink($options['filename']);
|
||||
}
|
||||
|
||||
public function testNonblocking() {
|
||||
$options = array(
|
||||
'blocking' => false
|
||||
);
|
||||
$request = Requests::get(httpbin('/get'), array(), $this->getOptions($options));
|
||||
$empty = new Requests_Response();
|
||||
$this->assertEquals($empty, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testBadIP() {
|
||||
$request = Requests::get('http://256.256.256.0/', array(), $this->getOptions());
|
||||
}
|
||||
|
||||
public function testHTTPS() {
|
||||
if ($this->skip_https) {
|
||||
$this->markTestSkipped('SSL support is not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
$request = Requests::get(httpbin('/get', true), array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
// Disable, since httpbin always returns http
|
||||
// $this->assertEquals(httpbin('/get', true), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testExpiredHTTPS() {
|
||||
if ($this->skip_https) {
|
||||
$this->markTestSkipped('SSL support is not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
$request = Requests::get('https://testssl-expire.disig.sk/index.en.html', array(), $this->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testRevokedHTTPS() {
|
||||
if ($this->skip_https) {
|
||||
$this->markTestSkipped('SSL support is not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
$request = Requests::get('https://testssl-revoked.disig.sk/index.en.html', array(), $this->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that SSL fails with a bad certificate
|
||||
*
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testBadDomain() {
|
||||
if ($this->skip_https) {
|
||||
$this->markTestSkipped('SSL support is not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
$request = Requests::head('https://wrong.host.badssl.com/', array(), $this->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the transport supports Server Name Indication with HTTPS
|
||||
*
|
||||
* badssl.com is used for SSL testing, and the common name is set to
|
||||
* `*.badssl.com` as such. Without alternate name support, this will fail
|
||||
* as `badssl.com` is only in the alternate name
|
||||
*/
|
||||
public function testAlternateNameSupport() {
|
||||
if ($this->skip_https) {
|
||||
$this->markTestSkipped('SSL support is not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
$request = Requests::head('https://badssl.com/', array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the transport supports Server Name Indication with HTTPS
|
||||
*
|
||||
* feelingrestful.com (owned by hmn.md and used with permission) points to
|
||||
* CloudFlare, and will fail if SNI isn't sent.
|
||||
*/
|
||||
public function testSNISupport() {
|
||||
if ($this->skip_https) {
|
||||
$this->markTestSkipped('SSL support is not available.');
|
||||
return;
|
||||
}
|
||||
|
||||
$request = Requests::head('https://feelingrestful.com/', array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Requests_Exception
|
||||
*/
|
||||
public function testTimeout() {
|
||||
$options = array(
|
||||
'timeout' => 1,
|
||||
);
|
||||
$request = Requests::get(httpbin('/delay/10'), array(), $this->getOptions($options));
|
||||
var_dump($request);
|
||||
}
|
||||
|
||||
public function testMultiple() {
|
||||
$requests = array(
|
||||
'test1' => array(
|
||||
'url' => httpbin('/get')
|
||||
),
|
||||
'test2' => array(
|
||||
'url' => httpbin('/get')
|
||||
),
|
||||
);
|
||||
$responses = Requests::request_multiple($requests, $this->getOptions());
|
||||
|
||||
// test1
|
||||
$this->assertNotEmpty($responses['test1']);
|
||||
$this->assertInstanceOf('Requests_Response', $responses['test1']);
|
||||
$this->assertEquals(200, $responses['test1']->status_code);
|
||||
|
||||
$result = json_decode($responses['test1']->body, true);
|
||||
$this->assertEquals(httpbin('/get'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
|
||||
// test2
|
||||
$this->assertNotEmpty($responses['test2']);
|
||||
$this->assertInstanceOf('Requests_Response', $responses['test2']);
|
||||
$this->assertEquals(200, $responses['test2']->status_code);
|
||||
|
||||
$result = json_decode($responses['test2']->body, true);
|
||||
$this->assertEquals(httpbin('/get'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
}
|
||||
|
||||
public function testMultipleWithDifferingMethods() {
|
||||
$requests = array(
|
||||
'get' => array(
|
||||
'url' => httpbin('/get'),
|
||||
),
|
||||
'post' => array(
|
||||
'url' => httpbin('/post'),
|
||||
'type' => Requests::POST,
|
||||
'data' => 'test',
|
||||
),
|
||||
);
|
||||
$responses = Requests::request_multiple($requests, $this->getOptions());
|
||||
|
||||
// get
|
||||
$this->assertEquals(200, $responses['get']->status_code);
|
||||
|
||||
// post
|
||||
$this->assertEquals(200, $responses['post']->status_code);
|
||||
$result = json_decode($responses['post']->body, true);
|
||||
$this->assertEquals('test', $result['data']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testTimeout
|
||||
*/
|
||||
public function testMultipleWithFailure() {
|
||||
$requests = array(
|
||||
'success' => array(
|
||||
'url' => httpbin('/get'),
|
||||
),
|
||||
'timeout' => array(
|
||||
'url' => httpbin('/delay/10'),
|
||||
'options' => array(
|
||||
'timeout' => 1,
|
||||
),
|
||||
),
|
||||
);
|
||||
$responses = Requests::request_multiple($requests, $this->getOptions());
|
||||
$this->assertEquals(200, $responses['success']->status_code);
|
||||
$this->assertInstanceOf('Requests_Exception', $responses['timeout']);
|
||||
}
|
||||
|
||||
public function testMultipleUsingCallback() {
|
||||
$requests = array(
|
||||
'get' => array(
|
||||
'url' => httpbin('/get'),
|
||||
),
|
||||
'post' => array(
|
||||
'url' => httpbin('/post'),
|
||||
'type' => Requests::POST,
|
||||
'data' => 'test',
|
||||
),
|
||||
);
|
||||
$this->completed = array();
|
||||
$options = array(
|
||||
'complete' => array($this, 'completeCallback'),
|
||||
);
|
||||
$responses = Requests::request_multiple($requests, $this->getOptions($options));
|
||||
|
||||
$this->assertEquals($this->completed, $responses);
|
||||
$this->completed = array();
|
||||
}
|
||||
|
||||
public function testMultipleUsingCallbackAndFailure() {
|
||||
$requests = array(
|
||||
'success' => array(
|
||||
'url' => httpbin('/get'),
|
||||
),
|
||||
'timeout' => array(
|
||||
'url' => httpbin('/delay/10'),
|
||||
'options' => array(
|
||||
'timeout' => 1,
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->completed = array();
|
||||
$options = array(
|
||||
'complete' => array($this, 'completeCallback'),
|
||||
);
|
||||
$responses = Requests::request_multiple($requests, $this->getOptions($options));
|
||||
|
||||
$this->assertEquals($this->completed, $responses);
|
||||
$this->completed = array();
|
||||
}
|
||||
|
||||
public function completeCallback($response, $key) {
|
||||
$this->completed[$key] = $response;
|
||||
}
|
||||
|
||||
public function testMultipleToFile() {
|
||||
$requests = array(
|
||||
'get' => array(
|
||||
'url' => httpbin('/get'),
|
||||
'options' => array(
|
||||
'filename' => tempnam(sys_get_temp_dir(), 'RLT') // RequestsLibraryTest
|
||||
),
|
||||
),
|
||||
'post' => array(
|
||||
'url' => httpbin('/post'),
|
||||
'type' => Requests::POST,
|
||||
'data' => 'test',
|
||||
'options' => array(
|
||||
'filename' => tempnam(sys_get_temp_dir(), 'RLT') // RequestsLibraryTest
|
||||
),
|
||||
),
|
||||
);
|
||||
$responses = Requests::request_multiple($requests, $this->getOptions());
|
||||
|
||||
// GET request
|
||||
$contents = file_get_contents($requests['get']['options']['filename']);
|
||||
$result = json_decode($contents, true);
|
||||
$this->assertEquals(httpbin('/get'), $result['url']);
|
||||
$this->assertEmpty($result['args']);
|
||||
unlink($requests['get']['options']['filename']);
|
||||
|
||||
// POST request
|
||||
$contents = file_get_contents($requests['post']['options']['filename']);
|
||||
$result = json_decode($contents, true);
|
||||
$this->assertEquals(httpbin('/post'), $result['url']);
|
||||
$this->assertEquals('test', $result['data']);
|
||||
unlink($requests['post']['options']['filename']);
|
||||
}
|
||||
|
||||
public function testAlternatePort() {
|
||||
$request = Requests::get('http://portquiz.net:8080/', array(), $this->getOptions());
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
$num = preg_match('#You have reached this page on port <b>(\d+)</b>#i', $request->body, $matches);
|
||||
$this->assertEquals(1, $num, 'Response should contain the port number');
|
||||
$this->assertEquals(8080, $matches[1]);
|
||||
}
|
||||
|
||||
public function testProgressCallback() {
|
||||
$mock = $this->getMockBuilder('stdClass')->setMethods(array('progress'))->getMock();
|
||||
$mock->expects($this->atLeastOnce())->method('progress');
|
||||
$hooks = new Requests_Hooks();
|
||||
$hooks->register('request.progress', array($mock, 'progress'));
|
||||
$options = array(
|
||||
'hooks' => $hooks,
|
||||
);
|
||||
$options = $this->getOptions($options);
|
||||
|
||||
$response = Requests::get(httpbin('/get'), array(), $options);
|
||||
}
|
||||
|
||||
public function testAfterRequestCallback() {
|
||||
$mock = $this->getMockBuilder('stdClass')
|
||||
->setMethods(array('after_request'))
|
||||
->getMock();
|
||||
|
||||
$mock->expects($this->atLeastOnce())
|
||||
->method('after_request')
|
||||
->with(
|
||||
$this->isType('string'),
|
||||
$this->logicalAnd($this->isType('array'), $this->logicalNot($this->isEmpty()))
|
||||
);
|
||||
$hooks = new Requests_Hooks();
|
||||
$hooks->register('curl.after_request', array($mock, 'after_request'));
|
||||
$hooks->register('fsockopen.after_request', array($mock, 'after_request'));
|
||||
$options = array(
|
||||
'hooks' => $hooks,
|
||||
);
|
||||
$options = $this->getOptions($options);
|
||||
|
||||
$response = Requests::get(httpbin('/get'), array(), $options);
|
||||
}
|
||||
|
||||
public function testReusableTransport() {
|
||||
$options = $this->getOptions(array('transport' => new $this->transport()));
|
||||
|
||||
$request1 = Requests::get(httpbin('/get'), array(), $options);
|
||||
$request2 = Requests::get(httpbin('/get'), array(), $options);
|
||||
|
||||
$this->assertEquals(200, $request1->status_code);
|
||||
$this->assertEquals(200, $request2->status_code);
|
||||
|
||||
$result1 = json_decode($request1->body, true);
|
||||
$result2 = json_decode($request2->body, true);
|
||||
|
||||
$this->assertEquals(httpbin('/get'), $result1['url']);
|
||||
$this->assertEquals(httpbin('/get'), $result2['url']);
|
||||
|
||||
$this->assertEmpty($result1['args']);
|
||||
$this->assertEmpty($result2['args']);
|
||||
}
|
||||
|
||||
public function testQueryDataFormat() {
|
||||
$data = array('test' => 'true', 'test2' => 'test');
|
||||
$request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions(array('data_format' => 'query')));
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/post').'?test=true&test2=test', $result['url']);
|
||||
$this->assertEquals('', $result['data']);
|
||||
}
|
||||
|
||||
public function testBodyDataFormat() {
|
||||
$data = array('test' => 'true', 'test2' => 'test');
|
||||
$request = Requests::post(httpbin('/post'), array(), $data, $this->getOptions(array('data_format' => 'body')));
|
||||
$this->assertEquals(200, $request->status_code);
|
||||
|
||||
$result = json_decode($request->body, true);
|
||||
$this->assertEquals(httpbin('/post'), $result['url']);
|
||||
$this->assertEquals(array('test' => 'true', 'test2' => 'test'), $result['form']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_Transport_cURL extends RequestsTest_Transport_Base {
|
||||
protected $transport = 'Requests_Transport_cURL';
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class RequestsTest_Transport_fsockopen extends RequestsTest_Transport_Base {
|
||||
protected $transport = 'Requests_Transport_fsockopen';
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
date_default_timezone_set('UTC');
|
||||
|
||||
function define_from_env($name, $default = false) {
|
||||
$env = getenv($name);
|
||||
if ($env) {
|
||||
define($name, $env);
|
||||
}
|
||||
else {
|
||||
define($name, $default);
|
||||
}
|
||||
}
|
||||
|
||||
define_from_env('REQUESTS_TEST_HOST', 'requests-php-tests.herokuapp.com');
|
||||
define_from_env('REQUESTS_TEST_HOST_HTTP', REQUESTS_TEST_HOST);
|
||||
define_from_env('REQUESTS_TEST_HOST_HTTPS', REQUESTS_TEST_HOST);
|
||||
|
||||
define_from_env('REQUESTS_HTTP_PROXY');
|
||||
define_from_env('REQUESTS_HTTP_PROXY_AUTH');
|
||||
define_from_env('REQUESTS_HTTP_PROXY_AUTH_USER');
|
||||
define_from_env('REQUESTS_HTTP_PROXY_AUTH_PASS');
|
||||
|
||||
include(dirname(dirname(__FILE__)) . '/library/Requests.php');
|
||||
Requests::register_autoloader();
|
||||
|
||||
function autoload_tests($class) {
|
||||
if (strpos($class, 'RequestsTest_') !== 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$class = substr($class, 13);
|
||||
$file = str_replace('_', '/', $class);
|
||||
if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
|
||||
require_once(dirname(__FILE__) . '/' . $file . '.php');
|
||||
}
|
||||
}
|
||||
|
||||
spl_autoload_register('autoload_tests');
|
||||
|
||||
function httpbin($suffix = '', $ssl = false) {
|
||||
$host = $ssl ? 'https://' . REQUESTS_TEST_HOST_HTTPS : 'http://' . REQUESTS_TEST_HOST_HTTP;
|
||||
return rtrim( $host, '/' ) . '/' . ltrim( $suffix, '/' );
|
||||
}
|
||||
|
||||
class MockTransport implements Requests_Transport {
|
||||
public $code = 200;
|
||||
public $chunked = false;
|
||||
public $body = 'Test Body';
|
||||
public $raw_headers = '';
|
||||
|
||||
private static $messages = array(
|
||||
100 => '100 Continue',
|
||||
101 => '101 Switching Protocols',
|
||||
200 => '200 OK',
|
||||
201 => '201 Created',
|
||||
202 => '202 Accepted',
|
||||
203 => '203 Non-Authoritative Information',
|
||||
204 => '204 No Content',
|
||||
205 => '205 Reset Content',
|
||||
206 => '206 Partial Content',
|
||||
300 => '300 Multiple Choices',
|
||||
301 => '301 Moved Permanently',
|
||||
302 => '302 Found',
|
||||
303 => '303 See Other',
|
||||
304 => '304 Not Modified',
|
||||
305 => '305 Use Proxy',
|
||||
306 => '306 (Unused)',
|
||||
307 => '307 Temporary Redirect',
|
||||
400 => '400 Bad Request',
|
||||
401 => '401 Unauthorized',
|
||||
402 => '402 Payment Required',
|
||||
403 => '403 Forbidden',
|
||||
404 => '404 Not Found',
|
||||
405 => '405 Method Not Allowed',
|
||||
406 => '406 Not Acceptable',
|
||||
407 => '407 Proxy Authentication Required',
|
||||
408 => '408 Request Timeout',
|
||||
409 => '409 Conflict',
|
||||
410 => '410 Gone',
|
||||
411 => '411 Length Required',
|
||||
412 => '412 Precondition Failed',
|
||||
413 => '413 Request Entity Too Large',
|
||||
414 => '414 Request-URI Too Long',
|
||||
415 => '415 Unsupported Media Type',
|
||||
416 => '416 Requested Range Not Satisfiable',
|
||||
417 => '417 Expectation Failed',
|
||||
418 => '418 I\'m a teapot',
|
||||
428 => '428 Precondition Required',
|
||||
429 => '429 Too Many Requests',
|
||||
431 => '431 Request Header Fields Too Large',
|
||||
500 => '500 Internal Server Error',
|
||||
501 => '501 Not Implemented',
|
||||
502 => '502 Bad Gateway',
|
||||
503 => '503 Service Unavailable',
|
||||
504 => '504 Gateway Timeout',
|
||||
505 => '505 HTTP Version Not Supported',
|
||||
511 => '511 Network Authentication Required',
|
||||
);
|
||||
|
||||
public function request($url, $headers = array(), $data = array(), $options = array()) {
|
||||
$status = isset(self::$messages[$this->code]) ? self::$messages[$this->code] : $this->code . ' unknown';
|
||||
$response = "HTTP/1.0 $status\r\n";
|
||||
$response .= "Content-Type: text/plain\r\n";
|
||||
if ($this->chunked) {
|
||||
$response .= "Transfer-Encoding: chunked\r\n";
|
||||
}
|
||||
$response .= $this->raw_headers;
|
||||
$response .= "Connection: close\r\n\r\n";
|
||||
$response .= $this->body;
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function request_multiple($requests, $options) {
|
||||
$responses = array();
|
||||
foreach ($requests as $id => $request) {
|
||||
$handler = new MockTransport();
|
||||
$handler->code = $request['options']['mock.code'];
|
||||
$handler->chunked = $request['options']['mock.chunked'];
|
||||
$handler->body = $request['options']['mock.body'];
|
||||
$handler->raw_headers = $request['options']['mock.raw_headers'];
|
||||
$responses[$id] = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']);
|
||||
|
||||
if (!empty($options['mock.parse'])) {
|
||||
$request['options']['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$id], $request));
|
||||
$request['options']['hooks']->dispatch('multiple.request.complete', array(&$responses[$id], $id));
|
||||
}
|
||||
}
|
||||
|
||||
return $responses;
|
||||
}
|
||||
|
||||
public static function test() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class RawTransport implements Requests_Transport {
|
||||
public $data = '';
|
||||
public function request($url, $headers = array(), $data = array(), $options = array()) {
|
||||
return $this->data;
|
||||
}
|
||||
public function request_multiple($requests, $options) {
|
||||
foreach ($requests as $id => &$request) {
|
||||
$handler = new RawTransport();
|
||||
$handler->data = $request['options']['raw.data'];
|
||||
$request = $handler->request($request['url'], $request['headers'], $request['data'], $request['options']);
|
||||
}
|
||||
|
||||
return $requests;
|
||||
}
|
||||
public static function test() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="bootstrap.php">
|
||||
<testsuites>
|
||||
<testsuite name="Authentication">
|
||||
<directory suffix=".php">Auth</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Transports">
|
||||
<directory suffix=".php">Transport</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Proxies">
|
||||
<directory suffix=".php">Proxy</directory>
|
||||
</testsuite>
|
||||
<testsuite name="General">
|
||||
<file>ChunkedEncoding.php</file>
|
||||
<file>Cookies.php</file>
|
||||
<file>IDNAEncoder.php</file>
|
||||
<file>IRI.php</file>
|
||||
<file>Requests.php</file>
|
||||
<file>Response/Headers.php</file>
|
||||
<file>Session.php</file>
|
||||
<file>SSL.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<logging>
|
||||
<log type="coverage-html" target="coverage" title="PHPUnit"
|
||||
charset="UTF-8" yui="true" highlight="true"
|
||||
lowUpperBound="35" highLowerBound="90"/>
|
||||
</logging>
|
||||
|
||||
<filter>
|
||||
<blacklist>
|
||||
<directory suffix=".php">.</directory>
|
||||
</blacklist>
|
||||
<whitelist>
|
||||
<directory suffix=".php">../library</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,5 @@
|
||||
def request(context, flow):
|
||||
flow.request.headers["x-requests-proxy"] = "http"
|
||||
|
||||
def response(context, flow):
|
||||
flow.response.headers[b"x-requests-proxied"] = "http"
|
||||
@@ -0,0 +1,11 @@
|
||||
PROXYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
PORT=${PORT:-9000}
|
||||
|
||||
PROXYBIN=${PROXYBIN:-"$(which mitmdump)"}
|
||||
ARGS="-s '$PROXYDIR/proxy.py' -p $PORT"
|
||||
if [[ ! -z "$AUTH" ]]; then
|
||||
ARGS="$ARGS --singleuser=$AUTH"
|
||||
fi
|
||||
PIDFILE="$PROXYDIR/proxy.pid"
|
||||
|
||||
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --exec $PROXYBIN -- $ARGS
|
||||
@@ -0,0 +1,5 @@
|
||||
PROXYDIR="$PWD/$(dirname $0)"
|
||||
|
||||
PIDFILE="$PROXYDIR/proxy.pid"
|
||||
|
||||
start-stop-daemon --stop --pidfile $PIDFILE --make-pidfile && rm $PROXYDIR/proxy.pid
|
||||
Reference in New Issue
Block a user