Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy testing #75

Merged
merged 23 commits into from
Oct 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,40 @@ matrix:
# Use new container infrastructure
sudo: false

before_script:
cache:
directories:
- $HOME/.cache/pip
- $HOME/.composer/cache
- vendor

install:
# Setup the test server
- phpenv local 5.5
- composer install --dev --no-interaction

- TESTPHPBIN=$(phpenv which php)
- phpenv local --unset

# Setup the proxy
- pip install --user mitmproxy

before_script:
- PHPBIN=$TESTPHPBIN PORT=8080 vendor/bin/start.sh
- export REQUESTS_TEST_HOST_HTTP="localhost:8080"
- phpenv local --unset

# Work out of the tests directory
- cd tests
- PROXYBIN="$HOME/.local/bin/mitmdump" PORT=9002 utils/proxy/start.sh
- PROXYBIN="$HOME/.local/bin/mitmdump" PORT=9003 AUTH="test:pass" utils/proxy/start.sh
- export REQUESTS_HTTP_PROXY="localhost:9002"
- export REQUESTS_HTTP_PROXY_AUTH="localhost:9003"
- export REQUESTS_HTTP_PROXY_AUTH_USER="test"
- export REQUESTS_HTTP_PROXY_AUTH_PASS="pass"

script:
- phpunit --coverage-clover clover.xml

after_script:
- utils/proxy/stop.sh
- cd ..
- phpenv local 5.5
- PATH=$PATH vendor/bin/stop.sh
Expand Down
131 changes: 131 additions & 0 deletions tests/Proxy/HTTP.php
Original file line number Diff line number Diff line change
@@ -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' => 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);
}
}
21 changes: 18 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@

date_default_timezone_set('UTC');

function define_from_env($name, $default = false) {
$env = getenv($name);
if ($env) {
define($name, $env);
}
else {
define($name, $default);
}
}

$host = getenv('REQUESTS_TEST_HOST');
if (empty($host)) {
$host = 'httpbin.org';
}
define('REQUESTS_TEST_HOST', getenv('REQUESTS_TEST_HOST') ? getenv('REQUESTS_TEST_HOST') : 'httpbin.org');
define('REQUESTS_TEST_HOST_HTTP', getenv('REQUESTS_TEST_HOST_HTTP') ? getenv('REQUESTS_TEST_HOST_HTTP') : REQUESTS_TEST_HOST);
define('REQUESTS_TEST_HOST_HTTPS', getenv('REQUESTS_TEST_HOST_HTTPS') ? getenv('REQUESTS_TEST_HOST_HTTPS'): REQUESTS_TEST_HOST);
define_from_env('REQUESTS_TEST_HOST', 'httpbin.org');
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();
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<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>
Expand Down
5 changes: 5 additions & 0 deletions tests/utils/proxy/proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def request(context, flow):
flow.request.headers["x-requests-proxy"] = ["http"]

def response(context, flow):
flow.response.headers["x-requests-proxied"] = ["http"]
11 changes: 11 additions & 0 deletions tests/utils/proxy/start.sh
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/utils/proxy/stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
PROXYDIR="$PWD/$(dirname $0)"

PIDFILE="$PROXYDIR/proxy.pid"

start-stop-daemon --stop --pidfile $PIDFILE --make-pidfile && rm $PROXYDIR/proxy.pid