Skip to content

Commit

Permalink
Merge pull request #34 from reactphp/guzzle-message-parser-replacement
Browse files Browse the repository at this point in the history
Replace the abandoned guzzle/parser
  • Loading branch information
cboden committed Aug 30, 2015
2 parents 5e54eb8 + 4c3b5db commit fed9c06
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"require": {
"php": ">=5.4.0",
"guzzle/parser": "~3.0",
"guzzlehttp/psr7": "^1.0",
"react/socket-client": "0.4.*",
"react/dns": "0.4.*",
"react/event-loop": "0.4.*",
Expand Down
24 changes: 15 additions & 9 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace React\HttpClient;

use Evenement\EventEmitterTrait;
use Guzzle\Parser\Message\MessageParser;
use GuzzleHttp\Psr7 as gPsr;
use React\SocketClient\ConnectorInterface;
use React\Stream\WritableStreamInterface;

Expand Down Expand Up @@ -203,20 +203,26 @@ public function close(\Exception $error = null)

protected function parseResponse($data)
{
$parser = new MessageParser();
$parsed = $parser->parseResponse($data);
$psrResponse = gPsr\parse_response($data);
$headers = array_map(function($val) {
if (1 === count($val)) {
$val = $val[0];
}

return $val;
}, $psrResponse->getHeaders());

$factory = $this->getResponseFactory();

$response = $factory(
$parsed['protocol'],
$parsed['version'],
$parsed['code'],
$parsed['reason_phrase'],
$parsed['headers']
'HTTP',
$psrResponse->getProtocolVersion(),
$psrResponse->getStatusCode(),
$psrResponse->getReasonPhrase(),
$headers
);

return array($response, $parsed['body']);
return array($response, $psrResponse->getBody());
}

protected function connect()
Expand Down
40 changes: 39 additions & 1 deletion tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,5 +462,43 @@ private function rejectedConnectionMock()
->with('www.example.com', 80)
->will($this->returnValue(new RejectedPromise(new \RuntimeException())));
}
}

/** @test */
public function multivalueHeader()
{
$requestData = new RequestData('GET', 'http://www.example.com');
$request = new Request($this->connector, $requestData);

$this->successfulConnectionMock();

$response = $this->response;

$response->expects($this->at(0))
->method('on')
->with('end', $this->anything());
$response->expects($this->at(1))
->method('on')
->with('error', $this->anything())
->will($this->returnCallback(function ($event, $cb) use (&$errorCallback) {
$errorCallback = $cb;
}));

$factory = $this->createCallableMock();
$factory->expects($this->once())
->method('__invoke')
->with('HTTP', '1.0', '200', 'OK', array('Content-Type' => 'text/plain', 'X-Xss-Protection' => '1; mode=block', 'Cache-Control' => 'public, must-revalidate, max-age=0'))
->will($this->returnValue($response));

$request->setResponseFactory($factory);
$request->end();

$request->handleData("HTTP/1.0 200 OK\r\n");
$request->handleData("Content-Type: text/plain\r\n");
$request->handleData("X-Xss-Protection:1; mode=block\r\n");
$request->handleData("Cache-Control:public, must-revalidate, max-age=0\r\n");
$request->handleData("\r\nbody");

$this->assertNotNull($errorCallback);
call_user_func($errorCallback, new \Exception('test'));
}
}

0 comments on commit fed9c06

Please sign in to comment.