Add Http Basic Authentication test

This commit is contained in:
php-curl-class
2013-08-19 11:49:20 -07:00
parent 7b57814973
commit dc162566f7
2 changed files with 35 additions and 4 deletions
+15 -1
View File
@@ -12,7 +12,7 @@ class Test {
$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE);
}
function server($request_method, $test, $key) {
function server($request_method, $test, $key='') {
$request_method = strtolower($request_method);
$url = BASE_URL . 'tests/server.php';
$this->curl->$request_method($url, array(
@@ -39,4 +39,18 @@ class CurlTest extends PHPUnit_Framework_TestCase {
$test = new Test();
$this->assertTrue($test->server('POST', 'server', 'REQUEST_METHOD') === 'POST');
}
public function testBasicHttpAuth() {
$test = new Test();
$this->assertTrue($test->server('GET', 'http_basic_auth') === 'canceled');
$username = 'myusername';
$password = 'mypassword';
$test = new Test();
$test->curl->setBasicAuthentication($username, $password);
$test->server('GET', 'http_basic_auth');
$json = json_decode($test->curl->response);
$this->assertTrue($json->username === $username);
$this->assertTrue($json->password === $password);
}
}
+20 -3
View File
@@ -1,12 +1,29 @@
<?php
header('Content-Type: text/plain');
$request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
$var = '_' . strtoupper($request_method);
$var = $$var;
$test = isset($var['test']) ? '_' . strtoupper($var['test']) : '';
$test = isset($var['test']) ? $var['test'] : '';
if ($test == 'http_basic_auth') {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'canceled';
exit;
}
header('Content-Type: application/json');
echo json_encode(array(
'username' => $_SERVER['PHP_AUTH_USER'],
'password' => $_SERVER['PHP_AUTH_PW'],
));
exit;
}
header('Content-Type: text/plain');
$test = '_' . strtoupper($var['test']);
$test = $$test;
//var_dump('test:', $test);