Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
/**
3
 * HttpSocketTest 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
 
19
App::uses('HttpSocket', 'Network/Http');
20
App::uses('HttpResponse', 'Network/Http');
21
 
22
/**
23
 * TestAuthentication class
24
 *
25
 * @package       Cake.Test.Case.Network.Http
26
 */
27
class TestAuthentication {
28
 
29
/**
30
 * authentication method
31
 *
32
 * @param HttpSocket $http
33
 * @param array $authInfo
34
 * @return void
35
 */
36
	public static function authentication(HttpSocket $http, &$authInfo) {
37
		$http->request['header']['Authorization'] = 'Test ' . $authInfo['user'] . '.' . $authInfo['pass'];
38
	}
39
 
40
/**
41
 * proxyAuthentication method
42
 *
43
 * @param HttpSocket $http
44
 * @param array $proxyInfo
45
 * @return void
46
 */
47
	public static function proxyAuthentication(HttpSocket $http, &$proxyInfo) {
48
		$http->request['header']['Proxy-Authorization'] = 'Test ' . $proxyInfo['user'] . '.' . $proxyInfo['pass'];
49
	}
50
 
51
}
52
 
53
/**
54
 * CustomResponse
55
 *
56
 */
57
class CustomResponse {
58
 
59
/**
60
 * First 10 chars
61
 *
62
 * @var string
63
 */
64
	public $first10;
65
 
66
/**
67
 * Constructor
68
 *
69
 */
70
	public function __construct($message) {
71
		$this->first10 = substr($message, 0, 10);
72
	}
73
 
74
}
75
 
76
/**
77
 * TestHttpSocket
78
 *
79
 */
80
class TestHttpSocket extends HttpSocket {
81
 
82
/**
83
 * Convenience method for testing protected method
84
 *
85
 * @param string|array $uri URI (see {@link _parseUri()})
86
 * @return array Current configuration settings
87
 */
88
	public function configUri($uri = null) {
89
		return parent::_configUri($uri);
90
	}
91
 
92
/**
93
 * Convenience method for testing protected method
94
 *
95
 * @param string|array $uri URI to parse
96
 * @param bool|array $base If true use default URI config, otherwise indexed array to set 'scheme', 'host', 'port', etc.
97
 * @return array Parsed URI
98
 */
99
	public function parseUri($uri = null, $base = array()) {
100
		return parent::_parseUri($uri, $base);
101
	}
102
 
103
/**
104
 * Convenience method for testing protected method
105
 *
106
 * @param array $uri A $uri array, or uses $this->config if left empty
107
 * @param string $uriTemplate The Uri template/format to use
108
 * @return string A fully qualified URL formatted according to $uriTemplate
109
 */
110
	public function buildUri($uri = array(), $uriTemplate = '%scheme://%user:%pass@%host:%port/%path?%query#%fragment') {
111
		return parent::_buildUri($uri, $uriTemplate);
112
	}
113
 
114
/**
115
 * Convenience method for testing protected method
116
 *
117
 * @param array $header Header to build
118
 * @return string Header built from array
119
 */
120
	public function buildHeader($header, $mode = 'standard') {
121
		return parent::_buildHeader($header, $mode);
122
	}
123
 
124
/**
125
 * Convenience method for testing protected method
126
 *
127
 * @param string|array $query A query string to parse into an array or an array to return directly "as is"
128
 * @return array The $query parsed into a possibly multi-level array. If an empty $query is given, an empty array is returned.
129
 */
130
	public function parseQuery($query) {
131
		return parent::_parseQuery($query);
132
	}
133
 
134
/**
135
 * Convenience method for testing protected method
136
 *
137
 * @param array $request Needs to contain a 'uri' key. Should also contain a 'method' key, otherwise defaults to GET.
138
 * @param string $versionToken The version token to use, defaults to HTTP/1.1
139
 * @return string Request line
140
 */
141
	public function buildRequestLine($request = array()) {
142
		return parent::_buildRequestLine($request);
143
	}
144
 
145
/**
146
 * Convenience method for testing protected method
147
 *
148
 * @param bool $hex true to get them as HEX values, false otherwise
149
 * @return array Escape chars
150
 */
151
	public function tokenEscapeChars($hex = true, $chars = null) {
152
		return parent::_tokenEscapeChars($hex, $chars);
153
	}
154
 
155
/**
156
 * Convenience method for testing protected method
157
 *
158
 * @param string $token Token to escape
159
 * @return string Escaped token
160
 */
161
	public function escapeToken($token, $chars = null) {
162
		return parent::_escapeToken($token, $chars);
163
	}
164
 
165
}
166
 
167
/**
168
 * HttpSocketTest class
169
 *
170
 * @package       Cake.Test.Case.Network.Http
171
 */
