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

Enable HTTP keep-alive by default for HTTP client #495

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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