@@ -221,7 +221,7 @@ public function requestShouldEmitErrorIfGuzzleParseThrowsException()
221221
222222 $ request ->on ('error ' , $ handler );
223223
224- $ request ->writeHead ();
224+ $ request ->end ();
225225 $ request ->handleData ("\r\n\r\n" );
226226 }
227227
@@ -253,13 +253,9 @@ public function postRequestShouldSendAPostRequest()
253253 $ this ->successfulConnectionMock ();
254254
255255 $ this ->stream
256- ->expects ($ this ->at (5 ))
257- ->method ('write ' )
258- ->with ($ this ->matchesRegularExpression ("#^POST / HTTP/1\.0 \r\nHost: www.example.com \r\nUser-Agent:.* \r\n\r\n$# " ));
259- $ this ->stream
260- ->expects ($ this ->at (6 ))
256+ ->expects ($ this ->once ())
261257 ->method ('write ' )
262- ->with ($ this ->identicalTo ( " some post data " ));
258+ ->with ($ this ->matchesRegularExpression ( " #^POST / HTTP/1\.0 \r\n Host: www.example.com \r\n User-Agent:.* \r\n\r\n some post data$# " ));
263259
264260 $ factory = $ this ->createCallableMock ();
265261 $ factory ->expects ($ this ->once ())
@@ -285,17 +281,13 @@ public function writeWithAPostRequestShouldSendToTheStream()
285281 $ this ->stream
286282 ->expects ($ this ->at (5 ))
287283 ->method ('write ' )
288- ->with ($ this ->matchesRegularExpression ("#^POST / HTTP/1\.0 \r\nHost: www.example.com \r\nUser-Agent:.* \r\n\r\n $# " ));
284+ ->with ($ this ->matchesRegularExpression ("#^POST / HTTP/1\.0 \r\nHost: www.example.com \r\nUser-Agent:.* \r\n\r\n some $# " ));
289285 $ this ->stream
290286 ->expects ($ this ->at (6 ))
291287 ->method ('write ' )
292- ->with ($ this ->identicalTo ("some " ));
293- $ this ->stream
294- ->expects ($ this ->at (7 ))
295- ->method ('write ' )
296288 ->with ($ this ->identicalTo ("post " ));
297289 $ this ->stream
298- ->expects ($ this ->at (8 ))
290+ ->expects ($ this ->at (7 ))
299291 ->method ('write ' )
300292 ->with ($ this ->identicalTo ("data " ));
301293
@@ -326,17 +318,56 @@ public function writeWithAPostRequestShouldSendBodyAfterHeadersAndEmitDrainEvent
326318 $ this ->stream
327319 ->expects ($ this ->at (5 ))
328320 ->method ('write ' )
329- ->with ($ this ->matchesRegularExpression ("#^POST / HTTP/1\.0 \r\nHost: www.example.com \r\nUser-Agent:.* \r\n\r\n$# " ));
321+ ->with ($ this ->matchesRegularExpression ("#^POST / HTTP/1\.0 \r\nHost: www.example.com \r\nUser-Agent:.* \r\n\r\nsomepost$# " ))
322+ ->willReturn (true );
330323 $ this ->stream
331324 ->expects ($ this ->at (6 ))
332325 ->method ('write ' )
333- ->with ($ this ->identicalTo ("some " ));
326+ ->with ($ this ->identicalTo ("data " ));
327+
328+ $ factory = $ this ->createCallableMock ();
329+ $ factory ->expects ($ this ->once ())
330+ ->method ('__invoke ' )
331+ ->will ($ this ->returnValue ($ this ->response ));
332+
333+ $ request ->setResponseFactory ($ factory );
334+
335+ $ this ->assertFalse ($ request ->write ("some " ));
336+ $ this ->assertFalse ($ request ->write ("post " ));
337+
338+ $ request ->on ('drain ' , $ this ->expectCallableOnce ());
339+ $ request ->once ('drain ' , function () use ($ request ) {
340+ $ request ->write ("data " );
341+ $ request ->end ();
342+ });
343+
344+ $ resolveConnection ();
345+
346+ $ request ->handleData ("HTTP/1.0 200 OK \r\n" );
347+ $ request ->handleData ("Content-Type: text/plain \r\n" );
348+ $ request ->handleData ("\r\nbody " );
349+ }
350+
351+ /** @test */
352+ public function writeWithAPostRequestShouldForwardDrainEventIfFirstChunkExceedsBuffer ()
353+ {
354+ $ requestData = new RequestData ('POST ' , 'http://www.example.com ' );
355+ $ request = new Request ($ this ->connector , $ requestData );
356+
357+ $ this ->stream = $ this ->getMockBuilder ('React\Socket\Connection ' )
358+ ->disableOriginalConstructor ()
359+ ->setMethods (array ('write ' ))
360+ ->getMock ();
361+
362+ $ resolveConnection = $ this ->successfulAsyncConnectionMock ();
363+
334364 $ this ->stream
335- ->expects ($ this ->at (7 ))
365+ ->expects ($ this ->at (0 ))
336366 ->method ('write ' )
337- ->with ($ this ->identicalTo ("post " ));
367+ ->with ($ this ->matchesRegularExpression ("#^POST / HTTP/1\.0 \r\nHost: www.example.com \r\nUser-Agent:.* \r\n\r\nsomepost$# " ))
368+ ->willReturn (false );
338369 $ this ->stream
339- ->expects ($ this ->at (8 ))
370+ ->expects ($ this ->at (1 ))
340371 ->method ('write ' )
341372 ->with ($ this ->identicalTo ("data " ));
342373
@@ -350,12 +381,14 @@ public function writeWithAPostRequestShouldSendBodyAfterHeadersAndEmitDrainEvent
350381 $ this ->assertFalse ($ request ->write ("some " ));
351382 $ this ->assertFalse ($ request ->write ("post " ));
352383
384+ $ request ->on ('drain ' , $ this ->expectCallableOnce ());
353385 $ request ->once ('drain ' , function () use ($ request ) {
354386 $ request ->write ("data " );
355387 $ request ->end ();
356388 });
357389
358390 $ resolveConnection ();
391+ $ this ->stream ->emit ('drain ' );
359392
360393 $ request ->handleData ("HTTP/1.0 200 OK \r\n" );
361394 $ request ->handleData ("Content-Type: text/plain \r\n" );
@@ -373,17 +406,13 @@ public function pipeShouldPipeDataIntoTheRequestBody()
373406 $ this ->stream
374407 ->expects ($ this ->at (5 ))
375408 ->method ('write ' )
376- ->with ($ this ->matchesRegularExpression ("#^POST / HTTP/1\.0 \r\nHost: www.example.com \r\nUser-Agent:.* \r\n\r\n $# " ));
409+ ->with ($ this ->matchesRegularExpression ("#^POST / HTTP/1\.0 \r\nHost: www.example.com \r\nUser-Agent:.* \r\n\r\n some $# " ));
377410 $ this ->stream
378411 ->expects ($ this ->at (6 ))
379412 ->method ('write ' )
380- ->with ($ this ->identicalTo ("some " ));
381- $ this ->stream
382- ->expects ($ this ->at (7 ))
383- ->method ('write ' )
384413 ->with ($ this ->identicalTo ("post " ));
385414 $ this ->stream
386- ->expects ($ this ->at (8 ))
415+ ->expects ($ this ->at (7 ))
387416 ->method ('write ' )
388417 ->with ($ this ->identicalTo ("data " ));
389418
@@ -424,6 +453,32 @@ public function endShouldOnlyAcceptScalars()
424453 $ request ->end (array ());
425454 }
426455
456+ /**
457+ * @test
458+ */
459+ public function closeShouldEmitCloseEvent ()
460+ {
461+ $ requestData = new RequestData ('POST ' , 'http://www.example.com ' );
462+ $ request = new Request ($ this ->connector , $ requestData );
463+
464+ $ request ->on ('close ' , $ this ->expectCallableOnce ());
465+ $ request ->close ();
466+ }
467+
468+ /**
469+ * @test
470+ */
471+ public function writeAfterCloseReturnsFalse ()
472+ {
473+ $ requestData = new RequestData ('POST ' , 'http://www.example.com ' );
474+ $ request = new Request ($ this ->connector , $ requestData );
475+
476+ $ request ->close ();
477+
478+ $ this ->assertFalse ($ request ->isWritable ());
479+ $ this ->assertFalse ($ request ->write ('nope ' ));
480+ }
481+
427482 /** @test */
428483 public function requestShouldRelayErrorEventsFromResponse ()
429484 {
0 commit comments