172
class HttpSocketTest extends CakeTestCase {
173
 
174
/**
175
 * Socket property
176
 *
177
 * @var mixed
178
 */
179
	public $Socket = null;
180
 
181
/**
182
 * RequestSocket property
183
 *
184
 * @var mixed
185
 */
186
	public $RequestSocket = null;
187
 
188
/**
189
 * This function sets up a TestHttpSocket instance we are going to use for testing
190
 *
191
 * @return void
192
 */
193
	public function setUp() {
194
		parent::setUp();
195
		$this->Socket = $this->getMock('TestHttpSocket', array('read', 'write', 'connect'));
196
		$this->RequestSocket = $this->getMock('TestHttpSocket', array('read', 'write', 'connect', 'request'));
197
	}
198
 
199
/**
200
 * We use this function to clean up after the test case was executed
201
 *
202
 * @return void
203
 */
204
	public function tearDown() {
205
		parent::tearDown();
206
		unset($this->Socket, $this->RequestSocket);
207
	}
208
 
209
/**
210
 * Test that HttpSocket::__construct does what one would expect it to do
211
 *
212
 * @return void
213
 */
214
	public function testConstruct() {
215
		$this->Socket->reset();
216
		$baseConfig = $this->Socket->config;
217
		$this->Socket->expects($this->never())->method('connect');
218
		$this->Socket->__construct(array('host' => 'foo-bar'));
219
		$baseConfig['host'] = 'foo-bar';
220
		$this->assertEquals($this->Socket->config, $baseConfig);
221
 
222
		$this->Socket->reset();
223
		$baseConfig = $this->Socket->config;
224
		$this->Socket->__construct('http://www.cakephp.org:23/');
225
		$baseConfig['host'] = $baseConfig['request']['uri']['host'] = 'www.cakephp.org';
226
		$baseConfig['port'] = $baseConfig['request']['uri']['port'] = 23;
227
		$baseConfig['request']['uri']['scheme'] = 'http';
228
		$this->assertEquals($this->Socket->config, $baseConfig);
229
 
230
		$this->Socket->reset();
231
		$this->Socket->__construct(array('request' => array('uri' => 'http://www.cakephp.org:23/')));
232
		$this->assertEquals($this->Socket->config, $baseConfig);
233
	}
234
 
235
/**
236
 * Test that HttpSocket::configUri works properly with different types of arguments
237
 *
238
 * @return void
239
 */
240
	public function testConfigUri() {
241
		$this->Socket->reset();
242
		$r = $this->Socket->configUri('https://bob:secret@www.cakephp.org:23/?query=foo');
243
		$expected = array(
244
			'persistent' => false,
245
			'host' => 'www.cakephp.org',
246
			'protocol' => 'tcp',
247
			'port' => 23,
248
			'timeout' => 30,
249
			'ssl_verify_peer' => true,
250
			'ssl_allow_self_signed' => false,
251
			'ssl_verify_depth' => 5,
252
			'ssl_verify_host' => true,
253
			'request' => array(
254
				'uri' => array(
255
					'scheme' => 'https',
256
					'host' => 'www.cakephp.org',
257
					'port' => 23
258
				),
259
				'redirect' => false,
260
				'cookies' => array(),
261
			)
262
		);
263
		$this->assertEquals($expected, $this->Socket->config);
264
		$this->assertTrue($r);
265
		$r = $this->Socket->configUri(array('host' => 'www.foo-bar.org'));
266
		$expected['host'] = 'www.foo-bar.org';
267
		$expected['request']['uri']['host'] = 'www.foo-bar.org';
268
		$this->assertEquals($expected, $this->Socket->config);
269
		$this->assertTrue($r);
270
 
271
		$r = $this->Socket->configUri('http://www.foo.com');
272
		$expected = array(
273
			'persistent' => false,
274
			'host' => 'www.foo.com',
275
			'protocol' => 'tcp',
276
			'port' => 80,
277
			'timeout' => 30,
278
			'ssl_verify_peer' => true,
279
			'ssl_allow_self_signed' => false,
280
			'ssl_verify_depth' => 5,
281
			'ssl_verify_host' => true,
282
			'request' => array(
283
				'uri' => array(
284
					'scheme' => 'http',
285
					'host' => 'www.foo.com',
286
					'port' => 80
287
				),
288
				'redirect' => false,
289
				'cookies' => array(),
290
			)
291
		);
292
		$this->assertEquals($expected, $this->Socket->config);
293
		$this->assertTrue($r);
294
 
295
		$r = $this->Socket->configUri('/this-is-broken');
296
		$this->assertEquals($expected, $this->Socket->config);
297
		$this->assertFalse($r);
298
 
299
		$r = $this->Socket->configUri(false);
300
		$this->assertEquals($expected, $this->Socket->config);
301
		$this->assertFalse($r);
302
	}
303
 
304
/**
305
 * Tests that HttpSocket::request (the heart of the HttpSocket) is working properly.
306
 *
307
 * @return void
308
 */
309
	public function testRequest() {
310
		$this->Socket->reset();
311
 
312
		$response = $this->Socket->request(true);
313
		$this->assertFalse($response);
314
 
315
		$context = array(
316
			'ssl' => array(
317
				'verify_peer' => true,
318
				'allow_self_signed' => false,
319
				'verify_depth' => 5,
320
				'SNI_enabled' => true,
321
				'CN_match' => 'www.cakephp.org',
322
				'cafile' => CAKE . 'Config' . DS . 'cacert.pem'
323
			)
324
		);
325
 
326
		if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
327
			$context['ssl']['peer_name'] = 'www.cakephp.org';
328
		} else {
329
			$context['ssl']['SNI_server_name'] = 'www.cakephp.org';
330
		}
331
 
332
		$tests = array(
333
			array(
334
				'request' => 'http://www.cakephp.org/?foo=bar',
335
				'expectation' => array(
336
					'config' => array(
337
						'persistent' => false,
338
						'host' => 'www.cakephp.org',
339
						'protocol' => 'tcp',
340
						'port' => 80,
341
						'timeout' => 30,
342
						'context' => $context,
343
						'request' => array(
344
							'uri' => array(
345
								'scheme' => 'http',
346
								'host' => 'www.cakephp.org',
347
								'port' => 80
348
							),
349
							'redirect' => false,
350
							'cookies' => array()
351
						)
352
					),
353
					'request' => array(
354
						'method' => 'GET',
355
						'uri' => array(
356
							'scheme' => 'http',
357
							'host' => 'www.cakephp.org',
358
							'port' => 80,
359
							'user' => null,
360
							'pass' => null,
361
							'path' => '/',
362
							'query' => array('foo' => 'bar'),
363
							'fragment' => null
364
						),
365
						'version' => '1.1',
366
						'body' => '',
367
						'line' => "GET /?foo=bar HTTP/1.1\r\n",
368
						'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n",
369
						'raw' => "",
370
						'redirect' => false,
371
						'cookies' => array(),
372
						'proxy' => array(),
373
						'auth' => array()
374
					)
375
				)
376
			),
377
			array(
378
				'request' => array(
379
					'uri' => array(
380
						'host' => 'www.cakephp.org',
381
						'query' => '?foo=bar'
382
					)
383
				)
384
			),
385
			array(
386
				'request' => 'www.cakephp.org/?foo=bar'
387
			),
388
			array(
389
				'request' => array(
390
					'host' => '192.168.0.1',
391
					'uri' => 'http://www.cakephp.org/?foo=bar'
392
				),
393
				'expectation' => array(
394
					'request' => array(
395
						'uri' => array('host' => 'www.cakephp.org')
396
					),
397
					'config' => array(
398
						'request' => array(
399
							'uri' => array('host' => 'www.cakephp.org')
400
						),
401
						'host' => '192.168.0.1'
402
					)
403
				)
404
			),
405
			'reset4' => array(
406
				'request.uri.query' => array()
407
			),
408
			array(
409
				'request' => array(
410
					'header' => array('Foo@woo' => 'bar-value')
411
				),
412
				'expectation' => array(
413
					'request' => array(
414
						'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n",
415
						'line' => "GET / HTTP/1.1\r\n"
416
					)
417
				)
418
			),
419
			array(
420
				'request' => array('header' => array('Foo@woo' => 'bar-value', 'host' => 'foo.com'), 'uri' => 'http://www.cakephp.org/'),
421
				'expectation' => array(
422
					'request' => array(
423
						'header' => "Host: foo.com\r\nConnection: close\r\nUser-Agent: CakePHP\r\nFoo\"@\"woo: bar-value\r\n"
424
					),
425
					'config' => array(
426
						'host' => 'www.cakephp.org'
427
					)
428
				)
429
			),
430
			array(
431
				'request' => array('header' => "Foo: bar\r\n"),
432
				'expectation' => array(
433
					'request' => array(
434
						'header' => "Foo: bar\r\n"
435
					)
436
				)
437
			),
438
			array(
439
				'request' => array('header' => "Foo: bar\r\n", 'uri' => 'http://www.cakephp.org/search?q=http_socket#ignore-me'),
440
				'expectation' => array(
441
					'request' => array(
442
						'uri' => array(
443
							'path' => '/search',
444
							'query' => array('q' => 'http_socket'),
445
							'fragment' => 'ignore-me'
446
						),
447
						'line' => "GET /search?q=http_socket HTTP/1.1\r\n"
448
					)
449
				)
450
			),
451
			'reset8' => array(
452
				'request.uri.query' => array()
453
			),
454
			array(
455
				'request' => array(
456
					'method' => 'POST',
457
					'uri' => 'http://www.cakephp.org/posts/add',
458
					'body' => array(
459
						'name' => 'HttpSocket-is-released',
460
						'date' => 'today'
461
					)
462
				),
463
				'expectation' => array(
464
					'request' => array(
465
						'method' => 'POST',
466
						'uri' => array(
467
							'path' => '/posts/add',
468
							'fragment' => null
469
						),
470
						'body' => "name=HttpSocket-is-released&date=today",
471
						'line' => "POST /posts/add HTTP/1.1\r\n",
472
						'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\n",
473
						'raw' => "name=HttpSocket-is-released&date=today"
474
					)
475
				)
476
			),
477
			array(
478
				'request' => array(
479
					'method' => 'POST',
480
					'uri' => 'http://www.cakephp.org:8080/posts/add',
481
					'body' => array(
482
						'name' => 'HttpSocket-is-released',
483
						'date' => 'today'
484
					)
485
				),
486
				'expectation' => array(
487
					'config' => array(
488
						'port' => 8080,
489
						'request' => array(
490
							'uri' => array(
491
								'port' => 8080
492
							)
493
						)
494
					),
495
					'request' => array(
496
						'uri' => array(
497
							'port' => 8080
498
						),
499
						'header' => "Host: www.cakephp.org:8080\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\n"
500
					)
501
				)
502
			),
503
			'reset10' => array(
504
				'config.protocol' => 'ssl'
505
			),
506
			array(
507
				'request' => array(
508
					'method' => 'POST',
509
					'uri' => 'https://www.cakephp.org/posts/add',
510
					'body' => array(
511
						'name' => 'HttpSocket-is-released',
512
						'date' => 'today'
513
					)
514
				),
515
				'expectation' => array(
516
					'config' => array(
517
						'port' => 443,
518
						'request' => array(
519
							'uri' => array(
520
								'scheme' => 'https',
521
								'port' => 443
522
							)
523
						)
524
					),
525
					'request' => array(
526
						'uri' => array(
527
							'scheme' => 'https',
528
							'port' => 443
529
						),
530
						'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\n"
531
					)
532
				)
533
			),
534
			'reset11' => array(
535
				'config.protocol' => 'ssl'
536
			),
537
			array(
538
				'request' => array(
539
					'version' => '1.0',
540
					'method' => 'POST',
541
					'uri' => 'https://www.cakephp.org/posts/add',
542
					'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'),
543
					'cookies' => array('foo' => array('value' => 'bar'))
544
				),
545
				'expectation' => array(
546
					'request' => array(
547
						'version' => '1.0',
548
						'line' => "POST /posts/add HTTP/1.0\r\n",
549
						'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 38\r\nCookie: foo=bar\r\n",
550
						'cookies' => array(
551
							'foo' => array('value' => 'bar'),
552
						)
553
					)
554
				)
555
			)
556
		);
557
 
558
		$expectation = array();
