Skip to content

Commit fed9c06

Browse files
committed
Merge pull request #34 from reactphp/guzzle-message-parser-replacement
Replace the abandoned guzzle/parser
2 parents 5e54eb8 + 4c3b5db commit fed9c06

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "MIT",
66
"require": {
77
"php": ">=5.4.0",
8-
"guzzle/parser": "~3.0",
8+
"guzzlehttp/psr7": "^1.0",
99
"react/socket-client": "0.4.*",
1010
"react/dns": "0.4.*",
1111
"react/event-loop": "0.4.*",

src/Request.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace React\HttpClient;
44

55
use Evenement\EventEmitterTrait;
6-
use Guzzle\Parser\Message\MessageParser;
6+
use GuzzleHttp\Psr7 as gPsr;
77
use React\SocketClient\ConnectorInterface;
88
use React\Stream\WritableStreamInterface;
99

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

204204
protected function parseResponse($data)
205205
{
206-
$parser = new MessageParser();
207-
$parsed = $parser->parseResponse($data);
206+
$psrResponse = gPsr\parse_response($data);
207+
$headers = array_map(function($val) {
208+
if (1 === count($val)) {
209+
$val = $val[0];
210+
}
211+
212+
return $val;
213+
}, $psrResponse->getHeaders());
208214

209215
$factory = $this->getResponseFactory();
210216

211217
$response = $factory(
212-
$parsed['protocol'],
213-
$parsed['version'],
214-
$parsed['code'],
215-
$parsed['reason_phrase'],
216-
$parsed['headers']
218+
'HTTP',
219+
$psrResponse->getProtocolVersion(),
220+
$psrResponse->getStatusCode(),
221+
$psrResponse->getReasonPhrase(),
222+
$headers
217223
);
218224

219-
return array($response, $parsed['body']);
225+
return array($response, $psrResponse->getBody());
220226
}
221227

222228
protected function connect()

tests/RequestTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,5 +462,43 @@ private function rejectedConnectionMock()
462462
->with('www.example.com', 80)
463463
->will($this->returnValue(new RejectedPromise(new \RuntimeException())));
464464
}
465-
}
466465

466+
/** @test */
467+
public function multivalueHeader()
468+
{
469+
$requestData = new RequestData('GET', 'http://www.example.com');
470+
$request = new Request($this->connector, $requestData);
471+
472+
$this->successfulConnectionMock();
473+
474+
$response = $this->response;
475+
476+
$response->expects($this->at(0))
477+
->method('on')
478+
->with('end', $this->anything());
479+
$response->expects($this->at(1))
480+
->method('on')
481+
->with('error', $this->anything())
482+
->will($this->returnCallback(function ($event, $cb) use (&$errorCallback) {
483+
$errorCallback = $cb;
484+
}));
485+
486+
$factory = $this->createCallableMock();
487+
$factory->expects($this->once())
488+
->method('__invoke')
489+
->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'))
490+
->will($this->returnValue($response));
491+
492+
$request->setResponseFactory($factory);
493+
$request->end();
494+
495+
$request->handleData("HTTP/1.0 200 OK\r\n");
496+
$request->handleData("Content-Type: text/plain\r\n");
497+
$request->handleData("X-Xss-Protection:1; mode=block\r\n");
498+
$request->handleData("Cache-Control:public, must-revalidate, max-age=0\r\n");
499+
$request->handleData("\r\nbody");
500+
501+
$this->assertNotNull($errorCallback);
502+
call_user_func($errorCallback, new \Exception('test'));
503+
}
504+
}

0 commit comments

Comments
 (0)