mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-31 12:43:14 +00:00
Add Http Basic Authentication test
This commit is contained in:
+15
-1
@@ -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
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user