559
		foreach ($tests as $i => $test) {
560
			if (strpos($i, 'reset') === 0) {
561
				foreach ($test as $path => $val) {
562
					$expectation = Hash::insert($expectation, $path, $val);
563
				}
564
				continue;
565
			}
566
 
567
			if (isset($test['expectation'])) {
568
				$expectation = Hash::merge($expectation, $test['expectation']);
569
			}
570
			$this->Socket->request($test['request']);
571
 
572
			$raw = $expectation['request']['raw'];
573
			$expectation['request']['raw'] = $expectation['request']['line'] . $expectation['request']['header'] . "\r\n" . $raw;
574
 
575
			$r = array('config' => $this->Socket->config, 'request' => $this->Socket->request);
576
			$this->assertEquals($r, $expectation, 'Failed test #' . $i . ' ');
577
			$expectation['request']['raw'] = $raw;
578
		}
579
 
580
		$this->Socket->reset();
581
		$request = array('method' => 'POST', 'uri' => 'http://www.cakephp.org/posts/add', 'body' => array('name' => 'HttpSocket-is-released', 'date' => 'today'));
582
		$response = $this->Socket->request($request);
583
		$this->assertEquals("name=HttpSocket-is-released&date=today", $this->Socket->request['body']);
584
	}
585
 
586
/**
587
 * Test the scheme + port keys
588
 *
589
 * @return void
590
 */
591
	public function testGetWithSchemeAndPort() {
592
		$this->Socket->reset();
593
		$request = array(
594
			'uri' => array(
595
				'scheme' => 'http',
596
				'host' => 'cakephp.org',
597
				'port' => 8080,
598
				'path' => '/',
599
			),
600
			'method' => 'GET'
601
		);
602
		$this->Socket->request($request);
603
		$this->assertContains('Host: cakephp.org:8080', $this->Socket->request['header']);
604
	}
605
 
606
/**
607
 * Test URLs like http://cakephp.org/index.php?somestring without key/value pair for query
608
 *
609
 * @return void
610
 */
611
	public function testRequestWithStringQuery() {
612
		$this->Socket->reset();
613
		$request = array(
614
			'uri' => array(
615
				'scheme' => 'http',
616
				'host' => 'cakephp.org',
617
				'path' => 'index.php',
618
				'query' => 'somestring'
619
			),
620
			'method' => 'GET'
621
		);
622
		$this->Socket->request($request);
623
		$this->assertContains("GET /index.php?somestring HTTP/1.1", $this->Socket->request['line']);
624
	}
625
 
626
/**
627
 * The "*" asterisk character is only allowed for the following methods: OPTIONS.
628
 *
629
 * @expectedException SocketException
630
 * @return void
631
 */
632
	public function testRequestNotAllowedUri() {
633
		$this->Socket->reset();
634
		$request = array('uri' => '*', 'method' => 'GET');
635
		$this->Socket->request($request);
636
	}
637
 
638
/**
639
 * testRequest2 method
640
 *
641
 * @return void
642
 */
643
	public function testRequest2() {
644
		$this->Socket->reset();
645
		$request = array('uri' => 'htpp://www.cakephp.org/');
646
		$number = mt_rand(0, 9999999);
647
		$this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
648
		$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>Hello, your lucky number is " . $number . "</h1>";
649
		$this->Socket->expects($this->at(0))->method('write')
650
			->with("GET / HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n");
651
		$this->Socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
652
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
653
		$response = (string)$this->Socket->request($request);
654
		$this->assertEquals($response, "<h1>Hello, your lucky number is " . $number . "</h1>");
655
	}
656
 
657
/**
658
 * testRequest3 method
659
 *
660
 * @return void
661
 */
662
	public function testRequest3() {
663
		$request = array('uri' => 'htpp://www.cakephp.org/');
664
		$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foo=bar\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a cookie test!</h1>";
665
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
666
		$this->Socket->connected = true;
667
		$this->Socket->request($request);
668
		$result = $this->Socket->response['cookies'];
669
		$expected = array(
670
			'foo' => array(
671
				'value' => 'bar'
672
			)
673
		);
674
		$this->assertEquals($expected, $result);
675
		$this->assertEquals($expected, $this->Socket->config['request']['cookies']['www.cakephp.org']);
676
		$this->assertFalse($this->Socket->connected);
677
	}
678
 
679
/**
680
 * testRequestWithConstructor method
681
 *
682
 * @return void
683
 */
684
	public function testRequestWithConstructor() {
685
		$request = array(
686
			'request' => array(
687
				'uri' => array(
688
					'scheme' => 'http',
689
					'host' => 'localhost',
690
					'port' => '5984',
691
					'user' => null,
692
					'pass' => null
693
				)
694
			)
695
		);
696
		$http = $this->getMock('TestHttpSocket', array('read', 'write', 'connect', 'request'), array($request));
697
 
698
		$expected = array('method' => 'GET', 'uri' => '/_test');
699
		$http->expects($this->at(0))->method('request')->with($expected);
700
		$http->get('/_test');
701
 
702
		$expected = array('method' => 'GET', 'uri' => 'http://localhost:5984/_test?count=4');
703
		$http->expects($this->at(0))->method('request')->with($expected);
704
		$http->get('/_test', array('count' => 4));
705
	}
706
 
707
/**
708
 * testRequestWithResource
709
 *
710
 * @return void
711
 */
712
	public function testRequestWithResource() {
713
		$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
714
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
715
		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
716
		$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse));
717
		$this->Socket->connected = true;
718
 
719
		$f = fopen(TMP . 'download.txt', 'w');
720
		if (!$f) {
721
			$this->markTestSkipped('Can not write in TMP directory.');
722
		}
723
 
724
		$this->Socket->setContentResource($f);
725
		$result = (string)$this->Socket->request('http://www.cakephp.org/');
726
		$this->assertEquals('', $result);
727
		$this->assertEquals('CakeHttp Server', $this->Socket->response['header']['Server']);
728
		fclose($f);
729
		$this->assertEquals(file_get_contents(TMP . 'download.txt'), '<h1>This is a test!</h1>');
730
		unlink(TMP . 'download.txt');
731
 
732
		$this->Socket->setContentResource(false);
733
		$result = (string)$this->Socket->request('http://www.cakephp.org/');
734
		$this->assertEquals('<h1>This is a test!</h1>', $result);
735
	}
736
 
737
/**
738
 * testRequestWithCrossCookie
739
 *
740
 * @return void
741
 */
742
	public function testRequestWithCrossCookie() {
743
		$this->Socket->connected = true;
744
		$this->Socket->config['request']['cookies'] = array();
745
 
746
		$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foo=bar\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
747
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
748
		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
749
		$expected = array('www.cakephp.org' => array('foo' => array('value' => 'bar')));
750
		$this->Socket->request('http://www.cakephp.org/');
751
		$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
752
 
753
		$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: bar=foo\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
754
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
755
		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
756
		$this->Socket->request('http://www.cakephp.org/other');
757
		$this->assertEquals(array('foo' => array('value' => 'bar')), $this->Socket->request['cookies']);
758
		$expected['www.cakephp.org'] += array('bar' => array('value' => 'foo'));
759
		$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
760
 
761
		$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
762
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
763
		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
764
		$this->Socket->request('/other2');
765
		$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
766
 
767
		$serverResponse = "HTTP/1.x 200 OK\r\nSet-Cookie: foobar=ok\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
768
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
769
		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
770
		$this->Socket->request('http://www.cake.com');
771
		$this->assertTrue(empty($this->Socket->request['cookies']));
772
		$expected['www.cake.com'] = array('foobar' => array('value' => 'ok'));
773
		$this->assertEquals($expected, $this->Socket->config['request']['cookies']);
774
	}
775
 
776
/**
777
 * testRequestCustomResponse
778
 *
779
 * @return void
780
 */
781
	public function testRequestCustomResponse() {
782
		$this->Socket->connected = true;
783
		$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
784
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
785
		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
786
 
787
		$this->Socket->responseClass = 'CustomResponse';
788
		$response = $this->Socket->request('http://www.cakephp.org/');
789
		$this->assertInstanceOf('CustomResponse', $response);
790
		$this->assertEquals('HTTP/1.x 2', $response->first10);
791
	}
792
 
793
/**
794
 * Test that redirect URLs are urldecoded
795
 *
796
 * @return void
797
 */
798
	public function testRequestWithRedirectUrlEncoded() {
799
		$request = array(
800
			'uri' => 'http://localhost/oneuri',
801
			'redirect' => 1
802
		);
803
		$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://i.cmpnet.com%2Ftechonline%2Fpdf%2Fa+b.pdf=\r\n\r\n";
804
		$serverResponse2 = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>You have been redirected</h1>";
805
 
806
		$this->Socket->expects($this->at(1))
807
			->method('read')
808
			->will($this->returnValue($serverResponse1));
809
 
810
		$this->Socket->expects($this->at(3))
811
			->method('write')
812
			->with($this->logicalAnd(
813
				$this->stringContains('Host: i.cmpnet.com'),
814
				$this->stringContains('GET /techonline/pdf/a+b.pdf')
815
			));
816
 
817
		$this->Socket->expects($this->at(4))
818
			->method('read')
819
			->will($this->returnValue($serverResponse2));
820
 
821
		$response = $this->Socket->request($request);
822
		$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
823
	}
