Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * HttpResponseTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Network.Http
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
App::uses('HttpResponse', 'Network/Http');
19
 
20
/**
21
 * TestHttpResponse class
22
 *
23
 * @package       Cake.Test.Case.Network.Http
24
 */
25
class TestHttpResponse extends HttpResponse {
26
 
27
/**
28
 * Convenience method for testing protected method
29
 *
30
 * @param array $header Header as an indexed array (field => value)
31
 * @return array Parsed header
32
 */
33
	public function parseHeader($header) {
34
		return parent::_parseHeader($header);
35
	}
36
 
37
/**
38
 * Convenience method for testing protected method
39
 *
40
 * @param string $body A string containing the body to decode
41
 * @param bool|string $encoding Can be false in case no encoding is being used, or a string representing the encoding
42
 * @return mixed Array or false
43
 */
44
	public function decodeBody($body, $encoding = 'chunked') {
45
		return parent::_decodeBody($body, $encoding);
46
	}
47
 
48
/**
49
 * Convenience method for testing protected method
50
 *
51
 * @param string $body A string containing the chunked body to decode
52
 * @return mixed Array or false
53
 */
54
	public function decodeChunkedBody($body) {
55
		return parent::_decodeChunkedBody($body);
56
	}
57
 
58
/**
59
 * Convenience method for testing protected method
60
 *
61
 * @param string $token Token to unescape
62
 * @return string Unescaped token
63
 */
64
	public function unescapeToken($token, $chars = null) {
65
		return parent::_unescapeToken($token, $chars);
66
	}
67
 
68
/**
69
 * Convenience method for testing protected method
70
 *
71
 * @param bool $hex true to get them as HEX values, false otherwise
72
 * @return array Escape chars
73
 */
74
	public function tokenEscapeChars($hex = true, $chars = null) {
75
		return parent::_tokenEscapeChars($hex, $chars);
76
	}
77
 
78
}
79
 
80
/**
81
 * HttpResponseTest class
82
 *
83
 * @package       Cake.Test.Case.Network.Http
84
 */
