Skip to content

Commit

Permalink
Merge pull request #495 from clue-labs/keep-alive-default
Browse files Browse the repository at this point in the history
Enable HTTP keep-alive by default for HTTP client
  • Loading branch information
WyriHaximus committed Apr 21, 2023
2 parents 5d2df79 + 684421f commit 7189c6a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class Browser
private $baseUrl;
private $protocolVersion = '1.1';
private $defaultHeaders = array(
'Connection' => 'close',
'User-Agent' => 'ReactPHP/1'
);

Expand Down
2 changes: 0 additions & 2 deletions tests/BrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,6 @@ public function testWithMultipleHeadersShouldBeMergedCorrectlyWithMultipleDefaul
'user-Agent' => array('ABC'),
'another-header' => array('value'),
'custom-header' => array('data'),

'Connection' => array('close')
);

$that->assertEquals($expectedHeaders, $request->getHeaders());
Expand Down
53 changes: 49 additions & 4 deletions tests/FunctionalBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public function testReceiveStreamAndExplicitlyCloseConnectionEvenWhenServerKeeps
$socket->close();
}

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

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

// add `Connection: close` request header to disable HTTP keep-alive
$this->browser = $this->browser->withHeader('Connection', 'close');

$response = \React\Async\await($this->browser->get($this->base . 'get'));
assert($response instanceof ResponseInterface);
$this->assertEquals('hello', (string)$response->getBody());
Expand All @@ -579,12 +582,54 @@ public function testRequestWillCreateNewConnectionForSecondRequestByDefaultEvenW
$this->assertEquals('hello', (string)$response->getBody());
}

public function testRequestWithoutConnectionHeaderWillReuseExistingConnectionForSecondRequest()
public function testRequestWithHttp10WillCreateNewConnectionForSecondRequestEvenWhenServerKeepsConnectionOpen()
{
$twice = $this->expectCallableOnce();
$socket = new SocketServer('127.0.0.1:0');
$socket->on('connection', function (\React\Socket\ConnectionInterface $connection) use ($socket, $twice) {
$connection->on('data', function () use ($connection) {
$connection->write("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello");
});

$socket->on('connection', $twice);
$socket->on('connection', function () use ($socket) {
$socket->close();
});
});

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

// use HTTP/1.0 to disable HTTP keep-alive
$this->browser = $this->browser->withProtocolVersion('1.0');

$response = \React\Async\await($this->browser->get($this->base . 'get'));
assert($response instanceof ResponseInterface);
$this->assertEquals('hello', (string)$response->getBody());

$response = \React\Async\await($this->browser->get($this->base . 'get'));
assert($response instanceof ResponseInterface);
$this->assertEquals('hello', (string)$response->getBody());
}

public function testRequestWillReuseExistingConnectionForSecondRequestByDefault()
{
$this->socket->on('connection', $this->expectCallableOnce());

// remove default `Connection: close` request header to enable keep-alive
$this->browser = $this->browser->withoutHeader('Connection');
$response = \React\Async\await($this->browser->get($this->base . 'get'));
assert($response instanceof ResponseInterface);
$this->assertEquals('hello', (string)$response->getBody());

$response = \React\Async\await($this->browser->get($this->base . 'get'));
assert($response instanceof ResponseInterface);
$this->assertEquals('hello', (string)$response->getBody());
}

public function testRequestWithHttp10AndConnectionKeepAliveHeaderWillReuseExistingConnectionForSecondRequest()
{
$this->socket->on('connection', $this->expectCallableOnce());

$this->browser = $this->browser->withProtocolVersion('1.0');
$this->browser = $this->browser->withHeader('Connection', 'keep-alive');

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

0 comments on commit 7189c6a

Please sign in to comment.