824
 
825
/**
826
 * testRequestWithRedirect method
827
 *
828
 * @return void
829
 */
830
	public function testRequestWithRedirectAsTrue() {
831
		$request = array(
832
			'uri' => 'http://localhost/oneuri',
833
			'redirect' => true
834
		);
835
		$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
836
		$serverResponse2 = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>You have been redirected</h1>";
837
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
838
		$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
839
 
840
		$response = $this->Socket->request($request);
841
		$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
842
	}
843
 
844
/**
845
 * Test that redirects with a count limit are decremented.
846
 *
847
 * @return void
848
 */
849
	public function testRequestWithRedirectAsInt() {
850
		$request = array(
851
			'uri' => 'http://localhost/oneuri',
852
			'redirect' => 2
853
		);
854
		$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
855
		$serverResponse2 = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>You have been redirected</h1>";
856
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
857
		$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
858
 
859
		$this->Socket->request($request);
860
		$this->assertEquals(1, $this->Socket->request['redirect']);
861
	}
862
 
863
/**
864
 * Test that redirects after the redirect count reaches 9 are not followed.
865
 *
866
 * @return void
867
 */
868
	public function testRequestWithRedirectAsIntReachingZero() {
869
		$request = array(
870
			'uri' => 'http://localhost/oneuri',
871
			'redirect' => 1
872
		);
873
		$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/oneruri\r\n\r\n";
874
		$serverResponse2 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
875
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
876
		$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
877
 
878
		$response = $this->Socket->request($request);
879
		$this->assertEquals(0, $this->Socket->request['redirect']);
880
		$this->assertEquals(302, $response->code);
881
		$this->assertEquals('http://localhost/anotheruri', $response->getHeader('Location'));
882
	}
883
 
884
/**
885
 * testProxy method
886
 *
887
 * @return void
888
 */
889
	public function testProxy() {
890
		$this->Socket->reset();
891
		$this->Socket->expects($this->any())->method('connect')->will($this->returnValue(true));
892
		$this->Socket->expects($this->any())->method('read')->will($this->returnValue(false));
893
 
894
		$this->Socket->configProxy('proxy.server', 123);
895
		$expected = "GET http://www.cakephp.org/ HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n";
896
		$this->Socket->request('http://www.cakephp.org/');
897
		$this->assertEquals($expected, $this->Socket->request['raw']);
898
		$this->assertEquals('proxy.server', $this->Socket->config['host']);
899
		$this->assertEquals(123, $this->Socket->config['port']);
900
		$expected = array(
901
			'host' => 'proxy.server',
902
			'port' => 123,
903
			'method' => null,
904
			'user' => null,
905
			'pass' => null
906
		);
907
		$this->assertEquals($expected, $this->Socket->request['proxy']);
908
 
909
		$expected = "GET http://www.cakephp.org/bakery HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n\r\n";
910
		$this->Socket->request('/bakery');
911
		$this->assertEquals($expected, $this->Socket->request['raw']);
912
		$this->assertEquals('proxy.server', $this->Socket->config['host']);
913
		$this->assertEquals(123, $this->Socket->config['port']);
914
		$expected = array(
915
			'host' => 'proxy.server',
916
			'port' => 123,
917
			'method' => null,
918
			'user' => null,
919
			'pass' => null
920
		);
921
		$this->assertEquals($expected, $this->Socket->request['proxy']);
922
 
923
		$expected = "GET http://www.cakephp.org/ HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nProxy-Authorization: Test mark.secret\r\n\r\n";
924
		$this->Socket->configProxy('proxy.server', 123, 'Test', 'mark', 'secret');
925
		$this->Socket->request('http://www.cakephp.org/');
926
		$this->assertEquals($expected, $this->Socket->request['raw']);
927
		$this->assertEquals('proxy.server', $this->Socket->config['host']);
928
		$this->assertEquals(123, $this->Socket->config['port']);
929
		$expected = array(
930
			'host' => 'proxy.server',
931
			'port' => 123,
932
			'method' => 'Test',
933
			'user' => 'mark',
934
			'pass' => 'secret'
935
		);
936
		$this->assertEquals($expected, $this->Socket->request['proxy']);
937
 
938
		$this->Socket->configAuth('Test', 'login', 'passwd');
939
		$expected = "GET http://www.cakephp.org/ HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nProxy-Authorization: Test mark.secret\r\nAuthorization: Test login.passwd\r\n\r\n";
940
		$this->Socket->request('http://www.cakephp.org/');
941
		$this->assertEquals($expected, $this->Socket->request['raw']);
942
		$expected = array(
943
			'host' => 'proxy.server',
944
			'port' => 123,
945
			'method' => 'Test',
946
			'user' => 'mark',
947
			'pass' => 'secret'
948
		);
949
		$this->assertEquals($expected, $this->Socket->request['proxy']);
950
		$expected = array(
951
			'Test' => array(
952
				'user' => 'login',
953
				'pass' => 'passwd'
954
			)
955
		);
956
		$this->assertEquals($expected, $this->Socket->request['auth']);
957
	}
958
 
959
/**
960
 * testUrl method
961
 *
962
 * @return void
963
 */
964
	public function testUrl() {
965
		$this->Socket->reset(true);
966
 
967
		$this->assertEquals(false, $this->Socket->url(true));
968
 
969
		$url = $this->Socket->url('www.cakephp.org');
970
		$this->assertEquals('http://www.cakephp.org/', $url);
971
 
972
		$url = $this->Socket->url('https://www.cakephp.org/posts/add');
973
		$this->assertEquals('https://www.cakephp.org/posts/add', $url);
974
		$url = $this->Socket->url('http://www.cakephp/search?q=socket', '/%path?%query');
975
		$this->assertEquals('/search?q=socket', $url);
976
 
977
		$this->Socket->config['request']['uri']['host'] = 'bakery.cakephp.org';
978
		$url = $this->Socket->url();
979
		$this->assertEquals('http://bakery.cakephp.org/', $url);
980
 
981
		$this->Socket->configUri('http://www.cakephp.org');
982
		$url = $this->Socket->url('/search?q=bar');
983
		$this->assertEquals('http://www.cakephp.org/search?q=bar', $url);
984
 
985
		$url = $this->Socket->url(array('host' => 'www.foobar.org', 'query' => array('q' => 'bar')));
986
		$this->assertEquals('http://www.foobar.org/?q=bar', $url);
987
 
988
		$url = $this->Socket->url(array('path' => '/supersearch', 'query' => array('q' => 'bar')));
989
		$this->assertEquals('http://www.cakephp.org/supersearch?q=bar', $url);
990
 
991
		$this->Socket->configUri('http://www.google.com');
992
		$url = $this->Socket->url('/search?q=socket');
993
		$this->assertEquals('http://www.google.com/search?q=socket', $url);
994
 
995
		$url = $this->Socket->url();
996
		$this->assertEquals('http://www.google.com/', $url);
997
 
998
		$this->Socket->configUri('https://www.google.com');
999
		$url = $this->Socket->url('/search?q=socket');
1000
		$this->assertEquals('https://www.google.com/search?q=socket', $url);
1001
 
1002
		$this->Socket->reset();
1003
		$this->Socket->configUri('www.google.com:443');
1004
		$url = $this->Socket->url('/search?q=socket');
1005
		$this->assertEquals('https://www.google.com/search?q=socket', $url);
1006
 
1007
		$this->Socket->reset();
1008
		$this->Socket->configUri('www.google.com:8080');
1009
		$url = $this->Socket->url('/search?q=socket');
1010
		$this->assertEquals('http://www.google.com:8080/search?q=socket', $url);
1011
	}
1012
 
1013
/**
1014
 * testGet method
1015
 *
1016
 * @return void
1017
 */
