1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
<?php
// $Id: http_test.php 1505 2007-04-30 23:39:59Z lastcraft $
require_once(dirname(__FILE__) . '/../autorun.php');
require_once(dirname(__FILE__) . '/../encoding.php');
require_once(dirname(__FILE__) . '/../http.php');
require_once(dirname(__FILE__) . '/../socket.php');
require_once(dirname(__FILE__) . '/../cookies.php');
Mock::generate('SimpleSocket');
Mock::generate('SimpleCookieJar');
Mock::generate('SimpleRoute');
Mock::generatePartial('SimpleRoute', 'PartialSimpleRoute', array('_createSocket'));
Mock::generatePartial(
'SimpleProxyRoute',
'PartialSimpleProxyRoute',
array('_createSocket'));
class TestOfDirectRoute extends UnitTestCase {
function testDefaultGetRequest() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("GET /here.html HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 3);
$route = &new PartialSimpleRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html'));
$this->assertReference($route->createConnection('GET', 15), $socket);
}
function testDefaultPostRequest() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("POST /here.html HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 3);
$route = &new PartialSimpleRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html'));
$route->createConnection('POST', 15);
}
function testGetWithPort() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("GET /here.html HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host:81\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 3);
$route = &new PartialSimpleRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleRoute(new SimpleUrl('http://a.valid.host:81/here.html'));
$route->createConnection('GET', 15);
}
function testGetWithParameters() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("GET /here.html?a=1&b=2 HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: a.valid.host\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 3);
$route = &new PartialSimpleRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleRoute(new SimpleUrl('http://a.valid.host/here.html?a=1&b=2'));
$route->createConnection('GET', 15);
}
}
class TestOfProxyRoute extends UnitTestCase {
function testDefaultGet() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 3);
$route = &new PartialSimpleProxyRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleProxyRoute(
new SimpleUrl('http://a.valid.host/here.html'),
new SimpleUrl('http://my-proxy'));
$route->createConnection('GET', 15);
}
function testDefaultPost() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("POST http://a.valid.host/here.html HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 3);
$route = &new PartialSimpleProxyRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleProxyRoute(
new SimpleUrl('http://a.valid.host/here.html'),
new SimpleUrl('http://my-proxy'));
$route->createConnection('POST', 15);
}
function testGetWithPort() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host:81/here.html HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8081\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 3);
$route = &new PartialSimpleProxyRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleProxyRoute(
new SimpleUrl('http://a.valid.host:81/here.html'),
new SimpleUrl('http://my-proxy:8081'));
$route->createConnection('GET', 15);
}
function testGetWithParameters() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html?a=1&b=2 HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 3);
$route = &new PartialSimpleProxyRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleProxyRoute(
new SimpleUrl('http://a.valid.host/here.html?a=1&b=2'),
new SimpleUrl('http://my-proxy'));
$route->createConnection('GET', 15);
}
function testGetWithAuthentication() {
$encoded = base64_encode('Me:Secret');
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("GET http://a.valid.host/here.html HTTP/1.0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Host: my-proxy:8080\r\n"));
$socket->expectArgumentsAt(2, 'write', array("Proxy-Authorization: Basic $encoded\r\n"));
$socket->expectArgumentsAt(3, 'write', array("Connection: close\r\n"));
$socket->expectCallCount('write', 4);
$route = &new PartialSimpleProxyRoute();
$route->setReturnReference('_createSocket', $socket);
$route->SimpleProxyRoute(
new SimpleUrl('http://a.valid.host/here.html'),
new SimpleUrl('http://my-proxy'),
'Me',
'Secret');
$route->createConnection('GET', 15);
}
}
class TestOfHttpRequest extends UnitTestCase {
function testReadingBadConnection() {
$socket = &new MockSimpleSocket();
$route = &new MockSimpleRoute();
$route->setReturnReference('createConnection', $socket);
$request = &new SimpleHttpRequest($route, new SimpleGetEncoding());
$reponse = &$request->fetch(15);
$this->assertTrue($reponse->isError());
}
function testReadingGoodConnection() {
$socket = &new MockSimpleSocket();
$socket->expectOnce('write', array("\r\n"));
$route = &new MockSimpleRoute();
$route->setReturnReference('createConnection', $socket);
$route->expectArguments('createConnection', array('GET', 15));
$request = &new SimpleHttpRequest($route, new SimpleGetEncoding());
$this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
}
function testWritingAdditionalHeaders() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("My: stuff\r\n"));
$socket->expectArgumentsAt(1, 'write', array("\r\n"));
$socket->expectCallCount('write', 2);
$route = &new MockSimpleRoute();
$route->setReturnReference('createConnection', $socket);
$request = &new SimpleHttpRequest($route, new SimpleGetEncoding());
$request->addHeaderLine('My: stuff');
$request->fetch(15);
}
function testCookieWriting() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("Cookie: a=A\r\n"));
$socket->expectArgumentsAt(1, 'write', array("\r\n"));
$socket->expectCallCount('write', 2);
$route = &new MockSimpleRoute();
$route->setReturnReference('createConnection', $socket);
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'A');
$request = &new SimpleHttpRequest($route, new SimpleGetEncoding());
$request->readCookiesFromJar($jar, new SimpleUrl('/'));
$this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
}
function testMultipleCookieWriting() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("Cookie: a=A;b=B\r\n"));
$route = &new MockSimpleRoute();
$route->setReturnReference('createConnection', $socket);
$jar = new SimpleCookieJar();
$jar->setCookie('a', 'A');
$jar->setCookie('b', 'B');
$request = &new SimpleHttpRequest($route, new SimpleGetEncoding());
$request->readCookiesFromJar($jar, new SimpleUrl('/'));
$request->fetch(15);
}
}
class TestOfHttpPostRequest extends UnitTestCase {
function testReadingBadConnectionCausesErrorBecauseOfDeadSocket() {
$socket = &new MockSimpleSocket();
$route = &new MockSimpleRoute();
$route->setReturnReference('createConnection', $socket);
$request = &new SimpleHttpRequest($route, new SimplePostEncoding());
$reponse = &$request->fetch(15);
$this->assertTrue($reponse->isError());
}
function testReadingGoodConnection() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("Content-Length: 0\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Content-Type: application/x-www-form-urlencoded\r\n"));
$socket->expectArgumentsAt(2, 'write', array("\r\n"));
$socket->expectArgumentsAt(3, 'write', array(""));
$route = &new MockSimpleRoute();
$route->setReturnReference('createConnection', $socket);
$route->expectArguments('createConnection', array('POST', 15));
$request = &new SimpleHttpRequest($route, new SimplePostEncoding());
$this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
}
function testContentHeadersCalculated() {
$socket = &new MockSimpleSocket();
$socket->expectArgumentsAt(0, 'write', array("Content-Length: 3\r\n"));
$socket->expectArgumentsAt(1, 'write', array("Content-Type: application/x-www-form-urlencoded\r\n"));
$socket->expectArgumentsAt(2, 'write', array("\r\n"));
$socket->expectArgumentsAt(3, 'write', array("a=A"));
$route = &new MockSimpleRoute();
$route->setReturnReference('createConnection', $socket);
$route->expectArguments('createConnection', array('POST', 15));
$request = &new SimpleHttpRequest(
$route,
new SimplePostEncoding(array('a' => 'A')));
$this->assertIsA($request->fetch(15), 'SimpleHttpResponse');
}
}
class TestOfHttpHeaders extends UnitTestCase {
function testParseBasicHeaders() {
$headers = new SimpleHttpHeaders(
"HTTP/1.1 200 OK\r\n" .
"Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n" .
"Content-Type: text/plain\r\n" .
"Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\n" .
"Connection: close");
$this->assertIdentical($headers->getHttpVersion(), "1.1");
$this->assertIdentical($headers->getResponseCode(), 200);
$this->assertEqual($headers->getMimeType(), "text/plain");
}
function testNonStandardResponseHeader() {
$headers = new SimpleHttpHeaders(
"HTTP/1.1 302 (HTTP-Version SP Status-Code CRLF)\r\n" .
"Connection: close");
$this->assertIdentical($headers->getResponseCode(), 302);
}
function testCanParseMultipleCookies() {
$jar = &new MockSimpleCookieJar();
$jar->expectAt(0, 'setCookie', array('a', 'aaa', 'host', '/here/', 'Wed, 25 Dec 2002 04:24:20 GMT'));
$jar->expectAt(1, 'setCookie', array('b', 'bbb', 'host', '/', false));
$headers = new SimpleHttpHeaders(
"HTTP/1.1 200 OK\r\n" .
"Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n" .
"Content-Type: text/plain\r\n" .
"Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\n" .
"Set-Cookie: a=aaa; expires=Wed, 25-Dec-02 04:24:20 GMT; path=/here/\r\n" .
"Set-Cookie: b=bbb\r\n" .
"Connection: close");
$headers->writeCookiesToJar($jar, new SimpleUrl('http://host'));
}
function testCanRecogniseRedirect() {
$headers = new SimpleHttpHeaders("HTTP/1.1 301 OK\r\n" .
"Content-Type: text/plain\r\n" .
"Content-Length: 0\r\n" .
"Location: http://www.somewhere-else.com/\r\n" .
"Connection: close");
$this->assertIdentical($headers->getResponseCode(), 301);
$this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com/");
$this->assertTrue($headers->isRedirect());
}
function testCanParseChallenge() {
$headers = new SimpleHttpHeaders("HTTP/1.1 401 Authorization required\r\n" .
"Content-Type: text/plain\r\n" .
"Connection: close\r\n" .
"WWW-Authenticate: Basic realm=\"Somewhere\"");
$this->assertEqual($headers->getAuthentication(), 'Basic');
$this->assertEqual($headers->getRealm(), 'Somewhere');
$this->assertTrue($headers->isChallenge());
}
}
class TestOfHttpResponse extends UnitTestCase {
function testBadRequest() {
$socket = &new MockSimpleSocket();
$socket->setReturnValue('getSent', '');
$response = &new SimpleHttpResponse($socket, new SimpleUrl('here'), new SimpleGetEncoding());
$this->assertTrue($response->isError());
$this->assertPattern('/Nothing fetched/', $response->getError());
$this->assertIdentical($response->getContent(), false);
$this->assertIdentical($response->getSent(), '');
}
function testBadSocketDuringResponse() {
$socket = &new MockSimpleSocket();
$socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\n");
$socket->setReturnValueAt(1, "read", "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
$socket->setReturnValue("read", "");
$socket->setReturnValue('getSent', 'HTTP/1.1 ...');
$response = &new SimpleHttpResponse($socket, new SimpleUrl('here'), new SimpleGetEncoding());
$this->assertTrue($response->isError());
$this->assertEqual($response->getContent(), '');
$this->assertEqual($response->getSent(), 'HTTP/1.1 ...');
}
function testIncompleteHeader() {
$socket = &new MockSimpleSocket();
$socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\n");
$socket->setReturnValueAt(1, "read", "Date: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
$socket->setReturnValueAt(2, "read", "Content-Type: text/plain\r\n");
$socket->setReturnValue("read", "");
$response = &new SimpleHttpResponse($socket, new SimpleUrl('here'), new SimpleGetEncoding());
$this->assertTrue($response->isError());
$this->assertEqual($response->getContent(), "");
}
function testParseOfResponseHeadersWhenChunked() {
$socket = &new MockSimpleSocket();
$socket->setReturnValueAt(0, "read", "HTTP/1.1 200 OK\r\nDate: Mon, 18 Nov 2002 15:50:29 GMT\r\n");
$socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
$socket->setReturnValueAt(2, "read", "Server: Apache/1.3.24 (Win32) PHP/4.2.3\r\nConne");
$socket->setReturnValueAt(3, "read", "ction: close\r\n\r\nthis is a test file\n");
$socket->setReturnValueAt(4, "read", "with two lines in it\n");
$socket->setReturnValue("read", "");
$response = &new SimpleHttpResponse($socket, new SimpleUrl('here'), new SimpleGetEncoding());
$this->assertFalse($response->isError());
$this->assertEqual(
$response->getContent(),
"this is a test file\nwith two lines in it\n");
$headers = $response->getHeaders();
$this->assertIdentical($headers->getHttpVersion(), "1.1");
$this->assertIdentical($headers->getResponseCode(), 200);
$this->assertEqual($headers->getMimeType(), "text/plain");
$this->assertFalse($headers->isRedirect());
$this->assertFalse($headers->getLocation());
}
function testRedirect() {
$socket = &new MockSimpleSocket();
$socket->setReturnValueAt(0, "read", "HTTP/1.1 301 OK\r\n");
$socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
$socket->setReturnValueAt(2, "read", "Location: http://www.somewhere-else.com/\r\n");
$socket->setReturnValueAt(3, "read", "Connection: close\r\n");
$socket->setReturnValueAt(4, "read", "\r\n");
$socket->setReturnValue("read", "");
$response = &new SimpleHttpResponse($socket, new SimpleUrl('here'), new SimpleGetEncoding());
$headers = $response->getHeaders();
$this->assertTrue($headers->isRedirect());
$this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com/");
}
function testRedirectWithPort() {
$socket = &new MockSimpleSocket();
$socket->setReturnValueAt(0, "read", "HTTP/1.1 301 OK\r\n");
$socket->setReturnValueAt(1, "read", "Content-Type: text/plain\r\n");
$socket->setReturnValueAt(2, "read", "Location: http://www.somewhere-else.com:80/\r\n");
$socket->setReturnValueAt(3, "read", "Connection: close\r\n");
$socket->setReturnValueAt(4, "read", "\r\n");
$socket->setReturnValue("read", "");
$response = &new SimpleHttpResponse($socket, new SimpleUrl('here'), new SimpleGetEncoding());
$headers = $response->getHeaders();
$this->assertTrue($headers->isRedirect());
$this->assertEqual($headers->getLocation(), "http://www.somewhere-else.com:80/");
}
}
?>
|