Skip to content

Commit 7189c6a

Browse files
authored
Merge pull request #495 from clue-labs/keep-alive-default
Enable HTTP keep-alive by default for HTTP client
2 parents 5d2df79 + 684421f commit 7189c6a

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

src/Browser.php

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class Browser
2323
private $baseUrl;
2424
private $protocolVersion = '1.1';
2525
private $defaultHeaders = array(
26-
'Connection' => 'close',
2726
'User-Agent' => 'ReactPHP/1'
2827
);
2928

tests/BrowserTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,6 @@ public function testWithMultipleHeadersShouldBeMergedCorrectlyWithMultipleDefaul
556556
'user-Agent' => array('ABC'),
557557
'another-header' => array('value'),
558558
'custom-header' => array('data'),
559-
560-
'Connection' => array('close')
561559
);
562560

563561
$that->assertEquals($expectedHeaders, $request->getHeaders());

tests/FunctionalBrowserTest.php

+49-4
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ public function testReceiveStreamAndExplicitlyCloseConnectionEvenWhenServerKeeps
553553
$socket->close();
554554
}
555555

556-
public function testRequestWillCreateNewConnectionForSecondRequestByDefaultEvenWhenServerKeepsConnectionOpen()
556+
public function testRequestWithConnectionCloseHeaderWillCreateNewConnectionForSecondRequestEvenWhenServerKeepsConnectionOpen()
557557
{
558558
$twice = $this->expectCallableOnce();
559559
$socket = new SocketServer('127.0.0.1:0');
@@ -570,6 +570,9 @@ public function testRequestWillCreateNewConnectionForSecondRequestByDefaultEvenW
570570

571571
$this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/';
572572

573+
// add `Connection: close` request header to disable HTTP keep-alive
574+
$this->browser = $this->browser->withHeader('Connection', 'close');
575+
573576
$response = \React\Async\await($this->browser->get($this->base . 'get'));
574577
assert($response instanceof ResponseInterface);
575578
$this->assertEquals('hello', (string)$response->getBody());
@@ -579,12 +582,54 @@ public function testRequestWillCreateNewConnectionForSecondRequestByDefaultEvenW
579582
$this->assertEquals('hello', (string)$response->getBody());
580583
}
581584

582-
public function testRequestWithoutConnectionHeaderWillReuseExistingConnectionForSecondRequest()
585+
public function testRequestWithHttp10WillCreateNewConnectionForSecondRequestEvenWhenServerKeepsConnectionOpen()
586+
{
587+
$twice = $this->expectCallableOnce();
588+
$socket = new SocketServer('127.0.0.1:0');
589+
$socket->on('connection', function (\React\Socket\ConnectionInterface $connection) use ($socket, $twice) {
590+
$connection->on('data', function () use ($connection) {
591+
$connection->write("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello");
592+
});
593+
594+
$socket->on('connection', $twice);
595+
$socket->on('connection', function () use ($socket) {
596+
$socket->close();
597+
});
598+
});
599+
600+
$this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/';
601+
602+
// use HTTP/1.0 to disable HTTP keep-alive
603+
$this->browser = $this->browser->withProtocolVersion('1.0');
604+
605+
$response = \React\Async\await($this->browser->get($this->base . 'get'));
606+
assert($response instanceof ResponseInterface);
607+
$this->assertEquals('hello', (string)$response->getBody());
608+
609+
$response = \React\Async\await($this->browser->get($this->base . 'get'));
610+
assert($response instanceof ResponseInterface);
611+
$this->assertEquals('hello', (string)$response->getBody());
612+
}
613+
614+
public function testRequestWillReuseExistingConnectionForSecondRequestByDefault()
583615
{
584616
$this->socket->on('connection', $this->expectCallableOnce());
585617

586-
// remove default `Connection: close` request header to enable keep-alive
587-
$this->browser = $this->browser->withoutHeader('Connection');
618+
$response = \React\Async\await($this->browser->get($this->base . 'get'));
619+
assert($response instanceof ResponseInterface);
620+
$this->assertEquals('hello', (string)$response->getBody());
621+
622+
$response = \React\Async\await($this->browser->get($this->base . 'get'));
623+
assert($response instanceof ResponseInterface);
624+
$this->assertEquals('hello', (string)$response->getBody());
625+
}
626+
627+
public function testRequestWithHttp10AndConnectionKeepAliveHeaderWillReuseExistingConnectionForSecondRequest()
628+
{
629+
$this->socket->on('connection', $this->expectCallableOnce());
630+
631+
$this->browser = $this->browser->withProtocolVersion('1.0');
632+
$this->browser = $this->browser->withHeader('Connection', 'keep-alive');
588633

589634
$response = \React\Async\await($this->browser->get($this->base . 'get'));
590635
assert($response instanceof ResponseInterface);

0 commit comments

Comments
 (0)