-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFunctionTest.php
More file actions
204 lines (164 loc) · 7.32 KB
/
FunctionTest.php
File metadata and controls
204 lines (164 loc) · 7.32 KB
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
<?php
namespace tests\Http\Message\MultipartStream;
use Http\Message\MultipartStream\CustomMimetypeHelper;
use Http\Message\MultipartStream\MultipartStreamBuilder;
use Nyholm\Psr7\Factory\HttplugFactory;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Stream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class FunctionTest extends TestCase
{
public function testSupportStreams()
{
$body = 'stream contents';
$builder = new MultipartStreamBuilder();
$builder->addResource('foobar', $this->createStream($body));
$multipartStream = (string) $builder->build();
$this->assertTrue(false !== strpos($multipartStream, $body));
}
public function testSupportResources()
{
$resource = fopen(__DIR__.'/Resources/httplug.png', 'r');
$builder = new MultipartStreamBuilder();
$builder->addResource('image', $resource);
$multipartStream = (string) $builder->build();
$this->assertTrue(false !== strpos($multipartStream, 'Content-Disposition: form-data; name="image"; filename="httplug.png"'));
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: image/png'));
}
public function testSupportURIResources()
{
$url = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/1.x/tests/Resources/httplug.png';
$resource = fopen($url, 'r');
$builder = new MultipartStreamBuilder();
$builder->addResource('image', $resource);
$multipartStream = (string) $builder->build();
$this->assertTrue(false !== strpos($multipartStream, 'Content-Disposition: form-data; name="image"; filename="httplug.png"'));
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: image/png'));
$urlContents = file_get_contents($url);
$this->assertStringContainsString($urlContents, $multipartStream);
}
public function testResourceFilenameIsNotLocaleAware()
{
// Get current locale
$originalLocale = setlocale(LC_ALL, "0");
// Set locale to something strange.
setlocale(LC_ALL, 'C');
$resource = fopen(__DIR__.'/Resources/httplug.png', 'r');
$builder = new MultipartStreamBuilder();
$builder->addResource('image', $resource, ['filename'=> 'äa.png']);
$multipartStream = (string) $builder->build();
$this->assertTrue(0 < preg_match('|filename="([^"]*?)"|si', $multipartStream, $matches), 'Could not find any filename in output.');
$this->assertEquals('äa.png', $matches[1]);
// Reset the locale
setlocale(LC_ALL, $originalLocale);
}
public function testHeaders()
{
$builder = new MultipartStreamBuilder();
$builder->addResource('foobar', 'stream contents', ['headers' => ['Content-Type' => 'html/image', 'content-length' => '4711', 'CONTENT-DISPOSITION' => 'none']]);
$multipartStream = (string) $builder->build();
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: html/image'));
$this->assertTrue(false !== strpos($multipartStream, 'content-length: 4711'));
$this->assertTrue(false !== strpos($multipartStream, 'CONTENT-DISPOSITION: none'));
// Make sure we do not add extra headers with a different case
$this->assertTrue(false === strpos($multipartStream, 'Content-Disposition:'));
}
/**
* Comply with RFC 7578 section 4.8
*/
public function testShouldNotContainContentLength()
{
$builder = new MultipartStreamBuilder();
$builder->addResource('foobar', 'stream contents');
$multipartStream = (string) $builder->build();
$this->assertTrue(false === strpos($multipartStream, 'Content-Length:'));
}
public function testFormName()
{
$builder = new MultipartStreamBuilder();
$builder->addResource('a-formname', 'string');
$multipartStream = (string) $builder->build();
$this->assertTrue(false !== strpos($multipartStream, 'Content-Disposition: form-data; name="a-formname"'));
}
public function testAddResourceWithSameName()
{
$builder = new MultipartStreamBuilder();
$builder->addResource('name', 'foo1234567890foo');
$builder->addResource('name', 'bar1234567890bar');
$multipartStream = (string) $builder->build();
$this->assertTrue(false !== strpos($multipartStream, 'bar1234567890bar'));
$this->assertTrue(false !== strpos($multipartStream, 'foo1234567890foo'), 'Using same name must not overwrite');
}
public function testBoundary()
{
$boundary = 'SpecialBoundary';
$builder = new MultipartStreamBuilder();
$builder->addResource('content0', 'string');
$builder->setBoundary($boundary);
$multipartStream = (string) $builder->build();
$this->assertEquals(2, substr_count($multipartStream, $boundary));
$builder->addResource('content1', 'string');
$builder->addResource('content2', 'string');
$builder->addResource('content3', 'string');
$multipartStream = (string) $builder->build();
$this->assertEquals(5, substr_count($multipartStream, $boundary));
}
public function testReset()
{
$boundary = 'SpecialBoundary';
$builder = new MultipartStreamBuilder();
$builder->addResource('content0', 'foobar');
$builder->setBoundary($boundary);
$builder->reset();
$multipartStream = (string) $builder->build();
$this->assertStringNotContainsString('foobar', $multipartStream, 'Stream should not have any data after reset()');
$this->assertNotEquals($boundary, $builder->getBoundary(), 'Stream should have a new boundary after reset()');
$this->assertNotEmpty($builder->getBoundary());
}
public function testThrowsExceptionIfNotStreamCompatible()
{
$builder = new MultipartStreamBuilder();
$this->expectException(\InvalidArgumentException::class);
$builder->addResource('foo', []);
}
public function testThrowsExceptionInConstructor()
{
$this->expectException(\LogicException::class);
new MultipartStreamBuilder(new CustomMimetypeHelper());
}
/**
* @dataProvider getStreamFactories
*/
#[DataProvider('getStreamFactories')]
public function testSupportDifferentFactories($factory)
{
$resource = fopen(__DIR__.'/Resources/httplug.png', 'r');
$builder = new MultipartStreamBuilder($factory);
$builder->addResource('image', $resource);
$multipartStream = (string) $builder->build();
$this->assertTrue(false !== strpos($multipartStream, 'Content-Disposition: form-data; name="image"; filename="httplug.png"'));
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: image/png'));
}
public static function getStreamFactories()
{
yield 'Httplug Stream Factory' => [new HttplugFactory()];
yield 'PSR-17 Stream Factory' => [new Psr17Factory()];
yield 'Null Stream Factory' => [null];
}
/**
* @param string $body
*
* @return StreamInterface
*/
private function createStream($body)
{
$stream = Stream::create($body);
$stream->rewind();
return $stream;
}
}