forked from php-http/psr7-integration-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestIntegrationTest.php
More file actions
211 lines (176 loc) · 7.09 KB
/
RequestIntegrationTest.php
File metadata and controls
211 lines (176 loc) · 7.09 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
205
206
207
208
209
210
211
<?php
namespace Http\Psr7Test;
use InvalidArgumentException;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Throwable;
use TypeError;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
abstract class RequestIntegrationTest extends BaseTest
{
use MessageTrait;
/**
* @var array with functionName => reason
*/
protected $skippedTests = [];
/**
* @var RequestInterface
*/
protected $request;
/**
* @return RequestInterface that is used in the tests
*/
abstract public function createSubject();
protected function setUp(): void
{
$this->request = $this->createSubject();
}
protected function getMessage()
{
return $this->request;
}
public function testRequestTarget()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$original = clone $this->request;
$this->assertEquals('/', $this->request->getRequestTarget());
$request = $this->request->withRequestTarget('*');
$this->assertNotSameObject($this->request, $request);
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
$this->assertEquals('*', $request->getRequestTarget());
}
public function testMethod()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$this->assertEquals('GET', $this->request->getMethod());
$original = clone $this->request;
$request = $this->request->withMethod('POST');
$this->assertNotSameObject($this->request, $request);
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
$this->assertEquals('POST', $request->getMethod());
}
public function testMethodIsCaseSensitive()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$request = $this->request->withMethod('head');
$this->assertEquals('head', $request->getMethod());
}
public function testMethodIsExtendable()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$request = $this->request->withMethod('CUSTOM');
$this->assertEquals('CUSTOM', $request->getMethod());
}
/**
* @dataProvider getInvalidMethods
*/
#[DataProvider('getInvalidMethods')]
public function testMethodWithInvalidArguments($method)
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
try {
$this->request->withMethod($method);
$this->fail('withMethod() should have raised exception on invalid argument');
} catch (AssertionFailedError $e) {
// invalid argument not caught
throw $e;
} catch (InvalidArgumentException|TypeError $e) {
// valid
$this->assertTrue($e instanceof Throwable);
} catch (Throwable $e) {
// invalid
$this->fail(sprintf(
'Unexpected exception (%s) thrown from withMethod(); expected TypeError or InvalidArgumentException',
gettype($e)
));
}
}
public static function getInvalidMethods()
{
return [
'null' => [null],
'false' => [false],
'array' => [['foo']],
'object' => [new \stdClass()],
];
}
public function testUri()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$original = clone $this->request;
$this->assertInstanceOf(UriInterface::class, $this->request->getUri());
$uri = $this->buildUri('http://www.foo.com/bar');
$request = $this->request->withUri($uri);
$this->assertNotSameObject($this->request, $request);
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
$this->assertEquals('www.foo.com', $request->getHeaderLine('host'));
$this->assertInstanceOf(UriInterface::class, $request->getUri());
$this->assertEquals('http://www.foo.com/bar', (string) $request->getUri());
$request = $request->withUri($this->buildUri('/foobar'));
$this->assertNotSameObject($this->request, $request);
$this->assertEquals($this->request, $original, 'Request object MUST not be mutated');
$this->assertEquals('www.foo.com', $request->getHeaderLine('host'), 'If the URI does not contain a host component, any pre-existing Host header MUST be carried over to the returned request.');
$this->assertEquals('/foobar', (string) $request->getUri());
}
public function testUriPreserveHost_NoHost_Host()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$request = $this->request->withUri($this->buildUri('http://www.foo.com/bar'), true);
$this->assertEquals('www.foo.com', $request->getHeaderLine('host'));
}
public function testUriPreserveHost_NoHost_NoHost()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$host = $this->request->getHeaderLine('host');
$request = $this->request->withUri($this->buildUri('/bar'), true);
$this->assertEquals($host, $request->getHeaderLine('host'));
}
public function testUriPreserveHost_Host_Host()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$request = $this->request->withUri($this->buildUri('http://www.foo.com/bar'));
$host = $request->getHeaderLine('host');
$request2 = $request->withUri($this->buildUri('http://www.bar.com/foo'), true);
$this->assertEquals($host, $request2->getHeaderLine('host'));
}
/**
* Tests that getRequestTarget(), when using the default behavior of
* displaying the origin-form, normalizes multiple leading slashes in the
* path to a single slash. This is done to prevent URL poisoning and/or XSS
* issues.
*
* @see UriIntegrationTest::testGetPathNormalizesMultipleLeadingSlashesToSingleSlashToPreventXSS
*/
public function testGetRequestTargetInOriginFormNormalizesUriWithMultipleLeadingSlashesInPath()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
$url = 'http://example.org//valid///path';
$request = $this->request->withUri($this->buildUri($url));
$requestTarget = $request->getRequestTarget();
$this->assertSame('/valid///path', $requestTarget);
}
}