Skip to content

Commit

Permalink
Merge 75bcdb9 into ea37f1e
Browse files Browse the repository at this point in the history
  • Loading branch information
ozh committed Apr 20, 2015
2 parents ea37f1e + 75bcdb9 commit 4b41061
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
2 changes: 1 addition & 1 deletion tests/Auth/Basic.php
Expand Up @@ -74,7 +74,7 @@ public function testPOSTUsingInstantiation($transport) {
$auth = explode(' ', $auth);

$this->assertEquals(base64_encode('user:passwd'), $auth[1]);
$this->assertEquals('test', $result->data);
$this->assertEquals('', $result->form->test);
}

/**
Expand Down
66 changes: 39 additions & 27 deletions tests/Transport/Base.php
Expand Up @@ -30,7 +30,6 @@ public function testSimpleGET() {
$this->assertEquals(200, $request->status_code);

$result = json_decode($request->body, true);
$this->assertEquals(httpbin('/get'), $result['url']);
$this->assertEmpty($result['args']);
}

Expand All @@ -39,7 +38,6 @@ public function testGETWithArgs() {
$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']);
}

Expand All @@ -52,7 +50,6 @@ public function testGETWithData() {
$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']);
}

Expand All @@ -68,7 +65,6 @@ public function testGETWithNestedData() {
$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']);
}

Expand All @@ -80,7 +76,6 @@ public function testGETWithDataAndQuery() {
$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']);
}

Expand All @@ -100,7 +95,6 @@ public function testChunked() {
$this->assertEquals(200, $request->status_code);

$result = json_decode($request->body, true);
$this->assertEquals(httpbin('/stream/1'), $result['url']);
$this->assertEmpty($result['args']);
}

Expand All @@ -116,7 +110,7 @@ public function testRawPOST() {
$this->assertEquals(200, $request->status_code);

$result = json_decode($request->body, true);
$this->assertEquals('test', $result['data']);
$this->assertEquals('', $result['form']['test']);
}

public function testFormPost() {
Expand Down Expand Up @@ -161,7 +155,7 @@ public function testRawPUT() {
$this->assertEquals(200, $request->status_code);

$result = json_decode($request->body, true);
$this->assertEquals('test', $result['data']);
$this->assertEquals('', $result['form']['test']);
}

public function testFormPUT() {
Expand Down Expand Up @@ -191,7 +185,7 @@ public function testRawPATCH() {
$this->assertEquals(200, $request->status_code);

$result = json_decode($request->body, true);
$this->assertEquals('test', $result['data']);
$this->assertEquals('', $result['form']['test']);
}

public function testFormPATCH() {
Expand Down Expand Up @@ -220,7 +214,6 @@ public function testDELETE() {
$this->assertEquals(200, $request->status_code);

$result = json_decode($request->body, true);
$this->assertEquals(httpbin('/delete'), $result['url']);
$this->assertEmpty($result['args']);
}

Expand All @@ -233,7 +226,6 @@ public function testDELETEWithData() {
$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']);
}

Expand Down Expand Up @@ -315,11 +307,16 @@ public static function statusCodeSuccessProvider() {
* @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(), $this->getOptions($options));
$request = Requests::get($url, array(), $options);
$this->assertEquals($code, $request->status_code);
$this->assertEquals($success, $request->success);
}
Expand All @@ -328,9 +325,13 @@ public function testStatusCode($code, $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) {
Expand All @@ -341,30 +342,41 @@ public function testStatusCodeThrow($code, $success) {
$this->setExpectedException('Requests_Exception');
}
}
$request = Requests::get($url, array(), $this->getOptions($options));
$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) {
$this->setExpectedException('Requests_Exception_HTTP_' . $code, $code);
}
}
$request = Requests::get($url, array(), $this->getOptions($options));
$request = Requests::get($url, array(), $options);
$request->throw_for_status(true);
}

public function testStatusCodeUnknown(){
$request = Requests::get(httpbin('/status/599'), array(), $this->getOptions());
$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);
}
Expand All @@ -373,7 +385,14 @@ public function testStatusCodeUnknown(){
* @expectedException Requests_Exception_HTTP_Unknown
*/
public function testStatusCodeThrowUnknown(){
$request = Requests::get(httpbin('/status/599'), array(), $this->getOptions());
$transport = new MockTransport();
$transport->code = 599;

$options = array(
'transport' => $transport,
);

$request = Requests::get(httpbin('/status/599'), array(), $options);
$request->throw_for_status(true);
}

Expand All @@ -395,7 +414,6 @@ public function testStreamToFile() {

$contents = file_get_contents($options['filename']);
$result = json_decode($contents, true);
$this->assertEquals(httpbin('/get'), $result['url']);
$this->assertEmpty($result['args']);

unlink($options['filename']);
Expand Down Expand Up @@ -427,8 +445,6 @@ public function testHTTPS() {
$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']);
}

Expand Down Expand Up @@ -536,7 +552,6 @@ public function testMultiple() {
$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
Expand All @@ -545,7 +560,6 @@ public function testMultiple() {
$this->assertEquals(200, $responses['test2']->status_code);

$result = json_decode($responses['test2']->body, true);
$this->assertEquals(httpbin('/get'), $result['url']);
$this->assertEmpty($result['args']);
}

Expand All @@ -568,7 +582,7 @@ public function testMultipleWithDifferingMethods() {
// post
$this->assertEquals(200, $responses['post']->status_code);
$result = json_decode($responses['post']->body, true);
$this->assertEquals('test', $result['data']);
$this->assertEquals('', $result['form']['test']);
}

/**
Expand Down Expand Up @@ -660,20 +674,18 @@ public function testMultipleToFile() {
// 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']);
$this->assertEquals('', $result['form']['test']);
unlink($requests['post']['options']['filename']);
}

public function testHostHeader() {
$request = Requests::get('http://portquiz.positon.org:8080/', array(), $this->getOptions());
public function testAlternatePort() {
$request = Requests::get('http://portquiz.net:8080/', array(), $this->getOptions());
$responseDoc = new DOMDocument;
$responseDoc->loadHTML($request->body);
$portXpath = new DOMXPath($responseDoc);
Expand Down
7 changes: 6 additions & 1 deletion tests/bootstrap.php
Expand Up @@ -74,16 +74,21 @@ class MockTransport implements Requests_Transport {
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 = self::$messages[$this->code];
$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) {
Expand Down

0 comments on commit 4b41061

Please sign in to comment.