Skip to content

Commit 7bdc952

Browse files
committed
Add post_file_path_upload.php
1 parent fe0de9e commit 7bdc952

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

src/Curl/Curl.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
class Curl {
66

7+
// The HTTP authentication method(s) to use.
8+
9+
const AUTH_BASIC = CURLAUTH_BASIC;
10+
const AUTH_DIGEST = CURLAUTH_DIGEST;
11+
const AUTH_GSSNEGOTIATE = CURLAUTH_GSSNEGOTIATE;
12+
const AUTH_NTLM = CURLAUTH_NTLM;
13+
const AUTH_ANY = CURLAUTH_ANY;
14+
const AUTH_ANYSAFE = CURLAUTH_ANYSAFE;
15+
716
const USER_AGENT = 'PHP Curl/1.1 (+https://github.com/mod-php/curl)';
817

918
private $_cookies = array();
@@ -28,7 +37,7 @@ class Curl {
2837
public $response = NULL;
2938

3039
public function __construct() {
31-
40+
3241
if (!extension_loaded('curl')) {
3342
throw new ErrorException('cURL library is not loaded');
3443
}
@@ -73,9 +82,13 @@ public function delete($url, $data=array()) {
7382
}
7483

7584
public function setBasicAuthentication($username, $password) {
76-
$this->setopt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
85+
$this->setHttpAuth(self::AUTH_BASIC);
7786
$this->setopt(CURLOPT_USERPWD, $username . ':' . $password);
7887
}
88+
89+
protected function setHttpAuth($httpauth) {
90+
$this->setOpt(CURLOPT_HTTPAUTH, $httpauth);
91+
}
7992

8093
public function setHeader($key, $value) {
8194
$this->_headers[$key] = $key . ': ' . $value;

tests/CurlTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,20 @@ public function testPostMultidimensionalData() {
8484
public function testPostFilePathUpload() {
8585
$file_path = $this->get_png();
8686

87-
$this->assertEquals('image/png', $this->server('POST', array(
88-
'test' => 'post_file_path_upload',
87+
$data = array(
8988
'key' => 'image',
9089
'image' => '@' . $file_path,
91-
)));
90+
);
91+
92+
$this->curl->post($this->test_url . 'post_file_path_upload.php', $data);
93+
94+
$this->assertEquals(
95+
array(
96+
'request_method' => 'POST',
97+
'key' => 'image',
98+
'mime_content_type' => 'image/png'
99+
),
100+
json_decode($this->curl->response));
92101

93102
unlink($file_path);
94103
}
@@ -144,10 +153,9 @@ public function testBasicHttpAuth() {
144153

145154
$username = 'myusername';
146155
$password = 'mypassword';
156+
147157
$this->curl->setBasicAuthentication($username, $password);
148-
$this->server('GET', array(
149-
'test' => 'http_basic_auth',
150-
));
158+
151159
$json = json_decode($this->curl->response);
152160
$this->assertTrue($json->username === $username);
153161
$this->assertTrue($json->password === $password);

tests/server.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
$test = isset($data_values['test']) ? $data_values['test'] : '';
55
$key = isset($data_values['key']) ? $data_values['key'] : '';
66

7-
if ($test === 'post_file_path_upload') {
8-
echo mime_content_type($_FILES[$key]['tmp_name']);
9-
exit;
10-
}
11-
else if ($test === 'put_file_handle') {
7+
if ($test === 'put_file_handle') {
128
$tmp_filename = tempnam('/tmp', 'php-curl-class.');
139
file_put_contents($tmp_filename, file_get_contents('php://input'));
1410
echo mime_content_type($tmp_filename);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
$request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
4+
$data_values = $request_method === 'POST' ? $_POST : $_GET;
5+
6+
$key = isset($data_values['key']) ? $data_values['key'] : '';
7+
8+
$response = array();
9+
10+
$response['request_method'] = $request_method;
11+
$response['key'] = $key;
12+
13+
if(isset($_FILES[$key])) {
14+
$response['mime_content_type'] = mime_content_type($_FILES[$key]['tmp_name']);
15+
} else {
16+
$response['mime_content_type'] = 'ERROR';
17+
}
18+
19+
echo json_encode($response);

0 commit comments

Comments
 (0)