Mocking
1 year ago
BodyTest.php
1 year ago
RequestTest.php
1 year ago
ResponseTest.php
1 year ago
bootstrap.php
1 year ago
RequestTest.php
581 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Unirest\Test; |
| 4 | |
| 5 | use CoreInterfaces\Core\Request\RequestMethod; |
| 6 | use Exception; |
| 7 | use PHPUnit\Framework\TestCase; |
| 8 | use Unirest\Configuration; |
| 9 | use Unirest\HttpClient; |
| 10 | use Unirest\Request\Body; |
| 11 | use Unirest\Request\Request; |
| 12 | use Unirest\Test\Mocking\HttpClientChild; |
| 13 | |
| 14 | class RequestTest extends TestCase |
| 15 | { |
| 16 | // Generic |
| 17 | public function testCurlOpts() |
| 18 | { |
| 19 | $httpClient = new HttpClient(Configuration::init()->curlOpt(CURLOPT_COOKIE, 'foo=bar')); |
| 20 | |
| 21 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 22 | |
| 23 | $this->assertTrue(property_exists($response->getBody()->cookies, 'foo')); |
| 24 | } |
| 25 | |
| 26 | public function testTimeoutFail() |
| 27 | { |
| 28 | $this->expectException(Exception::class); |
| 29 | $this->expectExceptionMessage('Operation timed out'); |
| 30 | $httpClient = new HttpClient(Configuration::init()->timeout(1)); |
| 31 | $httpClient->execute(new Request('http://mockbin.com/delay/2000')); |
| 32 | } |
| 33 | |
| 34 | public function testDefaultHeaders() |
| 35 | { |
| 36 | $httpClient = new HttpClient(Configuration::init() |
| 37 | ->defaultHeaders([ |
| 38 | 'header1' => 'Hello', |
| 39 | 'header2' => 'world' |
| 40 | ])); |
| 41 | |
| 42 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 43 | |
| 44 | $this->assertEquals(200, $response->getStatusCode()); |
| 45 | $this->assertEquals('Hello', $response->getBody()->headers->header1); |
| 46 | $this->assertEquals('world', $response->getBody()->headers->header2); |
| 47 | |
| 48 | $response = $httpClient->execute(new Request( |
| 49 | 'http://mockbin.com/request', |
| 50 | RequestMethod::GET, |
| 51 | ['header1' => 'Custom value'] |
| 52 | )); |
| 53 | |
| 54 | $this->assertEquals(200, $response->getStatusCode()); |
| 55 | $this->assertEquals('Custom value', $response->getBody()->headers->header1); |
| 56 | |
| 57 | $httpClient = new HttpClient(); |
| 58 | |
| 59 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 60 | |
| 61 | $this->assertEquals(200, $response->getStatusCode()); |
| 62 | $this->assertFalse(isset($response->getBody()->headers->header1)); |
| 63 | $this->assertFalse(isset($response->getBody()->headers->header2)); |
| 64 | } |
| 65 | |
| 66 | public function testDefaultHeader() |
| 67 | { |
| 68 | $httpClient = new HttpClient(Configuration::init() |
| 69 | ->defaultHeader('Hello', 'custom')); |
| 70 | |
| 71 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 72 | |
| 73 | $this->assertEquals(200, $response->getStatusCode()); |
| 74 | $this->assertTrue(property_exists($response->getBody()->headers, 'hello')); |
| 75 | $this->assertEquals('custom', $response->getBody()->headers->hello); |
| 76 | |
| 77 | $httpClient = new HttpClient(); |
| 78 | |
| 79 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 80 | |
| 81 | $this->assertEquals(200, $response->getStatusCode()); |
| 82 | $this->assertFalse(property_exists($response->getBody()->headers, 'hello')); |
| 83 | } |
| 84 | |
| 85 | public function testConnectionReuse() |
| 86 | { |
| 87 | $httpClientChild = new HttpClientChild(); |
| 88 | $url = "http://httpbin.org/get"; |
| 89 | |
| 90 | // test client sending keep-alive automatically |
| 91 | $res = $httpClientChild->execute(new Request($url)); |
| 92 | $this->assertEquals("keep-alive", $res->getHeaders()['Connection']); |
| 93 | $this->assertEquals(1, $httpClientChild->getTotalNumberOfConnections()); |
| 94 | |
| 95 | // test closing connection after response is received |
| 96 | $res = $httpClientChild->execute(new Request($url, RequestMethod::GET, [ 'Connection' => 'close' ])); |
| 97 | $this->assertEquals("close", $res->getHeaders()['Connection']); |
| 98 | $this->assertEquals(1, $httpClientChild->getTotalNumberOfConnections()); |
| 99 | |
| 100 | // test creating a new connection after closing previous one |
| 101 | $res = $httpClientChild->execute(new Request($url)); |
| 102 | $this->assertEquals("keep-alive", $res->getHeaders()['Connection']); |
| 103 | $this->assertEquals(2, $httpClientChild->getTotalNumberOfConnections()); |
| 104 | |
| 105 | // test persisting the new connection |
| 106 | $res = $httpClientChild->execute(new Request($url)); |
| 107 | $this->assertEquals("keep-alive", $res->getHeaders()['Connection']); |
| 108 | $this->assertEquals(2, $httpClientChild->getTotalNumberOfConnections()); |
| 109 | } |
| 110 | |
| 111 | public function testConnectionReuseForMultipleDomains() |
| 112 | { |
| 113 | $httpClientChild = new HttpClientChild(); |
| 114 | $url1 = "http://httpbin.org/get"; |
| 115 | $url2 = "http://ptsv2.com/t/cedqp-1655183385"; |
| 116 | $url3 = "http://en2hoq5smpha9.x.pipedream.net"; |
| 117 | $url4 = "http://mockbin.com/request"; |
| 118 | |
| 119 | $httpClientChild->execute(new Request($url1)); |
| 120 | $httpClientChild->execute(new Request($url2)); |
| 121 | $httpClientChild->execute(new Request($url3)); |
| 122 | // test creating 3 connections by calling 3 domains |
| 123 | $this->assertEquals(3, $httpClientChild->getTotalNumberOfConnections()); |
| 124 | |
| 125 | $httpClientChild->execute(new Request($url1)); |
| 126 | $httpClientChild->execute(new Request($url2)); |
| 127 | $httpClientChild->execute(new Request($url3)); |
| 128 | // test persisting previous 3 connections |
| 129 | $this->assertEquals(3, $httpClientChild->getTotalNumberOfConnections()); |
| 130 | |
| 131 | $httpClientChild->execute(new Request($url1)); |
| 132 | $httpClientChild->execute(new Request($url2)); |
| 133 | $httpClientChild->execute(new Request($url3)); |
| 134 | $httpClientChild->execute(new Request($url4)); |
| 135 | // test adding a new connection by persisting previous ones using a call to another domain |
| 136 | $this->assertEquals(4, $httpClientChild->getTotalNumberOfConnections()); |
| 137 | } |
| 138 | |
| 139 | public function testSetMashapeKey() |
| 140 | { |
| 141 | $httpClient = new HttpClient(Configuration::init()->defaultHeader('x-mashape-key', 'abcd')); |
| 142 | |
| 143 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 144 | |
| 145 | $this->assertEquals(200, $response->getStatusCode()); |
| 146 | $this->assertTrue(property_exists($response->getBody()->headers, 'x-mashape-key')); |
| 147 | $this->assertEquals('abcd', $response->getBody()->headers->{'x-mashape-key'}); |
| 148 | |
| 149 | // send another request |
| 150 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 151 | |
| 152 | $this->assertEquals(200, $response->getStatusCode()); |
| 153 | $this->assertTrue(property_exists($response->getBody()->headers, 'x-mashape-key')); |
| 154 | $this->assertEquals('abcd', $response->getBody()->headers->{'x-mashape-key'}); |
| 155 | |
| 156 | $httpClient = new HttpClient(); |
| 157 | |
| 158 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 159 | |
| 160 | $this->assertEquals(200, $response->getStatusCode()); |
| 161 | $this->assertFalse(property_exists($response->getBody()->headers, 'x-mashape-key')); |
| 162 | } |
| 163 | |
| 164 | public function testGzip() |
| 165 | { |
| 166 | $httpClient = new HttpClient(); |
| 167 | $response = $httpClient->execute(new Request('http://mockbin.com/gzip', RequestMethod::POST)); |
| 168 | |
| 169 | $this->assertEquals('gzip', $response->getHeaders()['Content-Encoding']); |
| 170 | } |
| 171 | |
| 172 | public function testBasicAuthentication() |
| 173 | { |
| 174 | $httpClient = new HttpClient(Configuration::init() |
| 175 | ->auth('user', 'password')); |
| 176 | |
| 177 | $response = $httpClient->execute(new Request('http://mockbin.com/request')); |
| 178 | |
| 179 | $this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $response->getBody()->headers->authorization); |
| 180 | } |
| 181 | |
| 182 | public function testCustomHeaders() |
| 183 | { |
| 184 | $httpClient = new HttpClient(); |
| 185 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::GET, [ |
| 186 | 'user-agent' => 'unirest-php', |
| 187 | ])); |
| 188 | |
| 189 | $this->assertEquals(200, $response->getStatusCode()); |
| 190 | $this->assertEquals('unirest-php', $response->getBody()->headers->{'user-agent'}); |
| 191 | } |
| 192 | |
| 193 | // GET |
| 194 | public function testGet() |
| 195 | { |
| 196 | $httpClient = new HttpClient(); |
| 197 | $response = $httpClient->execute(new Request('http://mockbin.com/request?name=Mark', RequestMethod::GET, [ |
| 198 | 'Accept' => 'application/json' |
| 199 | ], [ |
| 200 | 'nick' => 'thefosk' |
| 201 | ])); |
| 202 | |
| 203 | $this->assertEquals(200, $response->getStatusCode()); |
| 204 | $this->assertEquals('GET', $response->getBody()->method); |
| 205 | $this->assertEquals('Mark', $response->getBody()->queryString->name); |
| 206 | $this->assertEquals('thefosk', $response->getBody()->queryString->nick); |
| 207 | } |
| 208 | |
| 209 | public function testGetMultidimensionalArray() |
| 210 | { |
| 211 | $httpClient = new HttpClient(); |
| 212 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::GET, [ |
| 213 | 'Accept' => 'application/json' |
| 214 | ], [ |
| 215 | 'key' => 'value', |
| 216 | 'items' => [ |
| 217 | 'item1', |
| 218 | 'item2' |
| 219 | ] |
| 220 | ])); |
| 221 | |
| 222 | $this->assertEquals(200, $response->getStatusCode()); |
| 223 | $this->assertEquals('GET', $response->getBody()->method); |
| 224 | $this->assertEquals('value', $response->getBody()->queryString->key); |
| 225 | $this->assertEquals('item1', $response->getBody()->queryString->items[0]); |
| 226 | $this->assertEquals('item2', $response->getBody()->queryString->items[1]); |
| 227 | } |
| 228 | |
| 229 | public function testGetWithDots() |
| 230 | { |
| 231 | $httpClient = new HttpClient(); |
| 232 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::GET, [ |
| 233 | 'Accept' => 'application/json' |
| 234 | ], [ |
| 235 | 'user.name' => 'Mark', |
| 236 | 'nick' => 'thefosk' |
| 237 | ])); |
| 238 | |
| 239 | $this->assertEquals(200, $response->getStatusCode()); |
| 240 | $this->assertEquals('GET', $response->getBody()->method); |
| 241 | $this->assertEquals('Mark', $response->getBody()->queryString->{'user.name'}); |
| 242 | $this->assertEquals('thefosk', $response->getBody()->queryString->nick); |
| 243 | } |
| 244 | |
| 245 | public function testGetWithDotsAlt() |
| 246 | { |
| 247 | $httpClient = new HttpClient(); |
| 248 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::GET, [ |
| 249 | 'Accept' => 'application/json' |
| 250 | ], [ |
| 251 | 'user.name' => 'Mark Bond', |
| 252 | 'nick' => 'thefosk' |
| 253 | ])); |
| 254 | |
| 255 | $this->assertEquals(200, $response->getStatusCode()); |
| 256 | $this->assertEquals('GET', $response->getBody()->method); |
| 257 | $this->assertEquals('Mark Bond', $response->getBody()->queryString->{'user.name'}); |
| 258 | $this->assertEquals('thefosk', $response->getBody()->queryString->nick); |
| 259 | } |
| 260 | public function testGetWithEqualSign() |
| 261 | { |
| 262 | $httpClient = new HttpClient(); |
| 263 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::GET, [ |
| 264 | 'Accept' => 'application/json' |
| 265 | ], [ |
| 266 | 'name' => 'Mark=Hello' |
| 267 | ])); |
| 268 | |
| 269 | $this->assertEquals(200, $response->getStatusCode()); |
| 270 | $this->assertEquals('GET', $response->getBody()->method); |
| 271 | $this->assertEquals('Mark=Hello', $response->getBody()->queryString->name); |
| 272 | } |
| 273 | |
| 274 | public function testGetWithEqualSignAlt() |
| 275 | { |
| 276 | $httpClient = new HttpClient(); |
| 277 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::GET, [ |
| 278 | 'Accept' => 'application/json' |
| 279 | ], [ |
| 280 | 'name' => 'Mark=Hello=John' |
| 281 | ])); |
| 282 | |
| 283 | $this->assertEquals(200, $response->getStatusCode()); |
| 284 | $this->assertEquals('GET', $response->getBody()->method); |
| 285 | $this->assertEquals('Mark=Hello=John', $response->getBody()->queryString->name); |
| 286 | } |
| 287 | |
| 288 | public function testGetWithComplexQuery() |
| 289 | { |
| 290 | $httpClient = new HttpClient(); |
| 291 | $response = $httpClient->execute(new Request( |
| 292 | 'http://mockbin.com/request?query=[{"type":"/music/album","name":null,"artist":' . |
| 293 | '{"id":"/en/bob_dylan"},"limit":3}]&cursor' |
| 294 | )); |
| 295 | |
| 296 | $this->assertEquals(200, $response->getStatusCode()); |
| 297 | $this->assertEquals('GET', $response->getBody()->method); |
| 298 | $this->assertEquals('', $response->getBody()->queryString->cursor); |
| 299 | $this->assertEquals( |
| 300 | '[{"type":"/music/album","name":null,"artist":{"id":"/en/bob_dylan"},"limit":3}]', |
| 301 | $response->getBody()->queryString->query |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | public function testGetArray() |
| 306 | { |
| 307 | $httpClient = new HttpClient(); |
| 308 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::GET, [], [ |
| 309 | 'name[0]' => 'Mark', |
| 310 | 'name[1]' => 'John' |
| 311 | ])); |
| 312 | |
| 313 | $this->assertEquals(200, $response->getStatusCode()); |
| 314 | $this->assertEquals('GET', $response->getBody()->method); |
| 315 | $this->assertEquals('Mark', $response->getBody()->queryString->name[0]); |
| 316 | $this->assertEquals('John', $response->getBody()->queryString->name[1]); |
| 317 | } |
| 318 | |
| 319 | // HEAD |
| 320 | public function testHead() |
| 321 | { |
| 322 | $httpClient = new HttpClient(); |
| 323 | $response = $httpClient->execute(new Request('http://mockbin.com/request?name=Mark', RequestMethod::HEAD, [ |
| 324 | 'Accept' => 'application/json' |
| 325 | ])); |
| 326 | |
| 327 | $this->assertEquals(200, $response->getStatusCode()); |
| 328 | } |
| 329 | |
| 330 | // POST |
| 331 | public function testPost() |
| 332 | { |
| 333 | $httpClient = new HttpClient(); |
| 334 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 335 | 'Accept' => 'application/json' |
| 336 | ], [ |
| 337 | 'name' => 'Mark', |
| 338 | 'nick' => 'thefosk' |
| 339 | ])); |
| 340 | |
| 341 | $this->assertEquals(200, $response->getStatusCode()); |
| 342 | $this->assertEquals('POST', $response->getBody()->method); |
| 343 | $this->assertEquals('Mark', $response->getBody()->postData->params->name); |
| 344 | $this->assertEquals('thefosk', $response->getBody()->postData->params->nick); |
| 345 | } |
| 346 | |
| 347 | public function testPostForm() |
| 348 | { |
| 349 | $httpClient = new HttpClient(); |
| 350 | $body = Body::Form([ |
| 351 | 'name' => 'Mark', |
| 352 | 'nick' => 'thefosk' |
| 353 | ]); |
| 354 | |
| 355 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 356 | 'Accept' => 'application/json' |
| 357 | ], $body)); |
| 358 | |
| 359 | $this->assertEquals('POST', $response->getBody()->method); |
| 360 | $this->assertEquals('application/x-www-form-urlencoded', $response->getBody()->headers->{'content-type'}); |
| 361 | $this->assertEquals('application/x-www-form-urlencoded', $response->getBody()->postData->mimeType); |
| 362 | $this->assertEquals('Mark', $response->getBody()->postData->params->name); |
| 363 | $this->assertEquals('thefosk', $response->getBody()->postData->params->nick); |
| 364 | } |
| 365 | |
| 366 | public function testPostMultipart() |
| 367 | { |
| 368 | $httpClient = new HttpClient(); |
| 369 | $body = Body::Multipart([ |
| 370 | 'name' => 'Mark', |
| 371 | 'nick' => 'thefosk' |
| 372 | ]); |
| 373 | |
| 374 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 375 | 'Accept' => 'application/json', |
| 376 | ], $body)); |
| 377 | |
| 378 | $this->assertEquals('POST', $response->getBody()->method); |
| 379 | $this->assertEquals('multipart/form-data', explode(';', $response->getBody()->headers->{'content-type'})[0]); |
| 380 | $this->assertEquals('multipart/form-data', $response->getBody()->postData->mimeType); |
| 381 | $this->assertEquals('Mark', $response->getBody()->postData->params->name); |
| 382 | $this->assertEquals('thefosk', $response->getBody()->postData->params->nick); |
| 383 | } |
| 384 | |
| 385 | public function testPostWithEqualSign() |
| 386 | { |
| 387 | $httpClient = new HttpClient(); |
| 388 | $body = Body::Form([ |
| 389 | 'name' => 'Mark=Hello' |
| 390 | ]); |
| 391 | |
| 392 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 393 | 'Accept' => 'application/json' |
| 394 | ], $body)); |
| 395 | |
| 396 | $this->assertEquals(200, $response->getStatusCode()); |
| 397 | $this->assertEquals('POST', $response->getBody()->method); |
| 398 | $this->assertEquals('Mark=Hello', $response->getBody()->postData->params->name); |
| 399 | } |
| 400 | |
| 401 | public function testPostArray() |
| 402 | { |
| 403 | $httpClient = new HttpClient(); |
| 404 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 405 | 'Accept' => 'application/json' |
| 406 | ], [ |
| 407 | 'name[0]' => 'Mark', |
| 408 | 'name[1]' => 'John' |
| 409 | ])); |
| 410 | |
| 411 | $this->assertEquals(200, $response->getStatusCode()); |
| 412 | $this->assertEquals('POST', $response->getBody()->method); |
| 413 | $this->assertEquals('Mark', $response->getBody()->postData->params->{'name[0]'}); |
| 414 | $this->assertEquals('John', $response->getBody()->postData->params->{'name[1]'}); |
| 415 | } |
| 416 | |
| 417 | public function testPostWithDots() |
| 418 | { |
| 419 | $httpClient = new HttpClient(); |
| 420 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 421 | 'Accept' => 'application/json' |
| 422 | ], [ |
| 423 | 'user.name' => 'Mark', |
| 424 | 'nick' => 'thefosk' |
| 425 | ])); |
| 426 | |
| 427 | $this->assertEquals(200, $response->getStatusCode()); |
| 428 | $this->assertEquals('POST', $response->getBody()->method); |
| 429 | $this->assertEquals('Mark', $response->getBody()->postData->params->{'user.name'}); |
| 430 | $this->assertEquals('thefosk', $response->getBody()->postData->params->nick); |
| 431 | } |
| 432 | |
| 433 | public function testRawPost() |
| 434 | { |
| 435 | $httpClient = new HttpClient(); |
| 436 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 437 | 'Accept' => 'application/json', |
| 438 | 'Content-Type' => 'application/json' |
| 439 | ], json_encode([ |
| 440 | 'author' => 'Sam Sullivan' |
| 441 | ]))); |
| 442 | |
| 443 | $this->assertEquals(200, $response->getStatusCode()); |
| 444 | $this->assertEquals('POST', $response->getBody()->method); |
| 445 | $this->assertEquals('Sam Sullivan', json_decode($response->getBody()->postData->text)->author); |
| 446 | } |
| 447 | |
| 448 | public function testPostMultidimensionalArray() |
| 449 | { |
| 450 | $httpClient = new HttpClient(); |
| 451 | $body = Body::Form([ |
| 452 | 'key' => 'value', |
| 453 | 'items' => [ |
| 454 | 'item1', |
| 455 | 'item2' |
| 456 | ] |
| 457 | ]); |
| 458 | |
| 459 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 460 | 'Accept' => 'application/json' |
| 461 | ], $body)); |
| 462 | |
| 463 | $this->assertEquals(200, $response->getStatusCode()); |
| 464 | $this->assertEquals('POST', $response->getBody()->method); |
| 465 | $this->assertEquals('value', $response->getBody()->postData->params->key); |
| 466 | $this->assertEquals('item1', $response->getBody()->postData->params->{'items[0]'}); |
| 467 | $this->assertEquals('item2', $response->getBody()->postData->params->{'items[1]'}); |
| 468 | } |
| 469 | |
| 470 | // PUT |
| 471 | public function testPut() |
| 472 | { |
| 473 | $httpClient = new HttpClient(); |
| 474 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::PUT, [ |
| 475 | 'Accept' => 'application/json' |
| 476 | ], [ |
| 477 | 'name' => 'Mark', |
| 478 | 'nick' => 'thefosk' |
| 479 | ])); |
| 480 | |
| 481 | $this->assertEquals(200, $response->getStatusCode()); |
| 482 | $this->assertEquals('PUT', $response->getBody()->method); |
| 483 | $this->assertEquals('Mark', $response->getBody()->postData->params->name); |
| 484 | $this->assertEquals('thefosk', $response->getBody()->postData->params->nick); |
| 485 | } |
| 486 | |
| 487 | // PATCH |
| 488 | public function testPatch() |
| 489 | { |
| 490 | $httpClient = new HttpClient(); |
| 491 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::PATCH, [ |
| 492 | 'Accept' => 'application/json' |
| 493 | ], [ |
| 494 | 'name' => 'Mark', |
| 495 | 'nick' => 'thefosk' |
| 496 | ])); |
| 497 | |
| 498 | $this->assertEquals(200, $response->getStatusCode()); |
| 499 | $this->assertEquals('PATCH', $response->getBody()->method); |
| 500 | $this->assertEquals('Mark', $response->getBody()->postData->params->name); |
| 501 | $this->assertEquals('thefosk', $response->getBody()->postData->params->nick); |
| 502 | } |
| 503 | |
| 504 | // DELETE |
| 505 | public function testDelete() |
| 506 | { |
| 507 | $httpClient = new HttpClient(); |
| 508 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::DELETE, [ |
| 509 | 'Accept' => 'application/json', |
| 510 | 'Content-Type' => 'application/x-www-form-urlencoded' |
| 511 | ], [ |
| 512 | 'name' => 'Mark', |
| 513 | 'nick' => 'thefosk' |
| 514 | ])); |
| 515 | |
| 516 | $this->assertEquals(200, $response->getStatusCode()); |
| 517 | $this->assertEquals('DELETE', $response->getBody()->method); |
| 518 | } |
| 519 | |
| 520 | // Upload |
| 521 | public function testUpload() |
| 522 | { |
| 523 | $httpClient = new HttpClient(); |
| 524 | $fixture = __DIR__ . '/Mocking/upload.txt'; |
| 525 | |
| 526 | $headers = ['Accept' => 'application/json']; |
| 527 | $files = ['file' => $fixture]; |
| 528 | $data = ['name' => 'ahmad']; |
| 529 | |
| 530 | $body = Body::multipart($data, $files); |
| 531 | |
| 532 | $response = $httpClient->execute(new Request( |
| 533 | 'http://mockbin.com/request', |
| 534 | RequestMethod::POST, |
| 535 | $headers, |
| 536 | $body |
| 537 | )); |
| 538 | |
| 539 | $this->assertEquals(200, $response->getStatusCode()); |
| 540 | $this->assertEquals('POST', $response->getBody()->method); |
| 541 | $this->assertEquals('ahmad', $response->getBody()->postData->params->name); |
| 542 | $this->assertEquals('This is a test', $response->getBody()->postData->params->file); |
| 543 | } |
| 544 | |
| 545 | public function testUploadWithoutHelper() |
| 546 | { |
| 547 | $httpClient = new HttpClient(); |
| 548 | $fixture = __DIR__ . '/Mocking/upload.txt'; |
| 549 | |
| 550 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 551 | 'Accept' => 'application/json' |
| 552 | ], [ |
| 553 | 'name' => 'Mark', |
| 554 | 'file' => Body::File($fixture) |
| 555 | ])); |
| 556 | |
| 557 | $this->assertEquals(200, $response->getStatusCode()); |
| 558 | $this->assertEquals('POST', $response->getBody()->method); |
| 559 | $this->assertEquals('Mark', $response->getBody()->postData->params->name); |
| 560 | $this->assertEquals('This is a test', $response->getBody()->postData->params->file); |
| 561 | } |
| 562 | |
| 563 | public function testUploadIfFilePartOfData() |
| 564 | { |
| 565 | $httpClient = new HttpClient(); |
| 566 | $fixture = __DIR__ . '/Mocking/upload.txt'; |
| 567 | |
| 568 | $response = $httpClient->execute(new Request('http://mockbin.com/request', RequestMethod::POST, [ |
| 569 | 'Accept' => 'application/json' |
| 570 | ], [ |
| 571 | 'name' => 'Mark', |
| 572 | 'files[owl.gif]' => Body::File($fixture) |
| 573 | ])); |
| 574 | |
| 575 | $this->assertEquals(200, $response->getStatusCode()); |
| 576 | $this->assertEquals('POST', $response->getBody()->method); |
| 577 | $this->assertEquals('Mark', $response->getBody()->postData->params->name); |
| 578 | $this->assertEquals('This is a test', $response->getBody()->postData->params->{'files[owl.gif]'}); |
| 579 | } |
| 580 | } |
| 581 |