1018
	public function testGet() {
1019
		$this->RequestSocket->reset();
1020
 
1021
		$this->RequestSocket->expects($this->at(0))
1022
			->method('request')
1023
			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/'));
1024
 
1025
		$this->RequestSocket->expects($this->at(1))
1026
			->method('request')
1027
			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'));
1028
 
1029
		$this->RequestSocket->expects($this->at(2))
1030
			->method('request')
1031
			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'));
1032
 
1033
		$this->RequestSocket->expects($this->at(3))
1034
			->method('request')
1035
			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=23&foobar=42'));
1036
 
1037
		$this->RequestSocket->expects($this->at(4))
1038
			->method('request')
1039
			->with(array('method' => 'GET', 'uri' => 'http://www.google.com/', 'version' => '1.0'));
1040
 
1041
		$this->RequestSocket->expects($this->at(5))
1042
			->method('request')
1043
			->with(array('method' => 'GET', 'uri' => 'https://secure.example.com/test.php?one=two'));
1044
 
1045
		$this->RequestSocket->expects($this->at(6))
1046
			->method('request')
1047
			->with(array('method' => 'GET', 'uri' => 'https://example.com/oauth/access?clientid=123&redirect_uri=http%3A%2F%2Fexample.com&code=456'));
1048
 
1049
		$this->RequestSocket->get('http://www.google.com/');
1050
		$this->RequestSocket->get('http://www.google.com/', array('foo' => 'bar'));
1051
		$this->RequestSocket->get('http://www.google.com/', 'foo=bar');
1052
		$this->RequestSocket->get('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23'));
1053
		$this->RequestSocket->get('http://www.google.com/', null, array('version' => '1.0'));
1054
		$this->RequestSocket->get('https://secure.example.com/test.php', array('one' => 'two'));
1055
		$this->RequestSocket->get('https://example.com/oauth/access', array(
1056
			'clientid' => '123',
1057
			'redirect_uri' => 'http://example.com',
1058
			'code' => 456
1059
		));
1060
	}
1061
 
1062
/**
1063
 * Test the head method
1064
 *
1065
 * @return void
1066
 */
1067
	public function testHead() {
1068
		$this->RequestSocket->reset();
1069
		$this->RequestSocket->expects($this->at(0))
1070
			->method('request')
1071
			->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/'));
1072
 
1073
		$this->RequestSocket->expects($this->at(1))
1074
			->method('request')
1075
			->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=bar'));
1076
 
1077
		$this->RequestSocket->expects($this->at(2))
1078
			->method('request')
1079
			->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=bar'));
1080
 
1081
		$this->RequestSocket->expects($this->at(3))
1082
			->method('request')
1083
			->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/?foo=23&foobar=42'));
1084
 
1085
		$this->RequestSocket->expects($this->at(4))
1086
			->method('request')
1087
			->with(array('method' => 'HEAD', 'uri' => 'http://www.google.com/', 'version' => '1.0'));
1088
 
1089
		$this->RequestSocket->expects($this->at(5))
1090
			->method('request')
1091
			->with(array('method' => 'HEAD', 'uri' => 'https://secure.example.com/test.php?one=two'));
1092
 
1093
		$this->RequestSocket->expects($this->at(6))
1094
			->method('request')
1095
			->with(array('method' => 'HEAD', 'uri' => 'https://example.com/oauth/access?clientid=123&redirect_uri=http%3A%2F%2Fexample.com&code=456'));
1096
 
1097
		$this->RequestSocket->head('http://www.google.com/');
1098
		$this->RequestSocket->head('http://www.google.com/', array('foo' => 'bar'));
1099
		$this->RequestSocket->head('http://www.google.com/', 'foo=bar');
1100
		$this->RequestSocket->head('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23'));
1101
		$this->RequestSocket->head('http://www.google.com/', null, array('version' => '1.0'));
1102
		$this->RequestSocket->head('https://secure.example.com/test.php', array('one' => 'two'));
1103
		$this->RequestSocket->head('https://example.com/oauth/access', array(
1104
			'clientid' => '123',
1105
			'redirect_uri' => 'http://example.com',
1106
			'code' => 456
1107
		));
1108
	}
1109
 
1110
/**
1111
 * Test authentication
1112
 *
1113
 * @return void
1114
 */
1115
	public function testAuth() {
1116
		$this->Socket->get('http://mark:secret@example.com/test');
1117
		$this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
1118
 
1119
		$this->Socket->configAuth(false);
1120
		$this->Socket->get('http://example.com/test');
1121
		$this->assertFalse(strpos($this->Socket->request['header'], 'Authorization:'));
1122
 
1123
		$this->Socket->configAuth('Test', 'mark', 'passwd');
1124
		$this->Socket->get('http://example.com/test');
1125
		$this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Test mark.passwd') !== false);
1126
 
1127
		$this->Socket->configAuth(false);
1128
		$this->Socket->request(array(
1129
			'method' => 'GET',
1130
			'uri' => 'http://example.com/test',
1131
			'auth' => array(
1132
				'method' => 'Basic',
1133
				'user' => 'joel',
1134
				'pass' => 'hunter2'
1135
			)
1136
		));
1137
		$this->assertEquals($this->Socket->request['auth'], array('Basic' => array('user' => 'joel', 'pass' => 'hunter2')));
1138
		$this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic am9lbDpodW50ZXIy') !== false);
1139
 
1140
		$this->Socket->configAuth('Basic', 'mark', 'password');
1141
		$this->Socket->request(array(
1142
			'method' => 'GET',
1143
			'uri' => 'http://example.com/test',
1144
			'header' => array(
1145
				'Authorization' => 'OtherAuth Hi.There'
1146
			)
1147
		));
1148
		$this->assertPattern('/Authorization: OtherAuth Hi\.There/m', $this->Socket->request['header']);
1149
	}
1150
 
1151
/**
1152
 * test that two consecutive get() calls reset the authentication credentials.
1153
 *
1154
 * @return void
1155
 */
