diff --git a/src/Io/Sender.php b/src/Io/Sender.php index 0894c574..acbb6e7d 100644 --- a/src/Io/Sender.php +++ b/src/Io/Sender.php @@ -94,8 +94,10 @@ public function send(RequestInterface $request) } // automatically add `Connection: close` request header for HTTP/1.1 requests to avoid connection reuse - if ($request->getProtocolVersion() === '1.1' && !$request->hasHeader('Connection')) { + if ($request->getProtocolVersion() === '1.1') { $request = $request->withHeader('Connection', 'close'); + } else { + $request = $request->withoutHeader('Connection'); } // automatically add `Authorization: Basic …` request header if URL includes `user:pass@host` diff --git a/tests/Io/SenderTest.php b/tests/Io/SenderTest.php index c2357a1a..4ef06442 100644 --- a/tests/Io/SenderTest.php +++ b/tests/Io/SenderTest.php @@ -302,6 +302,20 @@ public function getHttp10RequestShouldSendAGetRequestWithoutConnectionHeaderByDe $sender->send($request); } + /** @test */ + public function getHttp10RequestShouldSendAGetRequestWithoutConnectionHeaderEvenWhenConnectionKeepAliveHeaderIsSpecified() + { + $client = $this->getMockBuilder('React\Http\Client\Client')->disableOriginalConstructor()->getMock(); + $client->expects($this->once())->method('request')->with($this->callback(function (RequestInterface $request) { + return !$request->hasHeader('Connection'); + }))->willReturn($this->getMockBuilder('React\Http\Io\ClientRequestStream')->disableOriginalConstructor()->getMock()); + + $sender = new Sender($client); + + $request = new Request('GET', 'http://www.example.com', array('Connection' => 'keep-alive'), '', '1.0'); + $sender->send($request); + } + /** @test */ public function getHttp11RequestShouldSendAGetRequestWithConnectionCloseHeaderByDefault() { @@ -317,16 +331,16 @@ public function getHttp11RequestShouldSendAGetRequestWithConnectionCloseHeaderBy } /** @test */ - public function getHttp11RequestShouldSendAGetRequestWithGivenConnectionUpgradeHeader() + public function getHttp11RequestShouldSendAGetRequestWithConnectionCloseHeaderEvenWhenConnectionKeepAliveHeaderIsSpecified() { $client = $this->getMockBuilder('React\Http\Client\Client')->disableOriginalConstructor()->getMock(); $client->expects($this->once())->method('request')->with($this->callback(function (RequestInterface $request) { - return $request->getHeaderLine('Connection') === 'upgrade'; + return $request->getHeaderLine('Connection') === 'close'; }))->willReturn($this->getMockBuilder('React\Http\Io\ClientRequestStream')->disableOriginalConstructor()->getMock()); $sender = new Sender($client); - $request = new Request('GET', 'http://www.example.com', array('Connection' => 'upgrade'), '', '1.1'); + $request = new Request('GET', 'http://www.example.com', array('Connection' => 'keep-alive'), '', '1.1'); $sender->send($request); }