public function requestShouldBindToStreamEventsAndUseconnector()
{
$requestData = new RequestData('GET', 'http://www.example.com');
$request = new Request($this->connector, $requestData);
$this->successfulConnectionMock();
$this->stream->expects($this->at(0))->method('on')->with('drain', $this->identicalTo(array($request, 'handleDrain')));
$this->stream->expects($this->at(1))->method('on')->with('data', $this->identicalTo(array($request, 'handleData')));
$this->stream->expects($this->at(2))->method('on')->with('end', $this->identicalTo(array($request, 'handleEnd')));
$this->stream->expects($this->at(3))->method('on')->with('error', $this->identicalTo(array($request, 'handleError')));
$this->stream->expects($this->at(5))->method('removeListener')->with('drain', $this->identicalTo(array($request, 'handleDrain')));
$this->stream->expects($this->at(6))->method('removeListener')->with('data', $this->identicalTo(array($request, 'handleData')));
$this->stream->expects($this->at(7))->method('removeListener')->with('end', $this->identicalTo(array($request, 'handleEnd')));
$this->stream->expects($this->at(8))->method('removeListener')->with('error', $this->identicalTo(array($request, 'handleError')));
$response = $this->response;
$this->stream->expects($this->once())->method('emit')->with('data', $this->identicalTo(array('body')));
$response->expects($this->at(0))->method('on')->with('end', $this->anything())->will($this->returnCallback(function ($event, $cb) use(&$endCallback) {
$endCallback = $cb;
}));
$factory = $this->createCallableMock();
$factory->expects($this->once())->method('__invoke')->with('HTTP', '1.0', '200', 'OK', array('Content-Type' => 'text/plain'))->will($this->returnValue($response));
$request->setResponseFactory($factory);
$handler = $this->createCallableMock();
$handler->expects($this->once())->method('__invoke')->with($response);
$request->on('response', $handler);
$request->on('close', $this->expectCallableNever());
$handler = $this->createCallableMock();
$handler->expects($this->once())->method('__invoke')->with(null, $this->isInstanceof('React\\HttpClient\\Response'), $this->isInstanceof('React\\HttpClient\\Request'));
$request->on('end', $handler);
$request->end();
$request->handleData("HTTP/1.0 200 OK\r\n");
$request->handleData("Content-Type: text/plain\r\n");
$request->handleData("\r\nbody");
$this->assertNotNull($endCallback);
call_user_func($endCallback);
}