1156
	public function testConsecutiveGetResetsAuthCredentials() {
1157
		$this->Socket->get('http://mark:secret@example.com/test');
1158
		$this->assertEquals('mark', $this->Socket->request['uri']['user']);
1159
		$this->assertEquals('secret', $this->Socket->request['uri']['pass']);
1160
		$this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
1161
 
1162
		$this->Socket->get('/test2');
1163
		$this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
1164
 
1165
		$this->Socket->request(array(
1166
			'method' => 'GET',
1167
			'uri' => 'http://example.com/test',
1168
			'header' => array(
1169
				'Authorization' => 'OtherAuth Hi.There'
1170
			)
1171
		));
1172
		$this->assertPattern('/Authorization: OtherAuth Hi\.There/m', $this->Socket->request['header']);
1173
 
1174
		$this->Socket->get('/test3');
1175
		$this->assertTrue(strpos($this->Socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
1176
	}
1177
 
1178
/**
1179
 * testPostPutDelete method
1180
 *
1181
 * @return void
1182
 */
1183
	public function testPost() {
1184
		$this->RequestSocket->reset();
1185
		$this->RequestSocket->expects($this->at(0))
1186
			->method('request')
1187
			->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array()));
1188
 
1189
		$this->RequestSocket->expects($this->at(1))
1190
			->method('request')
1191
			->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
1192
 
1193
		$this->RequestSocket->expects($this->at(2))
1194
			->method('request')
1195
			->with(array('method' => 'POST', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
1196
 
1197
		$this->RequestSocket->post('http://www.google.com/');
1198
		$this->RequestSocket->post('http://www.google.com/', array('Foo' => 'bar'));
1199
		$this->RequestSocket->post('http://www.google.com/', null, array('line' => 'Hey Server'));
1200
	}
1201
 
1202
/**
1203
 * testPut
1204
 *
1205
 * @return void
1206
 */
1207
	public function testPut() {
1208
		$this->RequestSocket->reset();
1209
		$this->RequestSocket->expects($this->at(0))
1210
			->method('request')
1211
			->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array()));
1212
 
1213
		$this->RequestSocket->expects($this->at(1))
1214
			->method('request')
1215
			->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
1216
 
1217
		$this->RequestSocket->expects($this->at(2))
1218
			->method('request')
1219
			->with(array('method' => 'PUT', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
1220
 
1221
		$this->RequestSocket->put('http://www.google.com/');
1222
		$this->RequestSocket->put('http://www.google.com/', array('Foo' => 'bar'));
1223
		$this->RequestSocket->put('http://www.google.com/', null, array('line' => 'Hey Server'));
1224
	}
1225
 
1226
/**
1227
 * testPatch
1228
 *
1229
 * @return void
1230
 */
1231
	public function testPatch() {
1232
		$this->RequestSocket->reset();
1233
		$this->RequestSocket->expects($this->at(0))
1234
			->method('request')
1235
			->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => array()));
1236
 
1237
		$this->RequestSocket->expects($this->at(1))
1238
			->method('request')
1239
			->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
1240
 
1241
		$this->RequestSocket->expects($this->at(2))
1242
			->method('request')
1243
			->with(array('method' => 'PATCH', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
1244
 
1245
		$this->RequestSocket->patch('http://www.google.com/');
1246
		$this->RequestSocket->patch('http://www.google.com/', array('Foo' => 'bar'));
1247
		$this->RequestSocket->patch('http://www.google.com/', null, array('line' => 'Hey Server'));
1248
	}
1249
 
1250
/**
1251
 * testDelete
1252
 *
1253
 * @return void
1254
 */
1255
	public function testDelete() {
1256
		$this->RequestSocket->reset();
1257
		$this->RequestSocket->expects($this->at(0))
1258
			->method('request')
1259
			->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array()));
1260
 
1261
		$this->RequestSocket->expects($this->at(1))
1262
			->method('request')
1263
			->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')));
1264
 
1265
		$this->RequestSocket->expects($this->at(2))
1266
			->method('request')
1267
			->with(array('method' => 'DELETE', 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'));
1268
 
1269
		$this->RequestSocket->delete('http://www.google.com/');
1270
		$this->RequestSocket->delete('http://www.google.com/', array('Foo' => 'bar'));
1271
		$this->RequestSocket->delete('http://www.google.com/', null, array('line' => 'Hey Server'));
1272
	}
1273
 
1274
/**
1275
 * testBuildRequestLine method
1276
 *
1277
 * @return void
1278
 */
1279
	public function testBuildRequestLine() {
1280
		$this->Socket->reset();
1281
 
1282
		$this->Socket->quirksMode = true;
1283
		$r = $this->Socket->buildRequestLine('Foo');
1284
		$this->assertEquals('Foo', $r);
1285
		$this->Socket->quirksMode = false;
1286
 
1287
		$r = $this->Socket->buildRequestLine(true);
1288
		$this->assertEquals(false, $r);
1289
 
1290
		$r = $this->Socket->buildRequestLine(array('foo' => 'bar', 'method' => 'foo'));
1291
		$this->assertEquals(false, $r);
1292
 
1293
		$r = $this->Socket->buildRequestLine(array('method' => 'GET', 'uri' => 'http://www.cakephp.org/search?q=socket'));
1294
		$this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
1295
 
1296
		$request = array(
1297
			'method' => 'GET',
1298
			'uri' => array(
1299
				'path' => '/search',
1300
				'query' => array('q' => 'socket')
1301
			)
1302
		);
1303
		$r = $this->Socket->buildRequestLine($request);
1304
		$this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
1305
 
1306
		unset($request['method']);
1307
		$r = $this->Socket->buildRequestLine($request);
1308
		$this->assertEquals("GET /search?q=socket HTTP/1.1\r\n", $r);
1309
 
1310
		$request = array('method' => 'OPTIONS', 'uri' => '*');
1311
		$r = $this->Socket->buildRequestLine($request);
1312
		$this->assertEquals("OPTIONS * HTTP/1.1\r\n", $r);
1313
 
1314
		$request['method'] = 'GET';
1315
		$this->Socket->quirksMode = true;
1316
		$r = $this->Socket->buildRequestLine($request);
1317
		$this->assertEquals("GET * HTTP/1.1\r\n", $r);
1318
 
1319
		$r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
1320
		$this->assertEquals("GET * HTTP/1.1\r\n", $r);
1321
 
1322
		$request = array(
1323
			'version' => '1.0',
1324
			'method' => 'GET',
1325
			'uri' => array(
1326
				'path' => '/search',
1327
				'query' => array('q' => 'socket')
1328
			)
1329
		);
1330
		$r = $this->Socket->buildRequestLine($request);
1331
		$this->assertEquals("GET /search?q=socket HTTP/1.0\r\n", $r);
1332
	}
1333
 
1334
/**
1335
 * testBadBuildRequestLine method
1336
 *
1337
 * @expectedException SocketException
1338
 * @return void
1339
 */
1340
	public function testBadBuildRequestLine() {
1341
		$this->Socket->buildRequestLine('Foo');
1342
	}
1343
 
1344
/**
1345
 * testBadBuildRequestLine2 method
1346
 *
1347
 * @expectedException SocketException
1348
 * @return void
1349
 */
1350
	public function testBadBuildRequestLine2() {
1351
		$this->Socket->buildRequestLine("GET * HTTP/1.1\r\n");
1352
	}
1353
 
1354
/**
1355
 * Asserts that HttpSocket::parseUri is working properly
1356
 *
1357
 * @return void
1358
 */
1359
	public function testParseUri() {
1360
		$this->Socket->reset();
1361
 
1362
		$uri = $this->Socket->parseUri(array('invalid' => 'uri-string'));
1363
		$this->assertEquals(false, $uri);
1364
 
1365
		$uri = $this->Socket->parseUri(array('invalid' => 'uri-string'), array('host' => 'somehost'));
1366
		$this->assertEquals(array('host' => 'somehost', 'invalid' => 'uri-string'), $uri);
1367
 
1368
		$uri = $this->Socket->parseUri(false);
1369
		$this->assertEquals(false, $uri);
1370
 
1371
		$uri = $this->Socket->parseUri('/my-cool-path');
1372
		$this->assertEquals(array('path' => '/my-cool-path'), $uri);
1373
 
1374
		$uri = $this->Socket->parseUri('http://bob:foo123@www.cakephp.org:40/search?q=dessert#results');
1375
		$this->assertEquals($uri, array(
1376
			'scheme' => 'http',
1377
			'host' => 'www.cakephp.org',
1378
			'port' => 40,
1379
			'user' => 'bob',
1380
			'pass' => 'foo123',
1381
			'path' => '/search',
1382
			'query' => array('q' => 'dessert'),
1383
			'fragment' => 'results'
1384
		));
1385
 
1386
		$uri = $this->Socket->parseUri('http://www.cakephp.org/');
1387
		$this->assertEquals($uri, array(
1388
			'scheme' => 'http',
1389
			'host' => 'www.cakephp.org',
1390
			'path' => '/'
1391
		));
1392
 
1393
		$uri = $this->Socket->parseUri('http://www.cakephp.org', true);
1394
		$this->assertEquals($uri, array(
1395
			'scheme' => 'http',
1396
			'host' => 'www.cakephp.org',
1397
			'port' => 80,
1398
			'user' => null,
1399
			'pass' => null,
1400
			'path' => '/',
1401
			'query' => array(),
1402
			'fragment' => null
1403
		));
1404
 
1405
		$uri = $this->Socket->parseUri('https://www.cakephp.org', true);
1406
		$this->assertEquals($uri, array(
1407
			'scheme' => 'https',
1408
			'host' => 'www.cakephp.org',
1409
			'port' => 443,
1410
			'user' => null,
1411
			'pass' => null,
1412
			'path' => '/',
1413
			'query' => array(),
1414
			'fragment' => null
1415
		));
1416
 
1417
		$uri = $this->Socket->parseUri('www.cakephp.org:443/query?foo', true);
1418
		$this->assertEquals($uri, array(
1419
			'scheme' => 'https',
1420
			'host' => 'www.cakephp.org',
1421
			'port' => 443,
1422
			'user' => null,
1423
			'pass' => null,
1424
			'path' => '/query',
1425
			'query' => array('foo' => ""),
1426
			'fragment' => null
1427
		));
1428
 
1429
		$uri = $this->Socket->parseUri('http://www.cakephp.org', array('host' => 'piephp.org', 'user' => 'bob', 'fragment' => 'results'));
1430
		$this->assertEquals($uri, array(
1431
			'host' => 'www.cakephp.org',
1432
			'user' => 'bob',
1433
			'fragment' => 'results',
1434
			'scheme' => 'http'
1435
		));
1436
 
1437
		$uri = $this->Socket->parseUri('https://www.cakephp.org', array('scheme' => 'http', 'port' => 23));
1438
		$this->assertEquals($uri, array(
1439
			'scheme' => 'https',
1440
			'port' => 23,
1441
			'host' => 'www.cakephp.org'
1442
		));
1443
 
1444
		$uri = $this->Socket->parseUri('www.cakephp.org:59', array('scheme' => array('http', 'https'), 'port' => 80));
1445
		$this->assertEquals($uri, array(
1446
			'scheme' => 'http',
1447
			'port' => 59,
1448
			'host' => 'www.cakephp.org'
1449
		));
1450
 
1451
		$uri = $this->Socket->parseUri(array('scheme' => 'http', 'host' => 'www.google.com', 'port' => 8080), array('scheme' => array('http', 'https'), 'host' => 'www.google.com', 'port' => array(80, 443)));
1452
		$this->assertEquals($uri, array(
1453
			'scheme' => 'http',
1454
			'host' => 'www.google.com',
1455
			'port' => 8080
1456
		));
1457
 
1458
		$uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2%3Dvalue3');
1459
		$this->assertEquals($uri, array(
1460
			'scheme' => 'http',
1461
			'host' => 'www.cakephp.org',
1462
			'path' => '/',
1463
			'query' => array(
1464
				'param1' => 'value1',
1465
				'param2' => 'value2=value3'
1466
			)
1467
		));
1468
 
1469
		$uri = $this->Socket->parseUri('http://www.cakephp.org/?param1=value1&param2=value2=value3');
1470
		$this->assertEquals($uri, array(
1471
			'scheme' => 'http',
1472
			'host' => 'www.cakephp.org',
1473
			'path' => '/',
1474
			'query' => array(
1475
				'param1' => 'value1',
1476
				'param2' => 'value2=value3'
1477
			)
1478
		));
1479
	}
1480
 
1481
/**
1482
 * Tests that HttpSocket::buildUri can turn all kinds of uri arrays (and strings) into fully or partially qualified URI's
1483
 *
1484
 * @return void
1485
 */
1486
	public function testBuildUri() {
1487
		$this->Socket->reset();
1488
 
1489
		$r = $this->Socket->buildUri(true);
1490
		$this->assertEquals(false, $r);
1491
 
1492
		$r = $this->Socket->buildUri('foo.com');
1493
		$this->assertEquals('http://foo.com/', $r);
1494
 
1495
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'));
1496
		$this->assertEquals('http://www.cakephp.org/', $r);
1497
 
1498
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https'));
1499
		$this->assertEquals('https://www.cakephp.org/', $r);
1500
 
1501
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'port' => 23));
1502
		$this->assertEquals('http://www.cakephp.org:23/', $r);
1503
 
1504
		$r = $this->Socket->buildUri(array('path' => 'www.google.com/search', 'query' => 'q=cakephp'));
1505
		$this->assertEquals('http://www.google.com/search?q=cakephp', $r);
1506
 
1507
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'scheme' => 'https', 'port' => 79));
1508
		$this->assertEquals('https://www.cakephp.org:79/', $r);
