|
| 1 | +<?php |
| 2 | +namespace Curl; |
| 3 | +include 'helper.inc.php'; |
| 4 | + |
| 5 | +class CurlTest extends \PHPUnit_Framework_TestCase { |
| 6 | + |
| 7 | + public function testExtensionLoaded() { |
| 8 | + |
| 9 | + $this->assertTrue(extension_loaded('curl')); |
| 10 | + } |
| 11 | + |
| 12 | + public function testUserAgent() { |
| 13 | + |
| 14 | + $test = new \Test(); |
| 15 | + $test->curl->setUserAgent(Curl::USER_AGENT); |
| 16 | + $this->assertEquals(Curl::USER_AGENT, $test->server('GET', array( |
| 17 | + 'test' => 'server', |
| 18 | + 'key' => 'HTTP_USER_AGENT', |
| 19 | + ))); |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + public function testGet() { |
| 24 | + $test = new \Test(); |
| 25 | + $this->assertTrue($test->server('GET', array( |
| 26 | + 'test' => 'server', |
| 27 | + 'key' => 'REQUEST_METHOD', |
| 28 | + )) === 'GET'); |
| 29 | + } |
| 30 | + |
| 31 | + public function testPostRequestMethod() { |
| 32 | + $test = new \Test(); |
| 33 | + $this->assertTrue($test->server('POST', array( |
| 34 | + 'test' => 'server', |
| 35 | + 'key' => 'REQUEST_METHOD', |
| 36 | + )) === 'POST'); |
| 37 | + } |
| 38 | + |
| 39 | + public function testPostData() { |
| 40 | + $test = new \Test(); |
| 41 | + $this->assertTrue($test->server('POST', array( |
| 42 | + 'test' => 'post', |
| 43 | + 'key' => 'test', |
| 44 | + )) === 'post'); |
| 45 | + } |
| 46 | + |
| 47 | + public function testPostMultidimensionalData() { |
| 48 | + $test = new \Test(); |
| 49 | + $this->assertTrue($test->server('POST', array( |
| 50 | + 'test' => 'post_multidimensional', |
| 51 | + 'key' => 'file', |
| 52 | + 'file' => array( |
| 53 | + 'wibble', |
| 54 | + 'wubble', |
| 55 | + 'wobble', |
| 56 | + ), |
| 57 | + )) === 'test=post_multidimensional&key=file&file%5B%5D=wibble&file%5B%5D=wubble&file%5B%5D=wobble'); |
| 58 | + } |
| 59 | + |
| 60 | + public function testPostFilePathUpload() { |
| 61 | + $file_path = get_png(); |
| 62 | + |
| 63 | + $test = new \Test(); |
| 64 | + $this->assertTrue($test->server('POST', array( |
| 65 | + 'test' => 'post_file_path_upload', |
| 66 | + 'key' => 'image', |
| 67 | + 'image' => '@' . $file_path, |
| 68 | + )) === 'image/png'); |
| 69 | + |
| 70 | + unlink($file_path); |
| 71 | + } |
| 72 | + |
| 73 | + public function testPutRequestMethod() { |
| 74 | + $test = new \Test(); |
| 75 | + $this->assertTrue($test->server('PUT', array( |
| 76 | + 'test' => 'server', |
| 77 | + 'key' => 'REQUEST_METHOD', |
| 78 | + )) === 'PUT'); |
| 79 | + } |
| 80 | + |
| 81 | + public function testPutData() { |
| 82 | + $test = new \Test(); |
| 83 | + $this->assertTrue($test->server('PUT', array( |
| 84 | + 'test' => 'put', |
| 85 | + 'key' => 'test', |
| 86 | + )) === 'put'); |
| 87 | + } |
| 88 | + |
| 89 | + public function testPutFileHandle() { |
| 90 | + $png = create_png(); |
| 91 | + $tmp_file = create_tmp_file($png); |
| 92 | + |
| 93 | + $test = new \Test(); |
| 94 | + $test->curl->setopt(CURLOPT_PUT, TRUE); |
| 95 | + $test->curl->setopt(CURLOPT_INFILE, $tmp_file); |
| 96 | + $test->curl->setopt(CURLOPT_INFILESIZE, strlen($png)); |
| 97 | + $test->curl->put(\Test::TEST_URL, array( |
| 98 | + 'test' => 'put_file_handle', |
| 99 | + )); |
| 100 | + |
| 101 | + fclose($tmp_file); |
| 102 | + |
| 103 | + $this->assertTrue($test->curl->response === 'image/png'); |
| 104 | + } |
| 105 | + |
| 106 | + public function testDelete() { |
| 107 | + $test = new \Test(); |
| 108 | + $this->assertTrue($test->server('DELETE', array( |
| 109 | + 'test' => 'server', |
| 110 | + 'key' => 'REQUEST_METHOD', |
| 111 | + )) === 'DELETE'); |
| 112 | + |
| 113 | + $test = new \Test(); |
| 114 | + $this->assertTrue($test->server('DELETE', array( |
| 115 | + 'test' => 'delete', |
| 116 | + 'key' => 'test', |
| 117 | + )) === 'delete'); |
| 118 | + } |
| 119 | + |
| 120 | + public function testBasicHttpAuth() { |
| 121 | + $test = new \Test(); |
| 122 | + $this->assertTrue($test->server('GET', array( |
| 123 | + 'test' => 'http_basic_auth', |
| 124 | + )) === 'canceled'); |
| 125 | + |
| 126 | + $username = 'myusername'; |
| 127 | + $password = 'mypassword'; |
| 128 | + $test = new \Test(); |
| 129 | + $test->curl->setBasicAuthentication($username, $password); |
| 130 | + $test->server('GET', array( |
| 131 | + 'test' => 'http_basic_auth', |
| 132 | + )); |
| 133 | + $json = json_decode($test->curl->response); |
| 134 | + $this->assertTrue($json->username === $username); |
| 135 | + $this->assertTrue($json->password === $password); |
| 136 | + } |
| 137 | + |
| 138 | + public function testReferrer() { |
| 139 | + $test = new \Test(); |
| 140 | + $test->curl->setReferrer('myreferrer'); |
| 141 | + $this->assertTrue($test->server('GET', array( |
| 142 | + 'test' => 'server', |
| 143 | + 'key' => 'HTTP_REFERER', |
| 144 | + )) === 'myreferrer'); |
| 145 | + } |
| 146 | + |
| 147 | + public function testCookies() { |
| 148 | + $test = new \Test(); |
| 149 | + $test->curl->setCookie('mycookie', 'yum'); |
| 150 | + $this->assertTrue($test->server('GET', array( |
| 151 | + 'test' => 'cookie', |
| 152 | + 'key' => 'mycookie', |
| 153 | + )) === 'yum'); |
| 154 | + } |
| 155 | + |
| 156 | + public function testError() { |
| 157 | + $test = new \Test(); |
| 158 | + $test->curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000); |
| 159 | + $test->curl->get('http://1.2.3.4/'); |
| 160 | + $this->assertTrue($test->curl->error === TRUE); |
| 161 | + $this->assertTrue($test->curl->curl_error === TRUE); |
| 162 | + $this->assertTrue($test->curl->curl_error_code === CURLE_OPERATION_TIMEOUTED); |
| 163 | + } |
| 164 | + |
| 165 | + public function testHeaders() { |
| 166 | + $test = new \Test(); |
| 167 | + $test->curl->setHeader('Content-Type', 'application/json'); |
| 168 | + $test->curl->setHeader('X-Requested-With', 'XMLHttpRequest'); |
| 169 | + $test->curl->setHeader('Accept', 'application/json'); |
| 170 | + $this->assertTrue($test->server('GET', array( |
| 171 | + 'test' => 'server', |
| 172 | + 'key' => 'CONTENT_TYPE', |
| 173 | + )) === 'application/json'); |
| 174 | + $this->assertTrue($test->server('GET', array( |
| 175 | + 'test' => 'server', |
| 176 | + 'key' => 'HTTP_X_REQUESTED_WITH', |
| 177 | + )) === 'XMLHttpRequest'); |
| 178 | + $this->assertTrue($test->server('GET', array( |
| 179 | + 'test' => 'server', |
| 180 | + 'key' => 'HTTP_ACCEPT', |
| 181 | + )) === 'application/json'); |
| 182 | + } |
| 183 | +} |
0 commit comments