1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChadicusTest\Slim\OAuth2\Http; |
4
|
|
|
|
5
|
|
|
use Chadicus\Slim\OAuth2\Http\RequestBridge; |
6
|
|
|
use Zend\Diactoros\ServerRequest; |
7
|
|
|
use Zend\Diactoros\UploadedFile; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Unit tests for the \Chadicus\Slim\OAuth2\Http\RequestBridge class. |
11
|
|
|
* |
12
|
|
|
* @coversDefaultClass \Chadicus\Slim\OAuth2\Http\RequestBridge |
13
|
|
|
* @covers ::<private> |
14
|
|
|
*/ |
15
|
|
|
final class RequestBridgeTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Verify basic behavior of toOAuth2() |
19
|
|
|
* |
20
|
|
|
* @test |
21
|
|
|
* @covers ::toOAuth2 |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public function toOAuth2() |
26
|
|
|
{ |
27
|
|
|
$uri = 'https://example.com/foo/bar'; |
28
|
|
|
$headers = ['Host' => ['example.com'], 'Accept' => ['application/json', 'text/json']]; |
29
|
|
|
$cookies = ['PHPSESSID' => uniqid()]; |
30
|
|
|
$server = ['SCRIPT_NAME' => __FILE__, 'SCRIPT_FILENAME' => __FILE__]; |
31
|
|
|
$json = json_encode(['foo' => 'bar', 'abc' => '123']); |
32
|
|
|
|
33
|
|
|
$stream = fopen('php://memory', 'r+'); |
34
|
|
|
fwrite($stream, $json); |
35
|
|
|
rewind($stream); |
36
|
|
|
|
37
|
|
|
$files = [ |
38
|
|
|
'foo' => new UploadedFile( |
39
|
|
|
__FILE__, |
40
|
|
|
100, |
41
|
|
|
UPLOAD_ERR_OK, |
42
|
|
|
'foo.txt', |
43
|
|
|
'text/plain' |
44
|
|
|
), |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$psr7Request = new ServerRequest($server, $files, $uri, 'PATCH', $stream, $headers, $cookies, ['baz' => 'bat']); |
48
|
|
|
|
49
|
|
|
$oauth2Request = RequestBridge::toOauth2($psr7Request); |
50
|
|
|
|
51
|
|
|
$this->assertInstanceOf('\OAuth2\Request', $oauth2Request); |
52
|
|
|
$this->assertSame('bat', $oauth2Request->query('baz')); |
53
|
|
|
$this->assertSame('example.com', $oauth2Request->headers('Host')); |
54
|
|
|
$this->assertSame('application/json, text/json', $oauth2Request->headers('Accept')); |
55
|
|
|
$this->assertSame($cookies, $oauth2Request->cookies); |
56
|
|
|
|
57
|
|
|
$this->assertSame(__FILE__, $oauth2Request->server('SCRIPT_NAME')); |
58
|
|
|
|
59
|
|
|
$this->assertSame($json, $oauth2Request->getContent()); |
60
|
|
|
|
61
|
|
|
$this->assertSame( |
62
|
|
|
[ |
63
|
|
|
'foo' => [ |
64
|
|
|
'name' => 'foo.txt', |
65
|
|
|
'type' => 'text/plain', |
66
|
|
|
'size' => 100, |
67
|
|
|
'tmp_name' => __FILE__, |
68
|
|
|
'error' => UPLOAD_ERR_OK, |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
$oauth2Request->files |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Verify behavior of toOAuth2() with application/json content type |
77
|
|
|
* |
78
|
|
|
* @test |
79
|
|
|
* @covers ::toOAuth2 |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public function toOAuth2JsonContentType() |
84
|
|
|
{ |
85
|
|
|
$uri = 'https://example.com/foos'; |
86
|
|
|
|
87
|
|
|
$data = ['foo' => 'bar', 'abc' => '123']; |
88
|
|
|
|
89
|
|
|
$json = json_encode($data); |
90
|
|
|
$stream = fopen('php://memory', 'r+'); |
91
|
|
|
fwrite($stream, $json); |
92
|
|
|
rewind($stream); |
93
|
|
|
|
94
|
|
|
$headers = [ |
95
|
|
|
'Content-Type' => ['application/json'], |
96
|
|
|
'Content-Length' => [strlen($json)], |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$psr7Request = new ServerRequest([], [], $uri, 'POST', $stream, $headers, [], [], $data); |
100
|
|
|
|
101
|
|
|
$oauth2Request = RequestBridge::toOAuth2($psr7Request); |
102
|
|
|
|
103
|
|
|
$this->assertSame((string)strlen($json), $oauth2Request->headers('Content-Length')); |
104
|
|
|
$this->assertSame('application/json', $oauth2Request->headers('Content-Type')); |
105
|
|
|
$this->assertSame('bar', $oauth2Request->request('foo')); |
106
|
|
|
$this->assertSame('123', $oauth2Request->request('abc')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Verify behavior of replacing bad header key names |
111
|
|
|
* |
112
|
|
|
* @test |
113
|
|
|
* @covers ::toOAuth2 |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function toOAuth2HeaderKeyNames() |
118
|
|
|
{ |
119
|
|
|
$uri = 'https://example.com/foos'; |
120
|
|
|
|
121
|
|
|
$headers = [ |
122
|
|
|
'Php-Auth-User' => ['test_client_id'], |
123
|
|
|
'Php-Auth-Pw' => ['test_secret'], |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
$psr7Request = new ServerRequest([], [], $uri, 'GET', 'php://input', $headers); |
127
|
|
|
|
128
|
|
|
$oauth2Request = RequestBridge::toOAuth2($psr7Request); |
129
|
|
|
|
130
|
|
|
$this->assertSame('test_client_id', $oauth2Request->headers('PHP_AUTH_USER')); |
131
|
|
|
$this->assertSame('test_secret', $oauth2Request->headers('PHP_AUTH_PW')); |
132
|
|
|
$this->assertNull($oauth2Request->headers('Php-Auth-User')); |
133
|
|
|
$this->assertNull($oauth2Request->headers('Php-Auth-Pw')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Verify behavior of PSR-7 request with Authorization header. |
138
|
|
|
* |
139
|
|
|
* @test |
140
|
|
|
* @covers ::toOAuth2 |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function toOAuth2WithAuthorization() |
145
|
|
|
{ |
146
|
|
|
$uri = 'https://example.com/foos'; |
147
|
|
|
|
148
|
|
|
$headers = ['HTTP_AUTHORIZATION' => ['Bearer abc123']]; |
149
|
|
|
|
150
|
|
|
$psr7Request = new ServerRequest([], [], $uri, 'GET', 'php://input', $headers); |
151
|
|
|
|
152
|
|
|
$oauth2Request = RequestBridge::toOAuth2($psr7Request); |
153
|
|
|
|
154
|
|
|
$this->assertSame('Bearer abc123', $oauth2Request->headers('AUTHORIZATION')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Verify that steam contents of a passed request is preserved (read and rewound). |
159
|
|
|
* |
160
|
|
|
* @test |
161
|
|
|
* @covers ::toOAuth2 |
162
|
|
|
* |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
public function toOAuth2BodyContentsOfRequestPreserved() |
166
|
|
|
{ |
167
|
|
|
$uri = 'https://example.com/foos'; |
168
|
|
|
|
169
|
|
|
$temp = tmpfile(); |
170
|
|
|
fwrite($temp, 'foo'); |
171
|
|
|
rewind($temp); |
172
|
|
|
|
173
|
|
|
$psr7Request = new ServerRequest([], [], $uri, 'POST', $temp); |
174
|
|
|
|
175
|
|
|
$oauth2Request = RequestBridge::toOAuth2($psr7Request); |
176
|
|
|
|
177
|
|
|
$this->assertSame('foo', $psr7Request->getBody()->getContents()); |
178
|
|
|
$this->assertSame('foo', $oauth2Request->getContent()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @test |
183
|
|
|
* @covers ::toOAuth2 |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function toOAuth2WithMultipleFiles() |
188
|
|
|
{ |
189
|
|
|
$files = [ |
190
|
|
|
'multi' => [ |
191
|
|
|
new UploadedFile( |
192
|
|
|
__FILE__, |
193
|
|
|
100, |
194
|
|
|
UPLOAD_ERR_OK, |
195
|
|
|
'foo1.txt', |
196
|
|
|
'text/plain' |
197
|
|
|
), |
198
|
|
|
new UploadedFile( |
199
|
|
|
__FILE__, |
200
|
|
|
100, |
201
|
|
|
UPLOAD_ERR_OK, |
202
|
|
|
'foo2.txt', |
203
|
|
|
'text/plain' |
204
|
|
|
), |
205
|
|
|
], |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
$psr7Request = (new ServerRequest())->withUploadedFiles($files); |
209
|
|
|
$oauth2Request = RequestBridge::toOauth2($psr7Request); |
210
|
|
|
|
211
|
|
|
$this->assertSame( |
212
|
|
|
[ |
213
|
|
|
'multi' => [ |
214
|
|
|
[ |
215
|
|
|
'name' => 'foo1.txt', |
216
|
|
|
'type' => 'text/plain', |
217
|
|
|
'size' => 100, |
218
|
|
|
'tmp_name' => __FILE__, |
219
|
|
|
'error' => UPLOAD_ERR_OK, |
220
|
|
|
], |
221
|
|
|
[ |
222
|
|
|
'name' => 'foo2.txt', |
223
|
|
|
'type' => 'text/plain', |
224
|
|
|
'size' => 100, |
225
|
|
|
'tmp_name' => __FILE__, |
226
|
|
|
'error' => UPLOAD_ERR_OK, |
227
|
|
|
], |
228
|
|
|
], |
229
|
|
|
], |
230
|
|
|
$oauth2Request->files |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Verify that sets request method. |
236
|
|
|
* |
237
|
|
|
* @test |
238
|
|
|
* @covers ::toOAuth2 |
239
|
|
|
* |
240
|
|
|
* @return void |
241
|
|
|
*/ |
242
|
|
|
public function toOAuth2RequestMethodPreserved() |
243
|
|
|
{ |
244
|
|
|
$uri = 'https://example.com/foos'; |
245
|
|
|
|
246
|
|
|
$psr7Request = new ServerRequest([], [], $uri, 'POST', 'php://input'); |
247
|
|
|
|
248
|
|
|
$oauth2Request = RequestBridge::toOAuth2($psr7Request); |
249
|
|
|
|
250
|
|
|
$this->assertSame('POST', $psr7Request->getMethod()); |
251
|
|
|
$this->assertSame('POST', $oauth2Request->server('REQUEST_METHOD')); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|