2
// $Id: user_agent_test.php 1509 2007-05-08 22:11:49Z lastcraft $
3
require_once(dirname(__FILE__) . '/../autorun.php');
4
require_once(dirname(__FILE__) . '/../user_agent.php');
5
require_once(dirname(__FILE__) . '/../authentication.php');
6
require_once(dirname(__FILE__) . '/../http.php');
7
require_once(dirname(__FILE__) . '/../encoding.php');
8
Mock::generate('SimpleHttpRequest');
9
Mock::generate('SimpleHttpResponse');
10
Mock::generate('SimpleHttpHeaders');
11
Mock::generatePartial('SimpleUserAgent', 'MockRequestUserAgent', array('_createHttpRequest'));
13
class TestOfFetchingUrlParameters extends UnitTestCase {
16
$this->_headers = &new MockSimpleHttpHeaders();
18
$this->_response = &new MockSimpleHttpResponse();
19
$this->_response->setReturnValue('isError', false);
20
$this->_response->setReturnReference('getHeaders', new MockSimpleHttpHeaders());
22
$this->_request = &new MockSimpleHttpRequest();
23
$this->_request->setReturnReference('fetch', $this->_response);
26
function testGetRequestWithoutIncidentGivesNoErrors() {
27
$url = new SimpleUrl('http://test:secret@this.com/page.html');
28
$url->addRequestParameters(array('a' => 'A', 'b' => 'B'));
30
$agent = &new MockRequestUserAgent();
31
$agent->setReturnReference('_createHttpRequest', $this->_request);
32
$agent->SimpleUserAgent();
34
$response = &$agent->fetchResponse(
35
new SimpleUrl('http://test:secret@this.com/page.html'),
36
new SimpleGetEncoding(array('a' => 'A', 'b' => 'B')));
37
$this->assertFalse($response->isError());
41
class TestOfAdditionalHeaders extends UnitTestCase {
43
function testAdditionalHeaderAddedToRequest() {
44
$response = &new MockSimpleHttpResponse();
45
$response->setReturnReference('getHeaders', new MockSimpleHttpHeaders());
47
$request = &new MockSimpleHttpRequest();
48
$request->setReturnReference('fetch', $response);
51
array('User-Agent: SimpleTest'));
53
$agent = &new MockRequestUserAgent();
54
$agent->setReturnReference('_createHttpRequest', $request);
55
$agent->SimpleUserAgent();
56
$agent->addHeader('User-Agent: SimpleTest');
57
$response = &$agent->fetchResponse(new SimpleUrl('http://this.host/'), new SimpleGetEncoding());
61
class TestOfBrowserCookies extends UnitTestCase {
63
function &_createStandardResponse() {
64
$response = &new MockSimpleHttpResponse();
65
$response->setReturnValue("isError", false);
66
$response->setReturnValue("getContent", "stuff");
67
$response->setReturnReference("getHeaders", new MockSimpleHttpHeaders());
71
function &_createCookieSite($header_lines) {
72
$headers = &new SimpleHttpHeaders($header_lines);
74
$response = &new MockSimpleHttpResponse();
75
$response->setReturnValue("isError", false);
76
$response->setReturnReference("getHeaders", $headers);
77
$response->setReturnValue("getContent", "stuff");
79
$request = &new MockSimpleHttpRequest();
80
$request->setReturnReference("fetch", $response);
84
function &_createMockedRequestUserAgent(&$request) {
85
$agent = &new MockRequestUserAgent();
86
$agent->setReturnReference('_createHttpRequest', $request);
87
$agent->SimpleUserAgent();
91
function testCookieJarIsSentToRequest() {
92
$jar = new SimpleCookieJar();
93
$jar->setCookie('a', 'A');
95
$request = &new MockSimpleHttpRequest();
96
$request->setReturnReference('fetch', $this->_createStandardResponse());
97
$request->expectOnce('readCookiesFromJar', array($jar, '*'));
99
$agent = &$this->_createMockedRequestUserAgent($request);
100
$agent->setCookie('a', 'A');
101
$agent->fetchResponse(
102
new SimpleUrl('http://this.com/this/path/page.html'),
103
new SimpleGetEncoding());
106
function testNoCookieJarIsSentToRequestWhenCookiesAreDisabled() {
107
$request = &new MockSimpleHttpRequest();
108
$request->setReturnReference('fetch', $this->_createStandardResponse());
109
$request->expectNever('readCookiesFromJar');
111
$agent = &$this->_createMockedRequestUserAgent($request);
112
$agent->setCookie('a', 'A');
113
$agent->ignoreCookies();
114
$agent->fetchResponse(
115
new SimpleUrl('http://this.com/this/path/page.html'),
116
new SimpleGetEncoding());
119
function testReadingNewCookie() {
120
$request = &$this->_createCookieSite('Set-cookie: a=AAAA');
121
$agent = &$this->_createMockedRequestUserAgent($request);
122
$agent->fetchResponse(
123
new SimpleUrl('http://this.com/this/path/page.html'),
124
new SimpleGetEncoding());
125
$this->assertEqual($agent->getCookieValue("this.com", "this/path/", "a"), "AAAA");
128
function testIgnoringNewCookieWhenCookiesDisabled() {
129
$request = &$this->_createCookieSite('Set-cookie: a=AAAA');
130
$agent = &$this->_createMockedRequestUserAgent($request);
131
$agent->ignoreCookies();
132
$agent->fetchResponse(
133
new SimpleUrl('http://this.com/this/path/page.html'),
134
new SimpleGetEncoding());
135
$this->assertIdentical($agent->getCookieValue("this.com", "this/path/", "a"), false);
138
function testOverwriteCookieThatAlreadyExists() {
139
$request = &$this->_createCookieSite('Set-cookie: a=AAAA');
140
$agent = &$this->_createMockedRequestUserAgent($request);
141
$agent->setCookie('a', 'A');
142
$agent->fetchResponse(
143
new SimpleUrl('http://this.com/this/path/page.html'),
144
new SimpleGetEncoding());
145
$this->assertEqual($agent->getCookieValue("this.com", "this/path/", "a"), "AAAA");
148
function testClearCookieBySettingExpiry() {
149
$request = &$this->_createCookieSite('Set-cookie: a=b');
150
$agent = &$this->_createMockedRequestUserAgent($request);
152
$agent->setCookie("a", "A", "this/path/", "Wed, 25-Dec-02 04:24:21 GMT");
153
$agent->fetchResponse(
154
new SimpleUrl('http://this.com/this/path/page.html'),
155
new SimpleGetEncoding());
156
$this->assertIdentical(
157
$agent->getCookieValue("this.com", "this/path/", "a"),
159
$agent->restart("Wed, 25-Dec-02 04:24:20 GMT");
160
$this->assertIdentical(
161
$agent->getCookieValue("this.com", "this/path/", "a"),
165
function testAgeingAndClearing() {
166
$request = &$this->_createCookieSite('Set-cookie: a=A; expires=Wed, 25-Dec-02 04:24:21 GMT; path=/this/path');
167
$agent = &$this->_createMockedRequestUserAgent($request);
169
$agent->fetchResponse(
170
new SimpleUrl('http://this.com/this/path/page.html'),
171
new SimpleGetEncoding());
172
$agent->restart("Wed, 25-Dec-02 04:24:20 GMT");
173
$this->assertIdentical(
174
$agent->getCookieValue("this.com", "this/path/", "a"),
176
$agent->ageCookies(2);
177
$agent->restart("Wed, 25-Dec-02 04:24:20 GMT");
178
$this->assertIdentical(
179
$agent->getCookieValue("this.com", "this/path/", "a"),
183
function testReadingIncomingAndSettingNewCookies() {
184
$request = &$this->_createCookieSite('Set-cookie: a=AAA');
185
$agent = &$this->_createMockedRequestUserAgent($request);
187
$this->assertNull($agent->getBaseCookieValue("a", false));
188
$agent->fetchResponse(
189
new SimpleUrl('http://this.com/this/path/page.html'),
190
new SimpleGetEncoding());
191
$agent->setCookie("b", "BBB", "this.com", "this/path/");
193
$agent->getBaseCookieValue("a", new SimpleUrl('http://this.com/this/path/page.html')),
196
$agent->getBaseCookieValue("b", new SimpleUrl('http://this.com/this/path/page.html')),
201
class TestOfHttpRedirects extends UnitTestCase {
203
function &createRedirect($content, $redirect) {
204
$headers = &new MockSimpleHttpHeaders();
205
$headers->setReturnValue('isRedirect', (boolean)$redirect);
206
$headers->setReturnValue('getLocation', $redirect);
208
$response = &new MockSimpleHttpResponse();
209
$response->setReturnValue('getContent', $content);
210
$response->setReturnReference('getHeaders', $headers);
212
$request = &new MockSimpleHttpRequest();
213
$request->setReturnReference('fetch', $response);
217
function testDisabledRedirects() {
218
$agent = &new MockRequestUserAgent();
219
$agent->setReturnReference(
220
'_createHttpRequest',
221
$this->createRedirect('stuff', 'there.html'));
222
$agent->expectOnce('_createHttpRequest');
223
$agent->SimpleUserAgent();
225
$agent->setMaximumRedirects(0);
226
$response = &$agent->fetchResponse(new SimpleUrl('here.html'), new SimpleGetEncoding());
227
$this->assertEqual($response->getContent(), 'stuff');
230
function testSingleRedirect() {
231
$agent = &new MockRequestUserAgent();
232
$agent->setReturnReferenceAt(
234
'_createHttpRequest',
235
$this->createRedirect('first', 'two.html'));
236
$agent->setReturnReferenceAt(
238
'_createHttpRequest',
239
$this->createRedirect('second', 'three.html'));
240
$agent->expectCallCount('_createHttpRequest', 2);
241
$agent->SimpleUserAgent();
243
$agent->setMaximumRedirects(1);
244
$response = &$agent->fetchResponse(new SimpleUrl('one.html'), new SimpleGetEncoding());
245
$this->assertEqual($response->getContent(), 'second');
248
function testDoubleRedirect() {
249
$agent = &new MockRequestUserAgent();
250
$agent->setReturnReferenceAt(
252
'_createHttpRequest',
253
$this->createRedirect('first', 'two.html'));
254
$agent->setReturnReferenceAt(
256
'_createHttpRequest',
257
$this->createRedirect('second', 'three.html'));
258
$agent->setReturnReferenceAt(
260
'_createHttpRequest',
261
$this->createRedirect('third', 'four.html'));
262
$agent->expectCallCount('_createHttpRequest', 3);
263
$agent->SimpleUserAgent();
265
$agent->setMaximumRedirects(2);
266
$response = &$agent->fetchResponse(new SimpleUrl('one.html'), new SimpleGetEncoding());
267
$this->assertEqual($response->getContent(), 'third');
270
function testSuccessAfterRedirect() {
271
$agent = &new MockRequestUserAgent();
272
$agent->setReturnReferenceAt(
274
'_createHttpRequest',
275
$this->createRedirect('first', 'two.html'));
276
$agent->setReturnReferenceAt(
278
'_createHttpRequest',
279
$this->createRedirect('second', false));
280
$agent->setReturnReferenceAt(
282
'_createHttpRequest',
283
$this->createRedirect('third', 'four.html'));
284
$agent->expectCallCount('_createHttpRequest', 2);
285
$agent->SimpleUserAgent();
287
$agent->setMaximumRedirects(2);
288
$response = &$agent->fetchResponse(new SimpleUrl('one.html'), new SimpleGetEncoding());
289
$this->assertEqual($response->getContent(), 'second');
292
function testRedirectChangesPostToGet() {
293
$agent = &new MockRequestUserAgent();
294
$agent->setReturnReferenceAt(
296
'_createHttpRequest',
297
$this->createRedirect('first', 'two.html'));
298
$agent->expectArgumentsAt(0, '_createHttpRequest', array('*', new IsAExpectation('SimplePostEncoding')));
299
$agent->setReturnReferenceAt(
301
'_createHttpRequest',
302
$this->createRedirect('second', 'three.html'));
303
$agent->expectArgumentsAt(1, '_createHttpRequest', array('*', new IsAExpectation('SimpleGetEncoding')));
304
$agent->expectCallCount('_createHttpRequest', 2);
305
$agent->SimpleUserAgent();
306
$agent->setMaximumRedirects(1);
307
$response = &$agent->fetchResponse(new SimpleUrl('one.html'), new SimplePostEncoding());
311
class TestOfBadHosts extends UnitTestCase {
313
function &_createSimulatedBadHost() {
314
$response = &new MockSimpleHttpResponse();
315
$response->setReturnValue('isError', true);
316
$response->setReturnValue('getError', 'Bad socket');
317
$response->setReturnValue('getContent', false);
319
$request = &new MockSimpleHttpRequest();
320
$request->setReturnReference('fetch', $response);
324
function testUntestedHost() {
325
$request = &$this->_createSimulatedBadHost();
327
$agent = &new MockRequestUserAgent();
328
$agent->setReturnReference('_createHttpRequest', $request);
329
$agent->SimpleUserAgent();
331
$response = &$agent->fetchResponse(
332
new SimpleUrl('http://this.host/this/path/page.html'),
333
new SimpleGetEncoding());
334
$this->assertTrue($response->isError());
338
class TestOfAuthorisation extends UnitTestCase {
340
function testAuthenticateHeaderAdded() {
341
$response = &new MockSimpleHttpResponse();
342
$response->setReturnReference('getHeaders', new MockSimpleHttpHeaders());
344
$request = &new MockSimpleHttpRequest();
345
$request->setReturnReference('fetch', $response);
346
$request->expectOnce(
348
array('Authorization: Basic ' . base64_encode('test:secret')));
350
$agent = &new MockRequestUserAgent();
351
$agent->setReturnReference('_createHttpRequest', $request);
352
$agent->SimpleUserAgent();
353
$response = &$agent->fetchResponse(
354
new SimpleUrl('http://test:secret@this.host'),
355
new SimpleGetEncoding());
b'\\ No newline at end of file'