/
ScopingHttpClientTest.php
103 lines (83 loc) · 4.11 KB
/
ScopingHttpClientTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\ScopingHttpClient;
class ScopingHttpClientTest extends TestCase
{
public function testRelativeUrl()
{
$mockClient = new MockHttpClient();
$client = new ScopingHttpClient($mockClient, []);
$this->expectException(InvalidArgumentException::class);
$client->request('GET', '/foo');
}
public function testRelativeUrlWithDefaultRegexp()
{
$mockClient = new MockHttpClient();
$client = new ScopingHttpClient($mockClient, ['.*' => ['base_uri' => 'http://example.com']], '.*');
$this->assertSame('http://example.com/foo', $client->request('GET', '/foo')->getInfo('url'));
}
/**
* @dataProvider provideMatchingUrls
*/
public function testMatchingUrls(string $regexp, string $url, array $options)
{
$mockClient = new MockHttpClient();
$client = new ScopingHttpClient($mockClient, $options);
$response = $client->request('GET', $url);
$reuestedOptions = $response->getRequestOptions();
$this->assertEquals($reuestedOptions['case'], $options[$regexp]['case']);
}
public function provideMatchingUrls()
{
$defaultOptions = [
'.*/foo-bar' => ['case' => 1],
'.*' => ['case' => 2],
];
yield ['regexp' => '.*/foo-bar', 'url' => 'http://example.com/foo-bar', 'default_options' => $defaultOptions];
yield ['regexp' => '.*', 'url' => 'http://example.com/bar-foo', 'default_options' => $defaultOptions];
yield ['regexp' => '.*', 'url' => 'http://example.com/foobar', 'default_options' => $defaultOptions];
}
public function testMatchingUrlsAndOptions()
{
$defaultOptions = [
'.*/foo-bar' => ['headers' => ['x-app' => 'unit-test-foo-bar']],
'.*' => ['headers' => ['content-type' => 'text/html']],
];
$mockClient = new MockHttpClient();
$client = new ScopingHttpClient($mockClient, $defaultOptions);
$response = $client->request('GET', 'http://example.com/foo-bar', ['json' => ['url' => 'http://example.com']]);
$requestOptions = $response->getRequestOptions();
$this->assertEquals($requestOptions['headers']['content-type'][0], 'application/json');
$requestJson = json_decode($requestOptions['body'], true);
$this->assertEquals($requestJson['url'], 'http://example.com');
$this->assertEquals($requestOptions['headers']['x-app'][0], $defaultOptions['.*/foo-bar']['headers']['x-app']);
$response = $client->request('GET', 'http://example.com/bar-foo', ['headers' => ['x-app' => 'unit-test']]);
$requestOptions = $response->getRequestOptions();
$this->assertEquals($requestOptions['headers']['x-app'][0], 'unit-test');
$this->assertEquals($requestOptions['headers']['content-type'][0], 'text/html');
$response = $client->request('GET', 'http://example.com/foobar-foo', ['headers' => ['x-app' => 'unit-test']]);
$requestOptions = $response->getRequestOptions();
$this->assertEquals($requestOptions['headers']['x-app'][0], 'unit-test');
$this->assertEquals($requestOptions['headers']['content-type'][0], 'text/html');
}
public function testForBaseUri()
{
$client = ScopingHttpClient::forBaseUri(new MockHttpClient(), 'http://example.com/foo');
$response = $client->request('GET', '/bar');
$this->assertSame('http://example.com/foo', implode('', $response->getRequestOptions()['base_uri']));
$this->assertSame('http://example.com/bar', $response->getInfo('url'));
$response = $client->request('GET', 'http://foo.bar/');
$this->assertNull($response->getRequestOptions()['base_uri']);
}
}