Skip to content

Commit

Permalink
Merge pull request #170 from rmccue/fix-tests-damn-it
Browse files Browse the repository at this point in the history
Improve our tests (round 2)
  • Loading branch information
rmccue committed Oct 2, 2015
2 parents d3e6e43 + cea419c commit 94a3423
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 25 deletions.
24 changes: 16 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
language: php
matrix:
fast_finish: true
allow_failures:
- php: 7.0
include:
- php: 5.2
- php: 5.3
- php: 5.4
- php: 5.5
- php: 5.6
env: TEST_COVERAGE=1
- php: 7.0
- php: hhvm

before_script:
# Setup Coveralls and httpbin-php
# Setup the test server
- phpenv local 5.5
- composer install --dev --no-interaction

Expand All @@ -17,11 +31,5 @@ after_script:
- cd ..
- phpenv local 5.5
- sudo PATH=$PATH vendor/bin/stop.sh
- php vendor/bin/coveralls -v
- test $TEST_COVERAGE && bash <(curl -s https://codecov.io/bash)
- phpenv local --unset
php:
- 5.2
- 5.3
- 5.4
- 5.5
- hhvm
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ issue](https://github.com/rmccue/Requests/issues/new)!
Testing
-------
[![Build Status](https://secure.travis-ci.org/rmccue/Requests.png?branch=master)](http://travis-ci.org/rmccue/Requests)
[![Coverage Status](https://coveralls.io/repos/rmccue/Requests/badge.png?branch=master)][coveralls]
[![codecov.io](http://codecov.io/github/rmccue/Requests/coverage.svg?branch=master)](http://codecov.io/github/rmccue/Requests?branch=master)

Requests strives to have 100% code-coverage of the library with an extensive
set of tests. We're not quite there yet, but [we're getting close][coveralls].
set of tests. We're not quite there yet, but [we're getting close][codecov].

[coveralls]: https://coveralls.io/r/rmccue/Requests?branch=master
[codecov]: http://codecov.io/github/rmccue/Requests

To run the test suite, first check that you have the [PHP
JSON extension ](http://php.net/manual/en/book.json.php) enabled. Then
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"php": ">=5.2"
},
"require-dev": {
"satooshi/php-coveralls": "dev-master",
"requests/test-server": "dev-master"
},
"type": "library",
Expand Down
50 changes: 38 additions & 12 deletions tests/Transport/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,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 +333,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 +350,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 +393,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 Down Expand Up @@ -672,12 +699,11 @@ public function testMultipleToFile() {
unlink($requests['post']['options']['filename']);
}

public function testHostHeader() {
$request = Requests::get('http://portquiz.positon.org:8080/', array(), $this->getOptions());
$responseDoc = new DOMDocument;
$responseDoc->loadHTML($request->body);
$portXpath = new DOMXPath($responseDoc);
$portXpathMatches = $portXpath->query('//p/b');
$this->assertEquals(8080, $portXpathMatches->item(0)->nodeValue);
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]);
}
}
7 changes: 6 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
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 94a3423

Please sign in to comment.