85
class HttpResponseTest extends CakeTestCase {
86
 
87
/**
88
 * This function sets up a HttpResponse
89
 *
90
 * @return void
91
 */
92
	public function setUp() {
93
		parent::setUp();
94
		$this->HttpResponse = new TestHttpResponse();
95
	}
96
 
97
/**
98
 * testBody
99
 *
100
 * @return void
101
 */
102
	public function testBody() {
103
		$this->HttpResponse->body = 'testing';
104
		$this->assertEquals('testing', $this->HttpResponse->body());
105
 
106
		$this->HttpResponse->body = null;
107
		$this->assertSame($this->HttpResponse->body(), '');
108
	}
109
 
110
/**
111
 * testToString
112
 *
113
 * @return void
114
 */
115
	public function testToString() {
116
		$this->HttpResponse->body = 'other test';
117
		$this->assertEquals('other test', $this->HttpResponse->body());
118
		$this->assertEquals('other test', (string)$this->HttpResponse);
119
		$this->assertTrue(strpos($this->HttpResponse, 'test') > 0);
120
 
121
		$this->HttpResponse->body = null;
122
		$this->assertEquals('', (string)$this->HttpResponse);
123
	}
124
 
125
/**
126
 * testGetHeader
127
 *
128
 * @return void
129
 */
130
	public function testGetHeader() {
131
		$this->HttpResponse->headers = array(
132
			'foo' => 'Bar',
133
			'Some' => 'ok',
134
			'HeAdEr' => 'value',
135
			'content-Type' => 'text/plain'
136
		);
137
 
138
		$this->assertEquals('Bar', $this->HttpResponse->getHeader('foo'));
139
		$this->assertEquals('Bar', $this->HttpResponse->getHeader('Foo'));
140
		$this->assertEquals('Bar', $this->HttpResponse->getHeader('FOO'));
141
		$this->assertEquals('value', $this->HttpResponse->getHeader('header'));
142
		$this->assertEquals('text/plain', $this->HttpResponse->getHeader('Content-Type'));
143
		$this->assertNull($this->HttpResponse->getHeader(0));
144
 
145
		$this->assertEquals('Bar', $this->HttpResponse->getHeader('foo', false));
146
		$this->assertEquals('not from class', $this->HttpResponse->getHeader('foo', array('foo' => 'not from class')));
147
	}
148
 
149
/**
150
 * testIsOk
151
 *
152
 * @return void
153
 */
154
	public function testIsOk() {
155
		$this->HttpResponse->code = 0;
156
		$this->assertFalse($this->HttpResponse->isOk());
157
		$this->HttpResponse->code = -1;
158
		$this->assertFalse($this->HttpResponse->isOk());
159
		$this->HttpResponse->code = 'what?';
160
		$this->assertFalse($this->HttpResponse->isOk());
161
		$this->HttpResponse->code = 200;
162
		$this->assertTrue($this->HttpResponse->isOk());
163
		$this->HttpResponse->code = 201;
164
		$this->assertTrue($this->HttpResponse->isOk());
165
		$this->HttpResponse->code = 202;
166
		$this->assertTrue($this->HttpResponse->isOk());
167
		$this->HttpResponse->code = 203;
168
		$this->assertTrue($this->HttpResponse->isOk());
169
		$this->HttpResponse->code = 204;
170
		$this->assertTrue($this->HttpResponse->isOk());
171
		$this->HttpResponse->code = 205;
172
		$this->assertTrue($this->HttpResponse->isOk());
173
		$this->HttpResponse->code = 206;
174
		$this->assertTrue($this->HttpResponse->isOk());
175
		$this->HttpResponse->code = 207;
176
		$this->assertFalse($this->HttpResponse->isOk());
177
		$this->HttpResponse->code = 208;
178
		$this->assertFalse($this->HttpResponse->isOk());
179
		$this->HttpResponse->code = 209;
180
		$this->assertFalse($this->HttpResponse->isOk());
181
		$this->HttpResponse->code = 210;
182
		$this->assertFalse($this->HttpResponse->isOk());
183
		$this->HttpResponse->code = 226;
184
		$this->assertFalse($this->HttpResponse->isOk());
185
		$this->HttpResponse->code = 288;
186
		$this->assertFalse($this->HttpResponse->isOk());
187
		$this->HttpResponse->code = 301;
188
		$this->assertFalse($this->HttpResponse->isOk());
189
	}
190
 
191
/**
192
 * testIsRedirect
193
 *
194
 * @return void
195
 */
196
	public function testIsRedirect() {
197
		$this->HttpResponse->code = 0;
198
		$this->assertFalse($this->HttpResponse->isRedirect());
199
		$this->HttpResponse->code = -1;
200
		$this->assertFalse($this->HttpResponse->isRedirect());
201
		$this->HttpResponse->code = 201;
202
		$this->assertFalse($this->HttpResponse->isRedirect());
203
		$this->HttpResponse->code = 'what?';
204
		$this->assertFalse($this->HttpResponse->isRedirect());
205
		$this->HttpResponse->code = 301;
206
		$this->assertFalse($this->HttpResponse->isRedirect());
207
		$this->HttpResponse->code = 302;
208
		$this->assertFalse($this->HttpResponse->isRedirect());
209
		$this->HttpResponse->code = 303;
210
		$this->assertFalse($this->HttpResponse->isRedirect());
211
		$this->HttpResponse->code = 307;
212
		$this->assertFalse($this->HttpResponse->isRedirect());
213
		$this->HttpResponse->code = 301;
214
		$this->HttpResponse->headers['Location'] = 'http://somewhere/';
215
		$this->assertTrue($this->HttpResponse->isRedirect());
216
		$this->HttpResponse->code = 302;
217
		$this->HttpResponse->headers['Location'] = 'http://somewhere/';
218
		$this->assertTrue($this->HttpResponse->isRedirect());
219
		$this->HttpResponse->code = 303;
220
		$this->HttpResponse->headers['Location'] = 'http://somewhere/';
221
		$this->assertTrue($this->HttpResponse->isRedirect());
222
		$this->HttpResponse->code = 307;
223
		$this->HttpResponse->headers['Location'] = 'http://somewhere/';
224
		$this->assertTrue($this->HttpResponse->isRedirect());
225
	}
226
 
227
/**
228
 * Test that HttpSocket::parseHeader can take apart a given (and valid) $header string and turn it into an array.
229
 *
230
 * @return void
231
 */
232
	public function testParseHeader() {
233
		$r = $this->HttpResponse->parseHeader(array('foo' => 'Bar', 'fOO-bAr' => 'quux'));
234
		$this->assertEquals(array('foo' => 'Bar', 'fOO-bAr' => 'quux'), $r);
235
 
236
		$r = $this->HttpResponse->parseHeader(true);
237
		$this->assertEquals(false, $r);
238
 
239
		$header = "Host: cakephp.org\t\r\n";
240
		$r = $this->HttpResponse->parseHeader($header);
241
		$expected = array(
242
			'Host' => 'cakephp.org'
243
		);
244
		$this->assertEquals($expected, $r);
245
 
246
		$header = "Date:Sat, 07 Apr 2007 10:10:25 GMT\r\nX-Powered-By: PHP/5.1.2\r\n";
247
		$r = $this->HttpResponse->parseHeader($header);
248
		$expected = array(
249
			'Date' => 'Sat, 07 Apr 2007 10:10:25 GMT',
250
			'X-Powered-By' => 'PHP/5.1.2'
251
		);
252
		$this->assertEquals($expected, $r);
253
 
254
		$header = "people: Jim,John\r\nfoo-LAND: Bar\r\ncAKe-PHP: rocks\r\n";
255
		$r = $this->HttpResponse->parseHeader($header);
256
		$expected = array(
257
			'people' => 'Jim,John',
258
			'foo-LAND' => 'Bar',
259
			'cAKe-PHP' => 'rocks'
260
		);
261
		$this->assertEquals($expected, $r);
262
 
263
		$header = "People: Jim,John,Tim\r\nPeople: Lisa,Tina,Chelsea\r\n";
264
		$r = $this->HttpResponse->parseHeader($header);
265
		$expected = array(
266
			'People' => array('Jim,John,Tim', 'Lisa,Tina,Chelsea')
267
		);
268
		$this->assertEquals($expected, $r);
269
 
270
		$header = "Multi-Line: I am a \r\nmulti line\t\r\nfield value.\r\nSingle-Line: I am not\r\n";
271
		$r = $this->HttpResponse->parseHeader($header);
272
		$expected = array(
273
			'Multi-Line' => "I am a\r\nmulti line\r\nfield value.",
274
			'Single-Line' => 'I am not'
275
		);
276
		$this->assertEquals($expected, $r);
277
 
278
		$header = "Esc\"@\"ped: value\r\n";
279
		$r = $this->HttpResponse->parseHeader($header);
280
		$expected = array(
281
			'Esc@ped' => 'value'
282
		);
283
		$this->assertEquals($expected, $r);
284
	}
285
 
286
/**
287
 * testParseResponse method
288
 *
289
 * @return void
290
 */
291
	public function testParseResponse() {
292
		$tests = array(
293
			'simple-request' => array(
294
				'response' => array(
295
					'status-line' => "HTTP/1.x 200 OK\r\n",
296
					'header' => "Date: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n",
297
					'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
298
				),
299
				'expectations' => array(
300
					'httpVersion' => 'HTTP/1.x',
301
					'code' => 200,
302
					'reasonPhrase' => 'OK',
303
					'headers' => array('Date' => 'Mon, 16 Apr 2007 04:14:16 GMT', 'Server' => 'CakeHttp Server'),
304
					'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>"
305
				)
306
			),
307
			'no-header' => array(
308
				'response' => array(
309
					'status-line' => "HTTP/1.x 404 OK\r\n",
310
					'header' => null
311
				),
312
				'expectations' => array(
313
					'code' => 404,
314
					'headers' => array()
315
				)
316
			)
317
		);
318
 
319
		$testResponse = array();
320
		$expectations = array();
321
 
322
		foreach ($tests as $name => $test) {
323
			$testResponse = array_merge($testResponse, $test['response']);
324
			$testResponse['response'] = $testResponse['status-line'] . $testResponse['header'] . "\r\n" . $testResponse['body'];
325
			$this->HttpResponse->parseResponse($testResponse['response']);
326
			$expectations = array_merge($expectations, $test['expectations']);
327
 
328
			foreach ($expectations as $property => $expectedVal) {
329
				$this->assertEquals($expectedVal, $this->HttpResponse->{$property}, 'Test "' . $name . '": response.' . $property . ' - %s');
330
			}
331
 
332
			foreach (array('status-line', 'header', 'body', 'response') as $field) {
333
				$this->assertEquals($this->HttpResponse['raw'][$field], $testResponse[$field], 'Test response.raw.' . $field . ': %s');
334
			}
335
		}
336
	}
337
 
338
/**
339
 * data provider function for testInvalidParseResponseData
340
 *
341
 * @return array
342
 */
343
	public static function invalidParseResponseDataProvider() {
344
		return array(
345
			array(array('foo' => 'bar')),
346
			array(true),
347
			array("HTTP Foo\r\nBar: La"),
348
			array('HTTP/1.1 TEST ERROR')
349
		);
350
	}
351
 
352
/**
353
 * testInvalidParseResponseData
354
 *
355
 * @dataProvider invalidParseResponseDataProvider
356
 * @expectedException SocketException
357
 * @return void
358
 */
359
	public function testInvalidParseResponseData($value) {
360
		$this->HttpResponse->parseResponse($value);
361
	}
362
 
363
/**
364
 * testDecodeBody method
365
 *
366
 * @return void
367
 */
368
	public function testDecodeBody() {
369
		$r = $this->HttpResponse->decodeBody(true);
370
		$this->assertEquals(false, $r);
371
 
372
		$r = $this->HttpResponse->decodeBody('Foobar', false);
373
		$this->assertEquals(array('body' => 'Foobar', 'header' => false), $r);
374
 
375
		$encoding = 'chunked';
376
		$sample = array(
377
			'encoded' => "19\r\nThis is a chunked message\r\n0\r\n",
378
			'decoded' => array('body' => "This is a chunked message", 'header' => false)
379
		);
380
 
381
		$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
382
		$this->assertEquals($r, $sample['decoded']);
383
 
384
		$encoding = 'chunked';
385
		$sample = array(
386
			'encoded' => "19\nThis is a chunked message\r\n0\n",
387
			'decoded' => array('body' => "This is a chunked message", 'header' => false)
388
		);
389
 
390
		$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
391
		$this->assertEquals($r, $sample['decoded'], 'Inconsistent line terminators should be tolerated.');
392
	}
393
 
394
/**
395
 * testDecodeFooCoded
396
 *
397
 * @return void
398
 */
399
	public function testDecodeFooCoded() {
400
		$r = $this->HttpResponse->decodeBody(true);
401
		$this->assertEquals(false, $r);
402
 
403
		$r = $this->HttpResponse->decodeBody('Foobar', false);
404
		$this->assertEquals(array('body' => 'Foobar', 'header' => false), $r);
405
 
406
		$encoding = 'foo-bar';
407
		$sample = array(
408
			'encoded' => '!Foobar!',
409
			'decoded' => array('body' => '!Foobar!', 'header' => false),
410
		);
411
 
412
		$r = $this->HttpResponse->decodeBody($sample['encoded'], $encoding);
413
		$this->assertEquals($r, $sample['decoded']);
414
	}
415
 
416
/**
417
 * testDecodeChunkedBody method
418
 *
419
 * @return void
420
 */
421
	public function testDecodeChunkedBody() {
422
		$r = $this->HttpResponse->decodeChunkedBody(true);
423
		$this->assertEquals(false, $r);
424
 
425
		$encoded = "19\r\nThis is a chunked message\r\n0\r\n";
426
		$decoded = "This is a chunked message";
427
		$r = $this->HttpResponse->decodeChunkedBody($encoded);
428
		$this->assertEquals($r['body'], $decoded);
429
		$this->assertEquals(false, $r['header']);
430
 
431
		$encoded = "19 \r\nThis is a chunked message\r\n0\r\n";
432
		$r = $this->HttpResponse->decodeChunkedBody($encoded);
433
		$this->assertEquals($r['body'], $decoded);
434
 
435
		$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\n";
436
		$decoded = "This is a chunked message\nThat is cool\n";
437
		$r = $this->HttpResponse->decodeChunkedBody($encoded);
438
		$this->assertEquals($r['body'], $decoded);
439
		$this->assertEquals(false, $r['header']);
440
 
441
		$encoded = "19\r\nThis is a chunked message\r\nE;foo-chunk=5\r\n\nThat is cool\n\r\n0\r\n";
442
		$r = $this->HttpResponse->decodeChunkedBody($encoded);
443
		$this->assertEquals($r['body'], $decoded);
444
		$this->assertEquals(false, $r['header']);
445
 
446
		$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n";
447
		$r = $this->HttpResponse->decodeChunkedBody($encoded);
448
		$this->assertEquals($r['body'], $decoded);
449
		$this->assertEquals(array('foo-header' => 'bar', 'cake' => 'PHP'), $r['header']);
450
	}
451
 
452
/**
453
 * testDecodeChunkedBodyError method
454
 *
455
 * @return void
456
 */
457
	public function testDecodeChunkedBodyError() {
458
		$encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n";
459
		$result = $this->HttpResponse->decodeChunkedBody($encoded);
460
		$expected = "This is a chunked message\nThat is cool\n";
461
		$this->assertEquals($expected, $result['body']);
462
	}
463
 
464
/**
465
 * testParseCookies method
466
 *
467
 * @return void
468
 */
469
	public function testParseCookies() {
470
		$header = array(
471
			'Set-Cookie' => array(
472
				'foo=bar',
473
				'people=jim,jack,johnny";";Path=/accounts',
474
				'google=not=nice'
475
			),
476
			'Transfer-Encoding' => 'chunked',
477
			'Date' => 'Sun, 18 Nov 2007 18:57:42 GMT',
478
		);
479
		$cookies = $this->HttpResponse->parseCookies($header);
480
		$expected = array(
481
			'foo' => array(
482
				'value' => 'bar'
483
			),
484
			'people' => array(
485
				'value' => 'jim,jack,johnny";"',
486
				'path' => '/accounts',
487
			),
488
			'google' => array(
489
				'value' => 'not=nice',
490
			)
491
		);
492
		$this->assertEquals($expected, $cookies);
493
 
494
		$header['Set-Cookie'][] = 'cakephp=great; Secure';
495
		$expected['cakephp'] = array('value' => 'great', 'secure' => true);
496
		$cookies = $this->HttpResponse->parseCookies($header);
497
		$this->assertEquals($expected, $cookies);
498
 
499
		$header['Set-Cookie'] = 'foo=bar';
500
		unset($expected['people'], $expected['cakephp'], $expected['google']);
501
		$cookies = $this->HttpResponse->parseCookies($header);
502
		$this->assertEquals($expected, $cookies);
503
	}
504
 
505
/**
506
 * Test that escaped token strings are properly unescaped by HttpSocket::unescapeToken
507
 *
508
 * @return void
509
 */
510
	public function testUnescapeToken() {
511
		$this->assertEquals('Foo', $this->HttpResponse->unescapeToken('Foo'));
512
 
513
		$escape = $this->HttpResponse->tokenEscapeChars(false);
514
		foreach ($escape as $char) {
515
			$token = 'My-special-"' . $char . '"-Token';
516
			$unescapedToken = $this->HttpResponse->unescapeToken($token);
517
			$expectedToken = 'My-special-' . $char . '-Token';
518
 
519
			$this->assertEquals($expectedToken, $unescapedToken, 'Test token unescaping for ASCII ' . ord($char));
520
		}
521
 
522
		$token = 'Extreme-":"Token-"	"-""""@"-test';
523
		$escapedToken = $this->HttpResponse->unescapeToken($token);
524
		$expectedToken = 'Extreme-:Token-	-"@-test';
525
		$this->assertEquals($expectedToken, $escapedToken);
526
	}
527
 
528
/**
529
 * testArrayAccess
530
 *
531
 * @return void
532
 */
533
	public function testArrayAccess() {
534
		$this->HttpResponse->httpVersion = 'HTTP/1.1';
535
		$this->HttpResponse->code = 200;
536
		$this->HttpResponse->reasonPhrase = 'OK';
537
		$this->HttpResponse->headers = array(
538
			'Server' => 'CakePHP',
539
			'ContEnt-Type' => 'text/plain'
540
		);
541
		$this->HttpResponse->cookies = array(
542
			'foo' => array('value' => 'bar'),
543
			'bar' => array('value' => 'foo')
544
		);
545
		$this->HttpResponse->body = 'This is a test!';
546
		$this->HttpResponse->raw = "HTTP/1.1 200 OK\r\nServer: CakePHP\r\nContEnt-Type: text/plain\r\n\r\nThis is a test!";
547
		$expectedOne = "HTTP/1.1 200 OK\r\n";
548
		$this->assertEquals($expectedOne, $this->HttpResponse['raw']['status-line']);
549
		$expectedTwo = "Server: CakePHP\r\nContEnt-Type: text/plain\r\n";
550
		$this->assertEquals($expectedTwo, $this->HttpResponse['raw']['header']);
551
		$expectedThree = 'This is a test!';
552
		$this->assertEquals($expectedThree, $this->HttpResponse['raw']['body']);
553
		$expected = $expectedOne . $expectedTwo . "\r\n" . $expectedThree;
554
		$this->assertEquals($expected, $this->HttpResponse['raw']['response']);
555
 
556
		$expected = 'HTTP/1.1';
557
		$this->assertEquals($expected, $this->HttpResponse['status']['http-version']);
558
		$expected = 200;
559
		$this->assertEquals($expected, $this->HttpResponse['status']['code']);
560
		$expected = 'OK';
561
		$this->assertEquals($expected, $this->HttpResponse['status']['reason-phrase']);
562
 
563
		$expected = array(
564
			'Server' => 'CakePHP',
565
			'ContEnt-Type' => 'text/plain'
566
		);
567
		$this->assertEquals($expected, $this->HttpResponse['header']);
568
 
569
		$expected = 'This is a test!';
570
		$this->assertEquals($expected, $this->HttpResponse['body']);
571
 
572
		$expected = array(
573
			'foo' => array('value' => 'bar'),
574
			'bar' => array('value' => 'foo')
575
		);
576
		$this->assertEquals($expected, $this->HttpResponse['cookies']);
577
 
578
		$this->HttpResponse->raw = "HTTP/1.1 200 OK\r\n\r\nThis is a test!";
579
		$this->assertNull($this->HttpResponse['raw']['header']);
580
	}
581
 
582
}