1509
 
1510
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => 'foo'));
1511
		$this->assertEquals('http://www.cakephp.org/foo', $r);
1512
 
1513
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/foo'));
1514
		$this->assertEquals('http://www.cakephp.org/foo', $r);
1515
 
1516
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'path' => '/search', 'query' => array('q' => 'HttpSocket')));
1517
		$this->assertEquals('http://www.cakephp.org/search?q=HttpSocket', $r);
1518
 
1519
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'));
1520
		$this->assertEquals('http://www.cakephp.org/#bar', $r);
1521
 
1522
		$r = $this->Socket->buildUri(array(
1523
			'scheme' => 'https',
1524
			'host' => 'www.cakephp.org',
1525
			'port' => 25,
1526
			'user' => 'bob',
1527
			'pass' => 'secret',
1528
			'path' => '/cool',
1529
			'query' => array('foo' => 'bar'),
1530
			'fragment' => 'comment'
1531
		));
1532
		$this->assertEquals('https://bob:secret@www.cakephp.org:25/cool?foo=bar#comment', $r);
1533
 
1534
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org', 'fragment' => 'bar'), '%fragment?%host');
1535
		$this->assertEquals('bar?www.cakephp.org', $r);
1536
 
1537
		$r = $this->Socket->buildUri(array('host' => 'www.cakephp.org'), '%fragment???%host');
1538
		$this->assertEquals('???www.cakephp.org', $r);
1539
 
1540
		$r = $this->Socket->buildUri(array('path' => '*'), '/%path?%query');
1541
		$this->assertEquals('*', $r);
1542
 
1543
		$r = $this->Socket->buildUri(array('scheme' => 'foo', 'host' => 'www.cakephp.org'));
1544
		$this->assertEquals('foo://www.cakephp.org:80/', $r);
1545
	}
1546
 
1547
/**
1548
 * Asserts that HttpSocket::parseQuery is working properly
1549
 *
1550
 * @return void
1551
 */
1552
	public function testParseQuery() {
1553
		$this->Socket->reset();
1554
 
1555
		$query = $this->Socket->parseQuery(array('framework' => 'cakephp'));
1556
		$this->assertEquals(array('framework' => 'cakephp'), $query);
1557
 
1558
		$query = $this->Socket->parseQuery('');
1559
		$this->assertEquals(array(), $query);
1560
 
1561
		$query = $this->Socket->parseQuery('framework=cakephp');
1562
		$this->assertEquals(array('framework' => 'cakephp'), $query);
1563
 
1564
		$query = $this->Socket->parseQuery('?framework=cakephp');
1565
		$this->assertEquals(array('framework' => 'cakephp'), $query);
1566
 
1567
		$query = $this->Socket->parseQuery('a&b&c');
1568
		$this->assertEquals(array('a' => '', 'b' => '', 'c' => ''), $query);
1569
 
1570
		$query = $this->Socket->parseQuery('value=12345');
1571
		$this->assertEquals(array('value' => '12345'), $query);
1572
 
1573
		$query = $this->Socket->parseQuery('a[0]=foo&a[1]=bar&a[2]=cake');
1574
		$this->assertEquals(array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')), $query);
1575
 
1576
		$query = $this->Socket->parseQuery('a[]=foo&a[]=bar&a[]=cake');
1577
		$this->assertEquals(array('a' => array(0 => 'foo', 1 => 'bar', 2 => 'cake')), $query);
1578
 
1579
		$query = $this->Socket->parseQuery('a[][]=foo&a[][]=bar&a[][]=cake');
1580
		$expectedQuery = array(
1581
			'a' => array(
1582
 
1583
 
1584
				),
1585
				1 => array(
1586
 
1587
				),
1588
				array(
1589
 
1590
				)
1591
			)
1592
		);
1593
		$this->assertEquals($expectedQuery, $query);
1594
 
1595
		$query = $this->Socket->parseQuery('a[][]=foo&a[bar]=php&a[][]=bar&a[][]=cake');
1596
		$expectedQuery = array(
1597
			'a' => array(
1598
				array('foo'),
1599
				'bar' => 'php',
1600
				array('bar'),
1601
				array('cake')
1602
			)
1603
		);
1604
		$this->assertEquals($expectedQuery, $query);
1605
 
1606
		$query = $this->Socket->parseQuery('user[]=jim&user[3]=tom&user[]=bob');
1607
		$expectedQuery = array(
1608
			'user' => array(
1609
 
1610
				3 => 'tom',
1611
				4 => 'bob'
1612
			)
1613
		);
1614
		$this->assertEquals($expectedQuery, $query);
1615
 
1616
		$queryStr = 'user[0]=foo&user[0][items][]=foo&user[0][items][]=bar&user[][name]=jim&user[1][items][personal][]=book&user[1][items][personal][]=pen&user[1][items][]=ball&user[count]=2&empty';
1617
		$query = $this->Socket->parseQuery($queryStr);
1618
		$expectedQuery = array(
1619
			'user' => array(
1620
 
1621
					'items' => array(
1622
						'foo',
1623
						'bar'
1624
					)
1625
				),
1626
				1 => array(
1627
					'name' => 'jim',
1628
					'items' => array(
1629
						'personal' => array(
1630
							'book',
1631
							'pen'
1632
						),
1633
						'ball'
1634
					)
1635
				),
1636
				'count' => '2'
1637
			),
1638
			'empty' => ''
1639
		);
1640
		$this->assertEquals($expectedQuery, $query);
1641
 
1642
		$query = 'openid.ns=example.com&foo=bar&foo=baz';
1643
		$result = $this->Socket->parseQuery($query);
1644
		$expected = array(
1645
			'openid.ns' => 'example.com',
1646
			'foo' => array('bar', 'baz')
1647
		);
1648
		$this->assertEquals($expected, $result);
1649
	}
1650
 
1651
/**
1652
 * Tests that HttpSocket::buildHeader can turn a given $header array into a proper header string according to
1653
 * HTTP 1.1 specs.
1654
 *
1655
 * @return void
1656
 */
1657
	public function testBuildHeader() {
1658
		$this->Socket->reset();
1659
 
1660
		$r = $this->Socket->buildHeader(true);
1661
		$this->assertEquals(false, $r);
1662
 
1663
		$r = $this->Socket->buildHeader('My raw header');
1664
		$this->assertEquals('My raw header', $r);
1665
 
1666
		$r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org'));
1667
		$this->assertEquals("Host: www.cakephp.org\r\n", $r);
1668
 
1669
		$r = $this->Socket->buildHeader(array('Host' => 'www.cakephp.org', 'Connection' => 'Close'));
1670
		$this->assertEquals("Host: www.cakephp.org\r\nConnection: Close\r\n", $r);
1671
 
1672
		$r = $this->Socket->buildHeader(array('People' => array('Bob', 'Jim', 'John')));
1673
		$this->assertEquals("People: Bob,Jim,John\r\n", $r);
1674
 
1675
		$r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\nMulti Line field"));
1676
		$this->assertEquals("Multi-Line-Field: This is my\r\n Multi Line field\r\n", $r);
1677
 
1678
		$r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n Multi Line field"));
1679
		$this->assertEquals("Multi-Line-Field: This is my\r\n Multi Line field\r\n", $r);
1680
 
1681
		$r = $this->Socket->buildHeader(array('Multi-Line-Field' => "This is my\r\n\tMulti Line field"));
1682
		$this->assertEquals("Multi-Line-Field: This is my\r\n\tMulti Line field\r\n", $r);
1683
 
1684
		$r = $this->Socket->buildHeader(array('Test@Field' => "My value"));
1685
		$this->assertEquals("Test\"@\"Field: My value\r\n", $r);
1686
	}
