-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
HttpBrowserTest.php
214 lines (185 loc) · 8.44 KB
/
HttpBrowserTest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?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\BrowserKit\Tests;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\BrowserKit\HttpBrowser;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class HttpBrowserTest extends AbstractBrowserTest
{
public function getBrowser(array $server = [], History $history = null, CookieJar $cookieJar = null)
{
return new TestHttpClient($server, $history, $cookieJar);
}
/**
* @dataProvider validContentTypes
*/
public function testRequestHeaders(array $requestArguments, array $expectedArguments)
{
$client = $this->createMock(HttpClientInterface::class);
$client
->expects($this->once())
->method('request')
->with(...$expectedArguments)
->willReturn($this->createMock(ResponseInterface::class));
$browser = new HttpBrowser($client);
$browser->request(...$requestArguments);
}
public function validContentTypes()
{
$defaultHeaders = ['user-agent' => 'Symfony BrowserKit', 'host' => 'example.com'];
yield 'GET/HEAD' => [
['GET', 'http://example.com/', ['key' => 'value']],
['GET', 'http://example.com/', ['headers' => $defaultHeaders, 'body' => '', 'max_redirects' => 0]],
];
yield 'empty form' => [
['POST', 'http://example.com/'],
['POST', 'http://example.com/', ['headers' => $defaultHeaders, 'body' => '', 'max_redirects' => 0]],
];
yield 'form' => [
['POST', 'http://example.com/', ['key' => 'value', 'key2' => 'value']],
['POST', 'http://example.com/', ['headers' => $defaultHeaders + ['Content-Type' => 'application/x-www-form-urlencoded'], 'body' => 'key=value&key2=value', 'max_redirects' => 0]],
];
yield 'content' => [
['POST', 'http://example.com/', [], [], [], 'content'],
['POST', 'http://example.com/', ['headers' => $defaultHeaders + ['Content-Type: text/plain; charset=utf-8', 'Content-Transfer-Encoding: 8bit'], 'body' => 'content', 'max_redirects' => 0]],
];
yield 'POST JSON' => [
['POST', 'http://example.com/', [], [], ['CONTENT_TYPE' => 'application/json'], '["content"]'],
['POST', 'http://example.com/', ['headers' => $defaultHeaders + ['content-type' => 'application/json'], 'body' => '["content"]', 'max_redirects' => 0]],
];
}
public function testMultiPartRequestWithSingleFile()
{
$client = $this->createMock(HttpClientInterface::class);
$client
->expects($this->once())
->method('request')
->with('POST', 'http://example.com/', $this->callback(function ($options) {
$this->assertStringContainsString('Content-Type: multipart/form-data', implode('', $options['headers']));
$this->assertInstanceOf('\Generator', $options['body']);
$this->assertStringContainsString('my_file', implode('', iterator_to_array($options['body'])));
return true;
}))
->willReturn($this->createMock(ResponseInterface::class));
$browser = new HttpBrowser($client);
$path = tempnam(sys_get_temp_dir(), 'http');
file_put_contents($path, 'my_file');
$browser->request('POST', 'http://example.com/', [], ['file' => ['tmp_name' => $path, 'name' => 'foo']]);
}
public function testMultiPartRequestWithNormalFlatArray()
{
$client = $this->createMock(HttpClientInterface::class);
$this->expectClientToSendRequestWithFiles($client, ['file1_content', 'file2_content']);
$browser = new HttpBrowser($client);
$browser->request('POST', 'http://example.com/', [], [
'file1' => $this->getUploadedFile('file1'),
'file2' => $this->getUploadedFile('file2'),
]);
}
public function testMultiPartRequestWithNormalNestedArray()
{
$client = $this->createMock(HttpClientInterface::class);
$this->expectClientToSendRequestWithFiles($client, ['file1_content', 'file2_content']);
$browser = new HttpBrowser($client);
$browser->request('POST', 'http://example.com/', [], [
'level1' => [
'level2' => [
'file1' => $this->getUploadedFile('file1'),
'file2' => $this->getUploadedFile('file2'),
],
],
]);
}
public function testMultiPartRequestWithBracketedArray()
{
$client = $this->createMock(HttpClientInterface::class);
$this->expectClientToSendRequestWithFiles($client, ['file1_content', 'file2_content']);
$browser = new HttpBrowser($client);
$browser->request('POST', 'http://example.com/', [], [
'form[file1]' => $this->getUploadedFile('file1'),
'form[file2]' => $this->getUploadedFile('file2'),
]);
}
public function testMultiPartRequestWithInvalidItem()
{
$client = $this->createMock(HttpClientInterface::class);
$this->expectClientToSendRequestWithFiles($client, ['file1_content']);
$browser = new HttpBrowser($client);
$browser->request('POST', 'http://example.com/', [], [
'file1' => $this->getUploadedFile('file1'),
'file2' => 'INVALID',
]);
}
public function testMultiPartRequestWithAdditionalParameters()
{
$client = $this->createMock(HttpClientInterface::class);
$this->expectClientToSendRequestWithFiles($client, ['file1_content', 'baz']);
$browser = new HttpBrowser($client);
$browser->request('POST', 'http://example.com/', ['bar' => 'baz'], [
'file1' => $this->getUploadedFile('file1'),
]);
}
public function testMultiPartRequestWithAdditionalParametersOfTheSameName()
{
$client = $this->createMock(HttpClientInterface::class);
$this->expectClientToNotSendRequestWithFiles($client, ['baz']);
$browser = new HttpBrowser($client);
$browser->request('POST', 'http://example.com/', ['file1' => 'baz'], [
'file1' => $this->getUploadedFile('file1'),
]);
}
private function uploadFile(string $data): string
{
$path = tempnam(sys_get_temp_dir(), 'http');
file_put_contents($path, $data);
return $path;
}
private function getUploadedFile(string $name): array
{
return [
'tmp_name' => $this->uploadFile($name.'_content'),
'name' => $name.'_name',
];
}
protected function expectClientToSendRequestWithFiles(HttpClientInterface $client, $fileContents)
{
$client
->expects($this->once())
->method('request')
->with('POST', 'http://example.com/', $this->callback(function ($options) use ($fileContents) {
$this->assertStringContainsString('Content-Type: multipart/form-data', implode('', $options['headers']));
$this->assertInstanceOf('\Generator', $options['body']);
$body = implode('', iterator_to_array($options['body'], false));
foreach ($fileContents as $content) {
$this->assertStringContainsString($content, $body);
}
return true;
}))
->willReturn($this->createMock(ResponseInterface::class));
}
protected function expectClientToNotSendRequestWithFiles(HttpClientInterface $client, $fileContents)
{
$client
->expects($this->once())
->method('request')
->with('POST', 'http://example.com/', $this->callback(function ($options) use ($fileContents) {
$this->assertStringContainsString('Content-Type: multipart/form-data', implode('', $options['headers']));
$this->assertInstanceOf('\Generator', $options['body']);
$body = implode('', iterator_to_array($options['body'], false));
foreach ($fileContents as $content) {
$this->assertStringNotContainsString($content, $body);
}
return true;
}))
->willReturn($this->createMock(ResponseInterface::class));
}
}