Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the abandoned guzzle/parser #34

Merged
merged 5 commits into from
Aug 30, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 14 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 g7;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry to nitpick but can we come up with another alias? I used gPsr other places. At the rate Guzzle releases major versions this could quickly get mixed up with misunderstanding "Guzzle7".

use React\SocketClient\ConnectorInterface;
use React\Stream\WritableStreamInterface;

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

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

$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'));
}
}