1687
 
1688
/**
1689
 * testBuildCookies method
1690
 *
1691
 * @return void
1692
 */
1693
	public function testBuildCookies() {
1694
		$cookies = array(
1695
			'foo' => array(
1696
				'value' => 'bar'
1697
			),
1698
			'people' => array(
1699
				'value' => 'jim,jack,johnny;',
1700
				'path' => '/accounts'
1701
			),
1702
			'key' => 'value'
1703
		);
1704
		$expect = "Cookie: foo=bar; people=jim,jack,johnny\";\"; key=value\r\n";
1705
		$result = $this->Socket->buildCookies($cookies);
1706
		$this->assertEquals($expect, $result);
1707
	}
1708
 
1709
/**
1710
 * Tests that HttpSocket::_tokenEscapeChars() returns the right characters.
1711
 *
1712
 * @return void
1713
 */
1714
	public function testTokenEscapeChars() {
1715
		$this->Socket->reset();
1716
 
1717
		$expected = array(
1718
			'\x22', '\x28', '\x29', '\x3c', '\x3e', '\x40', '\x2c', '\x3b', '\x3a', '\x5c', '\x2f', '\x5b', '\x5d', '\x3f', '\x3d', '\x7b',
1719
			'\x7d', '\x20', '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d',
1720
			'\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d',
1721
			'\x1e', '\x1f', '\x7f'
1722
		);
1723
		$r = $this->Socket->tokenEscapeChars();
1724
		$this->assertEquals($expected, $r);
1725
 
1726
		foreach ($expected as $key => $char) {
1727
			$expected[$key] = chr(hexdec(substr($char, 2)));
1728
		}
1729
 
1730
		$r = $this->Socket->tokenEscapeChars(false);
1731
		$this->assertEquals($expected, $r);
1732
	}
1733
 
1734
/**
1735
 * Test that HttpSocket::escapeToken is escaping all characters as described in RFC 2616 (HTTP 1.1 specs)
1736
 *
1737
 * @return void
1738
 */
1739
	public function testEscapeToken() {
1740
		$this->Socket->reset();
1741
 
1742
		$this->assertEquals('Foo', $this->Socket->escapeToken('Foo'));
1743
 
1744
		$escape = $this->Socket->tokenEscapeChars(false);
1745
		foreach ($escape as $char) {
1746
			$token = 'My-special-' . $char . '-Token';
1747
			$escapedToken = $this->Socket->escapeToken($token);
1748
			$expectedToken = 'My-special-"' . $char . '"-Token';
1749
 
1750
			$this->assertEquals($expectedToken, $escapedToken, 'Test token escaping for ASCII ' . ord($char));
1751
		}
1752
 
1753
		$token = 'Extreme-:Token-	-"@-test';
1754
		$escapedToken = $this->Socket->escapeToken($token);
1755
		$expectedToken = 'Extreme-":"Token-"	"-""""@"-test';
1756
		$this->assertEquals($expectedToken, $escapedToken);
1757
	}
1758
 
1759
/**
1760
 * This tests asserts HttpSocket::reset() resets a HttpSocket instance to it's initial state (before Object::__construct
1761
 * got executed)
1762
 *
1763
 * @return void
1764
 */
1765
	public function testReset() {
1766
		$this->Socket->reset();
1767
 
1768
		$initialState = get_class_vars('HttpSocket');
1769
		foreach ($initialState as $property => $value) {
1770
			$this->Socket->{$property} = 'Overwritten';
1771
		}
1772
 
1773
		$return = $this->Socket->reset();
1774
 
1775
		foreach ($initialState as $property => $value) {
1776
			$this->assertEquals($this->Socket->{$property}, $value);
1777
		}
1778
 
1779
		$this->assertEquals(true, $return);
1780
	}
1781
 
1782
/**
1783
 * This tests asserts HttpSocket::reset(false) resets certain HttpSocket properties to their initial state (before
1784
 * Object::__construct got executed).
1785
 *
1786
 * @return void
1787
 */
1788
	public function testPartialReset() {
1789
		$this->Socket->reset();
1790
 
1791
		$partialResetProperties = array('request', 'response');
1792
		$initialState = get_class_vars('HttpSocket');
1793
 
1794
		foreach ($initialState as $property => $value) {
1795
			$this->Socket->{$property} = 'Overwritten';
1796
		}
1797
 
1798
		$return = $this->Socket->reset(false);
1799
 
1800
		foreach ($initialState as $property => $originalValue) {
1801
			if (in_array($property, $partialResetProperties)) {
1802
				$this->assertEquals($this->Socket->{$property}, $originalValue);
1803
			} else {
1804
				$this->assertEquals('Overwritten', $this->Socket->{$property});
1805
			}
1806
		}
1807
		$this->assertEquals(true, $return);
1808
	}
1809
 
1810
/**
1811
 * test configuring the context from the flat keys.
1812
 *
1813
 * @return void
1814
 */
1815
	public function testConfigContext() {
1816
		$this->Socket->reset();
1817
		$this->Socket->request('http://example.com');
1818
		$this->assertTrue($this->Socket->config['context']['ssl']['verify_peer']);
1819
		$this->assertFalse($this->Socket->config['context']['ssl']['allow_self_signed']);
1820
		$this->assertEquals(5, $this->Socket->config['context']['ssl']['verify_depth']);
1821
		$this->assertEquals('example.com', $this->Socket->config['context']['ssl']['CN_match']);
1822
		$this->assertArrayNotHasKey('ssl_verify_peer', $this->Socket->config);
1823
		$this->assertArrayNotHasKey('ssl_allow_self_signed', $this->Socket->config);
1824
		$this->assertArrayNotHasKey('ssl_verify_host', $this->Socket->config);
1825
		$this->assertArrayNotHasKey('ssl_verify_depth', $this->Socket->config);
1826
	}
1827
 
1828
/**
1829
 * Test that requests fail when peer verification fails.
1830
 *
1831
 * @return void
1832
 */
1833
	public function testVerifyPeer() {
1834
		$this->skipIf(!extension_loaded('openssl'), 'OpenSSL is not enabled cannot test SSL.');
1835
		$socket = new HttpSocket();
1836
		try {
1837
			$socket->get('https://tv.eurosport.com/');
1838
			$this->markTestSkipped('Found valid certificate, was expecting invalid certificate.');
1839
		} catch (SocketException $e) {
1840
			$message = $e->getMessage();
1841
			$this->skipIf(strpos($message, 'Invalid HTTP') !== false, 'Invalid HTTP Response received, skipping.');
1842
			$this->assertContains('Peer certificate CN', $message);
1843
			$this->assertContains('Failed to enable crypto', $message);
1844
		}
1845
	}
1846
 
1847
/**
1848
 * Data provider for status codes.
1849
 *
1850
 * @return array
1851
 */
1852
	public function statusProvider() {
1853
		return array(
1854
			array('HTTP/1.1 200 ', '200'),
1855
			array('HTTP/1.1 200    ', '200'),
1856
			array('HTTP/1.1 200', '200'),
1857
			array('HTTP/1.1 200  OK', '200', 'OK'),
1858
			array('HTTP/1.1 404 Not Found', '404', 'Not Found'),
1859
			array('HTTP/1.1 404    Not Found', '404', 'Not Found'),
1860
		);
1861
	}
1862
 
1863
/**
1864
 * test response status parsing
1865
 *
1866
 * @dataProvider statusProvider
1867
 * @return void
1868
 */
1869
	public function testResponseStatusParsing($status, $code, $msg = '') {
1870
		$this->Socket->connected = true;
1871
		$serverResponse = $status . "\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n\r\n<h1>This is a test!</h1>";
1872
		$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
1873
		$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
1874
 
1875
		$response = $this->Socket->request('http://www.cakephp.org/');
1876
		$this->assertInstanceOf('HttpSocketResponse', $response);
1877
		$expected = array(
1878
			'http-version' => 'HTTP/1.1',
1879
			'code' => $code,
1880
			'reason-phrase' => $msg
1881
		);
1882
		$this->assertEquals($expected, $response['status']);
1883
	}
1884
 
1885
}