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

Set a default ssl.peer_name context in StreamHandler #2988

Merged
merged 2 commits into from Mar 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Handler/StreamHandler.php
Expand Up @@ -377,6 +377,9 @@ private function getDefaultContext(RequestInterface $request): array
'ignore_errors' => true,
'follow_location' => 0,
],
'ssl' => [
'peer_name' => $request->getUri()->getHost(),
],
];

$body = (string) $request->getBody();
Expand Down
71 changes: 71 additions & 0 deletions tests/Handler/Network/StreamHandlerTest.php
@@ -0,0 +1,71 @@
<?php

namespace GuzzleHttp\Test\Handler\Network;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\StreamHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\RequestOptions;
use PHPUnit\Framework\TestCase;

/**
* @covers \GuzzleHttp\Handler\StreamHandler
*/
class StreamHandlerTest extends TestCase
{
public function setUp(): void
{
if (!($_SERVER['GUZZLE_TEST_ALLOW_NETWORK'] ?? false)) {
self::markTestSkipped("This test requires the GUZZLE_TEST_ALLOW_NETWORK environment variable.");
}
}

public function testSslRequestWorks()
{
$handler = new StreamHandler();

$response = $handler(
new Request('GET', 'https://www.example.com/'),
[
RequestOptions::STREAM => true,
]
)->wait();

self::assertSame(200, $response->getStatusCode());
self::assertStringContainsString('<h1>Example Domain</h1>', (string)$response->getBody());
}

public function testSslRequestWorksWithForceIpResolve()
{
$handler = new StreamHandler();

$response = $handler(
new Request('GET', 'https://www.example.com/'),
[
RequestOptions::STREAM => true,
'force_ip_resolve' => 'v4',
]
)->wait();

self::assertSame(200, $response->getStatusCode());
self::assertStringContainsString('<h1>Example Domain</h1>', (string)$response->getBody());
}

public function testSslRequestWorksWithForceIpResolveAfterRedirect()
{
$client = new Client(['handler' => HandlerStack::create(new StreamHandler())]);

$response = $client->send(
// Redirects to https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsrun.
new Request('GET', 'https://git.io/JvXDl'),
[
RequestOptions::STREAM => true,
'force_ip_resolve' => 'v4',
]
);

self::assertSame(200, $response->getStatusCode());
self::assertStringContainsString('jobsjob_idstepsrun', (string)$response->getBody());
}
}