-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
MockHttpClientTest.php
173 lines (139 loc) · 6.68 KB
/
MockHttpClientTest.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?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 Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\ChunkInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class MockHttpClientTest extends HttpClientTestCase
{
protected function getHttpClient(string $testCase): HttpClientInterface
{
$responses = [];
$headers = [
'Host: localhost:8057',
'Content-Type: application/json',
];
$body = '{
"SERVER_PROTOCOL": "HTTP/1.1",
"SERVER_NAME": "127.0.0.1",
"REQUEST_URI": "/",
"REQUEST_METHOD": "GET",
"HTTP_ACCEPT": "*/*",
"HTTP_FOO": "baR",
"HTTP_HOST": "localhost:8057"
}';
$client = new NativeHttpClient();
switch ($testCase) {
default:
return new MockHttpClient(function (string $method, string $url, array $options) use ($client) {
try {
// force the request to be completed so that we don't test side effects of the transport
$response = $client->request($method, $url, ['buffer' => false] + $options);
$content = $response->getContent(false);
return new MockResponse($content, $response->getInfo());
} catch (\Throwable $e) {
$this->fail($e->getMessage());
}
});
case 'testUnsupportedOption':
$this->markTestSkipped('MockHttpClient accepts any options by default');
break;
case 'testChunkedEncoding':
$this->markTestSkipped("MockHttpClient doesn't dechunk");
break;
case 'testGzipBroken':
$this->markTestSkipped("MockHttpClient doesn't unzip");
break;
case 'testDestruct':
$this->markTestSkipped("MockHttpClient doesn't timeout on destruct");
break;
case 'testGetRequest':
array_unshift($headers, 'HTTP/1.1 200 OK');
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
$headers = [
'Host: localhost:8057',
'Content-Length: 1000',
'Content-Type: application/json',
];
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
break;
case 'testDnsError':
$mock = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mock->expects($this->any())
->method('getStatusCode')
->willThrowException(new TransportException('DSN error'));
$mock->expects($this->any())
->method('getInfo')
->willReturn([]);
$responses[] = $mock;
$responses[] = $mock;
break;
case 'testBadRequestBody':
case 'testOnProgressCancel':
case 'testOnProgressError':
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
break;
case 'testTimeoutOnAccess':
$mock = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mock->expects($this->any())
->method('getHeaders')
->willThrowException(new TransportException('Timeout'));
$responses[] = $mock;
break;
case 'testAcceptHeader':
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
$responses[] = new MockResponse(str_replace('*/*', 'foo/bar', $body), ['response_headers' => $headers]);
$responses[] = new MockResponse(str_replace('"HTTP_ACCEPT": "*/*",', '', $body), ['response_headers' => $headers]);
break;
case 'testResolve':
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
$responses[] = new MockResponse((function () { throw new \Exception('Fake connection timeout'); yield ''; })(), ['response_headers' => $headers]);
break;
case 'testTimeoutOnStream':
case 'testUncheckedTimeoutThrows':
$body = ['<1>', '', '<2>'];
$responses[] = new MockResponse($body, ['response_headers' => $headers]);
break;
case 'testInformationalResponseStream':
$client = $this->createMock(HttpClientInterface::class);
$response = new MockResponse('Here the body', ['response_headers' => [
'HTTP/1.1 103 ',
'Link: </style.css>; rel=preload; as=style',
'HTTP/1.1 200 ',
'Date: foo',
'Content-Length: 13',
]]);
$client->method('request')->willReturn($response);
$client->method('stream')->willReturn(new ResponseStream((function () use ($response) {
$chunk = $this->createMock(ChunkInterface::class);
$chunk->method('getInformationalStatus')
->willReturn([103, ['link' => ['</style.css>; rel=preload; as=style', '</script.js>; rel=preload; as=script']]]);
yield $response => $chunk;
$chunk = $this->createMock(ChunkInterface::class);
$chunk->method('isFirst')->willReturn(true);
yield $response => $chunk;
$chunk = $this->createMock(ChunkInterface::class);
$chunk->method('getContent')->willReturn('Here the body');
yield $response => $chunk;
$chunk = $this->createMock(ChunkInterface::class);
$chunk->method('isLast')->willReturn(true);
yield $response => $chunk;
})()));
return $client;
}
return new MockHttpClient($responses);
}
}