1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-phantomjs. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace JonnyW\PhantomJs\Tests\Unit\Http; |
10
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Http\Response; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* PHP PhantomJs |
15
|
|
|
* |
16
|
|
|
* @author Jon Wenmoth <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ResponseTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
22
|
|
|
/** ++++++++++++++ TESTS ++++++++++++++ **/ |
23
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test status can be imported. |
27
|
|
|
* |
28
|
|
|
* @access public |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function testStatusCanBeImported() |
32
|
|
|
{ |
33
|
|
|
$data = array( |
34
|
|
|
'status' => 200 |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$response = $this->getResponse(); |
38
|
|
|
$response->import($data); |
39
|
|
|
|
40
|
|
|
$this->assertEquals(200, $response->getStatus()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test content can be imported |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function testContentCanBeImported() |
50
|
|
|
{ |
51
|
|
|
$data = array( |
52
|
|
|
'content' => 'Test content' |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$response = $this->getResponse(); |
56
|
|
|
$response->import($data); |
57
|
|
|
|
58
|
|
|
$this->assertEquals('Test content', $response->getContent()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Test content type can be imported. |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function testContentTypeCanBeImported() |
68
|
|
|
{ |
69
|
|
|
$data = array( |
70
|
|
|
'contentType' => 'text/html' |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$response = $this->getResponse(); |
74
|
|
|
$response->import($data); |
75
|
|
|
|
76
|
|
|
$this->assertEquals('text/html', $response->getContentType()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Test URL can be imported. |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function testUrlCanBeImported() |
86
|
|
|
{ |
87
|
|
|
$data = array( |
88
|
|
|
'url' => 'http://test.com' |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$response = $this->getResponse(); |
92
|
|
|
$response->import($data); |
93
|
|
|
|
94
|
|
|
$this->assertEquals('http://test.com', $response->getUrl()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Test redirect URL can be imported. |
99
|
|
|
* |
100
|
|
|
* @access public |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function testRedirectUrlCanBeImported() |
104
|
|
|
{ |
105
|
|
|
$data = array( |
106
|
|
|
'redirectURL' => 'http://test.com' |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$response = $this->getResponse(); |
110
|
|
|
$response->import($data); |
111
|
|
|
|
112
|
|
|
$this->assertEquals('http://test.com', $response->getRedirectUrl()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Test time can be imported. |
117
|
|
|
* |
118
|
|
|
* @access public |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
public function testTimeCanBeImported() |
122
|
|
|
{ |
123
|
|
|
$data = array( |
124
|
|
|
'time' => 123456789 |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
$response = $this->getResponse(); |
128
|
|
|
$response->import($data); |
129
|
|
|
|
130
|
|
|
$this->assertEquals(123456789, $response->getTime()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Test headers can be imported. |
135
|
|
|
* |
136
|
|
|
* @access public |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function testHeadersCanBeImported() |
140
|
|
|
{ |
141
|
|
|
$headers = array( |
142
|
|
|
array( |
143
|
|
|
'name' => 'Header1', |
144
|
|
|
'value' => 'Test Header 1' |
145
|
|
|
) |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$data = array( |
149
|
|
|
'headers' => $headers |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$response = $this->getResponse(); |
153
|
|
|
$response->import($data); |
154
|
|
|
|
155
|
|
|
$expectedHeaders = array( |
156
|
|
|
$headers[0]['name'] => $headers[0]['value'] |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$this->assertEquals($expectedHeaders, $response->getHeaders()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Test null is returned if header is not set. |
164
|
|
|
* |
165
|
|
|
* @access public |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
public function testNullIsReturnedIfHeaderIsNotSet() |
169
|
|
|
{ |
170
|
|
|
$response = $this->getResponse(); |
171
|
|
|
|
172
|
|
|
$this->assertNull($response->getHeader('invalid_header')); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Test can get header. |
177
|
|
|
* |
178
|
|
|
* @access public |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
|
|
public function testCanGetHeader() |
182
|
|
|
{ |
183
|
|
|
$headers = array( |
184
|
|
|
array( |
185
|
|
|
'name' => 'Header1', |
186
|
|
|
'value' => 'Test Header 1' |
187
|
|
|
) |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$data = array( |
191
|
|
|
'headers' => $headers |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$response = $this->getResponse(); |
195
|
|
|
$response->import($data); |
196
|
|
|
|
197
|
|
|
$this->assertEquals('Test Header 1', $response->getHeader('Header1')); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Test is redirect if status code |
202
|
|
|
* is 300. |
203
|
|
|
* |
204
|
|
|
* @access public |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
public function testIsRedirectIfStatusCodeIs300() |
208
|
|
|
{ |
209
|
|
|
$data = array( |
210
|
|
|
'status' => 300 |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
$response = $this->getResponse(); |
214
|
|
|
$response->import($data); |
215
|
|
|
|
216
|
|
|
$this->assertTrue($response->isRedirect()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Test is redirect if status code |
221
|
|
|
* is 301. |
222
|
|
|
* |
223
|
|
|
* @access public |
224
|
|
|
* @return void |
225
|
|
|
*/ |
226
|
|
|
public function testIsRedirectIfStatusCodeIs301() |
227
|
|
|
{ |
228
|
|
|
$data = array( |
229
|
|
|
'status' => 301 |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
$response = $this->getResponse(); |
233
|
|
|
$response->import($data); |
234
|
|
|
|
235
|
|
|
$this->assertTrue($response->isRedirect()); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Test is redirect if status code |
240
|
|
|
* is 302. |
241
|
|
|
* |
242
|
|
|
* @access public |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
public function testIsRedirectIfStatusCodeIs302() |
246
|
|
|
{ |
247
|
|
|
$data = array( |
248
|
|
|
'status' => 302 |
249
|
|
|
); |
250
|
|
|
|
251
|
|
|
$response = $this->getResponse(); |
252
|
|
|
$response->import($data); |
253
|
|
|
|
254
|
|
|
$this->assertTrue($response->isRedirect()); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Test is redirect if status code |
259
|
|
|
* is 303. |
260
|
|
|
* |
261
|
|
|
* @access public |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
public function testIsRedirectIfStatusCodeIs303() |
265
|
|
|
{ |
266
|
|
|
$data = array( |
267
|
|
|
'status' => 303 |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
$response = $this->getResponse(); |
271
|
|
|
$response->import($data); |
272
|
|
|
|
273
|
|
|
$this->assertTrue($response->isRedirect()); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Test is redirect if status code |
278
|
|
|
* is 304. |
279
|
|
|
* |
280
|
|
|
* @access public |
281
|
|
|
* @return void |
282
|
|
|
*/ |
283
|
|
|
public function testIsRedirectIfStatusCodeIs304() |
284
|
|
|
{ |
285
|
|
|
$data = array( |
286
|
|
|
'status' => 304 |
287
|
|
|
); |
288
|
|
|
|
289
|
|
|
$response = $this->getResponse(); |
290
|
|
|
$response->import($data); |
291
|
|
|
|
292
|
|
|
$this->assertTrue($response->isRedirect()); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Test is redirect if status code |
297
|
|
|
* is 305. |
298
|
|
|
* |
299
|
|
|
* @access public |
300
|
|
|
* @return void |
301
|
|
|
*/ |
302
|
|
|
public function testIsRedirectIfStatusCodeIs305() |
303
|
|
|
{ |
304
|
|
|
$data = array( |
305
|
|
|
'status' => 305 |
306
|
|
|
); |
307
|
|
|
|
308
|
|
|
$response = $this->getResponse(); |
309
|
|
|
$response->import($data); |
310
|
|
|
|
311
|
|
|
$this->assertTrue($response->isRedirect()); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Test is redirect if status code |
316
|
|
|
* is 306. |
317
|
|
|
* |
318
|
|
|
* @access public |
319
|
|
|
* @return void |
320
|
|
|
*/ |
321
|
|
|
public function testIsRedirectIfStatusCodeIs306() |
322
|
|
|
{ |
323
|
|
|
$data = array( |
324
|
|
|
'status' => 306 |
325
|
|
|
); |
326
|
|
|
|
327
|
|
|
$response = $this->getResponse(); |
328
|
|
|
$response->import($data); |
329
|
|
|
|
330
|
|
|
$this->assertTrue($response->isRedirect()); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Test is redirect if status code |
335
|
|
|
* is 307. |
336
|
|
|
* |
337
|
|
|
* @access public |
338
|
|
|
* @return void |
339
|
|
|
*/ |
340
|
|
|
public function testIsRedirectIfStatusCodeIs307() |
341
|
|
|
{ |
342
|
|
|
$data = array( |
343
|
|
|
'status' => 307 |
344
|
|
|
); |
345
|
|
|
|
346
|
|
|
$response = $this->getResponse(); |
347
|
|
|
$response->import($data); |
348
|
|
|
|
349
|
|
|
$this->assertTrue($response->isRedirect()); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Test is not redirect if status code is |
354
|
|
|
* not redirect. |
355
|
|
|
* |
356
|
|
|
* @access public |
357
|
|
|
* @return void |
358
|
|
|
*/ |
359
|
|
|
public function testIsNotRedirectIfStatusCodeIsNotRedirect() |
360
|
|
|
{ |
361
|
|
|
$data = array( |
362
|
|
|
'status' => 401 |
363
|
|
|
); |
364
|
|
|
|
365
|
|
|
$response = $this->getResponse(); |
366
|
|
|
$response->import($data); |
367
|
|
|
|
368
|
|
|
$this->assertFalse($response->isRedirect()); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Test if cookies can be parsed and imported |
373
|
|
|
* |
374
|
|
|
* @access public |
375
|
|
|
* @return void |
376
|
|
|
*/ |
377
|
|
|
public function testCookiesCanBeImported() |
378
|
|
|
{ |
379
|
|
|
$cookie = 'cookie=TESTING; HttpOnly; expires=Mon, 16-Nov-2020 00:00:00 GMT; domain=.jonnyw.kiwi; path=/'; |
380
|
|
|
$data = array( |
381
|
|
|
'cookies' => array($cookie) |
382
|
|
|
); |
383
|
|
|
|
384
|
|
|
$response = $this->getResponse(); |
385
|
|
|
$response->import($data); |
386
|
|
|
|
387
|
|
|
$this->assertContains($cookie, $response->getCookies()); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
391
|
|
|
/** ++++++++++ TEST ENTITIES ++++++++++ **/ |
392
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Get response instance. |
396
|
|
|
* |
397
|
|
|
* @access protected |
398
|
|
|
* @return \JonnyW\PhantomJs\Http\Response |
399
|
|
|
*/ |
400
|
|
|
protected function getResponse() |
401
|
|
|
{ |
402
|
|
|
$response = new Response(); |
403
|
|
|
|
404
|
|
|
return $response; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|