|
7 | 7 | use React\Stream\Stream; |
8 | 8 | use React\Promise\FulfilledPromise; |
9 | 9 | use React\Promise\RejectedPromise; |
| 10 | +use React\Promise; |
| 11 | +use React\Promise\Deferred; |
10 | 12 |
|
11 | 13 | class RequestTest extends TestCase |
12 | 14 | { |
@@ -288,6 +290,53 @@ public function writeWithAPostRequestShouldSendToTheStream() |
288 | 290 | $request->handleData("\r\nbody"); |
289 | 291 | } |
290 | 292 |
|
| 293 | + /** @test */ |
| 294 | + public function writeWithAPostRequestShouldSendBodyAfterHeadersAndEmitDrainEvent() |
| 295 | + { |
| 296 | + $requestData = new RequestData('POST', 'http://www.example.com'); |
| 297 | + $request = new Request($this->connector, $requestData); |
| 298 | + |
| 299 | + $resolveConnection = $this->successfulAsyncConnectionMock(); |
| 300 | + |
| 301 | + $this->stream |
| 302 | + ->expects($this->at(4)) |
| 303 | + ->method('write') |
| 304 | + ->with($this->matchesRegularExpression("#^POST / HTTP/1\.0\r\nHost: www.example.com\r\nUser-Agent:.*\r\n\r\n$#")); |
| 305 | + $this->stream |
| 306 | + ->expects($this->at(5)) |
| 307 | + ->method('write') |
| 308 | + ->with($this->identicalTo("some")); |
| 309 | + $this->stream |
| 310 | + ->expects($this->at(6)) |
| 311 | + ->method('write') |
| 312 | + ->with($this->identicalTo("post")); |
| 313 | + $this->stream |
| 314 | + ->expects($this->at(7)) |
| 315 | + ->method('write') |
| 316 | + ->with($this->identicalTo("data")); |
| 317 | + |
| 318 | + $factory = $this->createCallableMock(); |
| 319 | + $factory->expects($this->once()) |
| 320 | + ->method('__invoke') |
| 321 | + ->will($this->returnValue($this->response)); |
| 322 | + |
| 323 | + $request->setResponseFactory($factory); |
| 324 | + |
| 325 | + $this->assertFalse($request->write("some")); |
| 326 | + $this->assertFalse($request->write("post")); |
| 327 | + |
| 328 | + $request->once('drain', function () use ($request) { |
| 329 | + $request->write("data"); |
| 330 | + $request->end(); |
| 331 | + }); |
| 332 | + |
| 333 | + $resolveConnection(); |
| 334 | + |
| 335 | + $request->handleData("HTTP/1.0 200 OK\r\n"); |
| 336 | + $request->handleData("Content-Type: text/plain\r\n"); |
| 337 | + $request->handleData("\r\nbody"); |
| 338 | + } |
| 339 | + |
291 | 340 | /** @test */ |
292 | 341 | public function pipeShouldPipeDataIntoTheRequestBody() |
293 | 342 | { |
@@ -387,11 +436,22 @@ public function requestShouldRelayErrorEventsFromResponse() |
387 | 436 |
|
388 | 437 | private function successfulConnectionMock() |
389 | 438 | { |
| 439 | + call_user_func($this->successfulAsyncConnectionMock()); |
| 440 | + } |
| 441 | + |
| 442 | + private function successfulAsyncConnectionMock() |
| 443 | + { |
| 444 | + $deferred = new Deferred(); |
| 445 | + |
390 | 446 | $this->connector |
391 | 447 | ->expects($this->once()) |
392 | 448 | ->method('create') |
393 | 449 | ->with('www.example.com', 80) |
394 | | - ->will($this->returnValue(new FulfilledPromise($this->stream))); |
| 450 | + ->will($this->returnValue($deferred->promise())); |
| 451 | + |
| 452 | + return function () use ($deferred) { |
| 453 | + $deferred->resolve($this->stream); |
| 454 | + }; |
395 | 455 | } |
396 | 456 |
|
397 | 457 | private function rejectedConnectionMock() |
|
0 commit comments