| 1 | <?php |
|---|
| 2 | |
|---|
| 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')); |
|---|
| 12 | |
|---|
| 13 | class TestOfFetchingUrlParameters extends UnitTestCase { |
|---|
| 14 | |
|---|
| 15 | function setUp() { |
|---|
| 16 | $this->_headers = &new MockSimpleHttpHeaders(); |
|---|
| 17 | |
|---|
| 18 | $this->_response = &new MockSimpleHttpResponse(); |
|---|
| 19 | $this->_response->setReturnValue('isError', false); |
|---|
| 20 | $this->_response->setReturnReference('getHeaders', new MockSimpleHttpHeaders()); |
|---|
| 21 | |
|---|
| 22 | $this->_request = &new MockSimpleHttpRequest(); |
|---|
| 23 | $this->_request->setReturnReference('fetch', $this->_response); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | function testGetRequestWithoutIncidentGivesNoErrors() { |
|---|
| 27 | $url = new SimpleUrl('http://test:secret@this.com/page.html'); |
|---|
| 28 | $url->addRequestParameters(array('a' => 'A', 'b' => 'B')); |
|---|
| 29 | |
|---|
| 30 | $agent = &new MockRequestUserAgent(); |
|---|
| 31 | $agent->setReturnReference('_createHttpRequest', $this->_request); |
|---|
| 32 | $agent->SimpleUserAgent(); |
|---|
| 33 | |
|---|
| 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()); |
|---|
| 38 | } |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | class TestOfAdditionalHeaders extends UnitTestCase { |
|---|
| 42 | |
|---|
| 43 | function testAdditionalHeaderAddedToRequest() { |
|---|
| 44 | $response = &new MockSimpleHttpResponse(); |
|---|
| 45 | $response->setReturnReference('getHeaders', new MockSimpleHttpHeaders()); |
|---|
| 46 | |
|---|
| 47 | $request = &new MockSimpleHttpRequest(); |
|---|
| 48 | $request->setReturnReference('fetch', $response); |
|---|
| 49 | $request->expectOnce( |
|---|
| 50 | 'addHeaderLine', |
|---|
| 51 | array('User-Agent: SimpleTest')); |
|---|
| 52 | |
|---|
| 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()); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | class TestOfBrowserCookies extends UnitTestCase { |
|---|
| 62 | |
|---|
| 63 | function &_createStandardResponse() { |
|---|
| 64 | $response = &new MockSimpleHttpResponse(); |
|---|
| 65 | $response->setReturnValue("isError", false); |
|---|
| 66 | $response->setReturnValue("getContent", "stuff"); |
|---|
| 67 | $response->setReturnReference("getHeaders", new MockSimpleHttpHeaders()); |
|---|
| 68 | return $response; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | function &_createCookieSite($header_lines) { |
|---|
| 72 | $headers = &new SimpleHttpHeaders($header_lines); |
|---|
| 73 | |
|---|
| 74 | $response = &new MockSimpleHttpResponse(); |
|---|
| 75 | $response->setReturnValue("isError", false); |
|---|
| 76 | $response->setReturnReference("getHeaders", $headers); |
|---|
| 77 | $response->setReturnValue("getContent", "stuff"); |
|---|
| 78 | |
|---|
| 79 | $request = &new MockSimpleHttpRequest(); |
|---|
| 80 | $request->setReturnReference("fetch", $response); |
|---|
| 81 | return $request; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | function &_createMockedRequestUserAgent(&$request) { |
|---|
| 85 | $agent = &new MockRequestUserAgent(); |
|---|
| 86 | $agent->setReturnReference('_createHttpRequest', $request); |
|---|
| 87 | $agent->SimpleUserAgent(); |
|---|
| 88 | return $agent; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | function testCookieJarIsSentToRequest() { |
|---|
| 92 | $jar = new SimpleCookieJar(); |
|---|
| 93 | $jar->setCookie('a', 'A'); |
|---|
| 94 | |
|---|
| 95 | $request = &new MockSimpleHttpRequest(); |
|---|
| 96 | $request->setReturnReference('fetch', $this->_createStandardResponse()); |
|---|
| 97 | $request->expectOnce('readCookiesFromJar', array($jar, '*')); |
|---|
| 98 | |
|---|
| 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()); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | function testNoCookieJarIsSentToRequestWhenCookiesAreDisabled() { |
|---|
| 107 | $request = &new MockSimpleHttpRequest(); |
|---|
| 108 | $request->setReturnReference('fetch', $this->_createStandardResponse()); |
|---|
| 109 | $request->expectNever('readCookiesFromJar'); |
|---|
| 110 | |
|---|
| 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()); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 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"); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 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); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 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"); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | function testClearCookieBySettingExpiry() { |
|---|
| 149 | $request = &$this->_createCookieSite('Set-cookie: a=b'); |
|---|
| 150 | $agent = &$this->_createMockedRequestUserAgent($request); |
|---|
| 151 | |
|---|
| 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"), |
|---|
| 158 | "b"); |
|---|
| 159 | $agent->restart("Wed, 25-Dec-02 04:24:20 GMT"); |
|---|
| 160 | $this->assertIdentical( |
|---|
| 161 | $agent->getCookieValue("this.com", "this/path/", "a"), |
|---|
| 162 | false); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 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); |
|---|
| 168 | |
|---|
| 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"), |
|---|
| 175 | "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"), |
|---|
| 180 | false); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | function testReadingIncomingAndSettingNewCookies() { |
|---|
| 184 | $request = &$this->_createCookieSite('Set-cookie: a=AAA'); |
|---|
| 185 | $agent = &$this->_createMockedRequestUserAgent($request); |
|---|
| 186 | |
|---|
| 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/"); |
|---|
| 192 | $this->assertEqual( |
|---|
| 193 | $agent->getBaseCookieValue("a", new SimpleUrl('http://this.com/this/path/page.html')), |
|---|
| 194 | "AAA"); |
|---|
| 195 | $this->assertEqual( |
|---|
| 196 | $agent->getBaseCookieValue("b", new SimpleUrl('http://this.com/this/path/page.html')), |
|---|
| 197 | "BBB"); |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | class TestOfHttpRedirects extends UnitTestCase { |
|---|
| 202 | |
|---|
| 203 | function &createRedirect($content, $redirect) { |
|---|
| 204 | $headers = &new MockSimpleHttpHeaders(); |
|---|
| 205 | $headers->setReturnValue('isRedirect', (boolean)$redirect); |
|---|
| 206 | $headers->setReturnValue('getLocation', $redirect); |
|---|
| 207 | |
|---|
| 208 | $response = &new MockSimpleHttpResponse(); |
|---|
| 209 | $response->setReturnValue('getContent', $content); |
|---|
| 210 | $response->setReturnReference('getHeaders', $headers); |
|---|
| 211 | |
|---|
| 212 | $request = &new MockSimpleHttpRequest(); |
|---|
| 213 | $request->setReturnReference('fetch', $response); |
|---|
| 214 | return $request; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 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(); |
|---|
| 224 | |
|---|
| 225 | $agent->setMaximumRedirects(0); |
|---|
| 226 | $response = &$agent->fetchResponse(new SimpleUrl('here.html'), new SimpleGetEncoding()); |
|---|
| 227 | $this->assertEqual($response->getContent(), 'stuff'); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | function testSingleRedirect() { |
|---|
| 231 | $agent = &new MockRequestUserAgent(); |
|---|
| 232 | $agent->setReturnReferenceAt( |
|---|
| 233 | 0, |
|---|
| 234 | '_createHttpRequest', |
|---|
| 235 | $this->createRedirect('first', 'two.html')); |
|---|
| 236 | $agent->setReturnReferenceAt( |
|---|
| 237 | 1, |
|---|
| 238 | '_createHttpRequest', |
|---|
| 239 | $this->createRedirect('second', 'three.html')); |
|---|
| 240 | $agent->expectCallCount('_createHttpRequest', 2); |
|---|
| 241 | $agent->SimpleUserAgent(); |
|---|
| 242 | |
|---|
| 243 | $agent->setMaximumRedirects(1); |
|---|
| 244 | $response = &$agent->fetchResponse(new SimpleUrl('one.html'), new SimpleGetEncoding()); |
|---|
| 245 | $this->assertEqual($response->getContent(), 'second'); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | function testDoubleRedirect() { |
|---|
| 249 | $agent = &new MockRequestUserAgent(); |
|---|
| 250 | $agent->setReturnReferenceAt( |
|---|
| 251 | 0, |
|---|
| 252 | '_createHttpRequest', |
|---|
| 253 | $this->createRedirect('first', 'two.html')); |
|---|
| 254 | $agent->setReturnReferenceAt( |
|---|
| 255 | 1, |
|---|
| 256 | '_createHttpRequest', |
|---|
| 257 | $this->createRedirect('second', 'three.html')); |
|---|
| 258 | $agent->setReturnReferenceAt( |
|---|
| 259 | 2, |
|---|
| 260 | '_createHttpRequest', |
|---|
| 261 | $this->createRedirect('third', 'four.html')); |
|---|
| 262 | $agent->expectCallCount('_createHttpRequest', 3); |
|---|
| 263 | $agent->SimpleUserAgent(); |
|---|
| 264 | |
|---|
| 265 | $agent->setMaximumRedirects(2); |
|---|
| 266 | $response = &$agent->fetchResponse(new SimpleUrl('one.html'), new SimpleGetEncoding()); |
|---|
| 267 | $this->assertEqual($response->getContent(), 'third'); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | function testSuccessAfterRedirect() { |
|---|
| 271 | $agent = &new MockRequestUserAgent(); |
|---|
| 272 | $agent->setReturnReferenceAt( |
|---|
| 273 | 0, |
|---|
| 274 | '_createHttpRequest', |
|---|
| 275 | $this->createRedirect('first', 'two.html')); |
|---|
| 276 | $agent->setReturnReferenceAt( |
|---|
| 277 | 1, |
|---|
| 278 | '_createHttpRequest', |
|---|
| 279 | $this->createRedirect('second', false)); |
|---|
| 280 | $agent->setReturnReferenceAt( |
|---|
| 281 | 2, |
|---|
| 282 | '_createHttpRequest', |
|---|
| 283 | $this->createRedirect('third', 'four.html')); |
|---|
| 284 | $agent->expectCallCount('_createHttpRequest', 2); |
|---|
| 285 | $agent->SimpleUserAgent(); |
|---|
| 286 | |
|---|
| 287 | $agent->setMaximumRedirects(2); |
|---|
| 288 | $response = &$agent->fetchResponse(new SimpleUrl('one.html'), new SimpleGetEncoding()); |
|---|
| 289 | $this->assertEqual($response->getContent(), 'second'); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | function testRedirectChangesPostToGet() { |
|---|
| 293 | $agent = &new MockRequestUserAgent(); |
|---|
| 294 | $agent->setReturnReferenceAt( |
|---|
| 295 | 0, |
|---|
| 296 | '_createHttpRequest', |
|---|
| 297 | $this->createRedirect('first', 'two.html')); |
|---|
| 298 | $agent->expectArgumentsAt(0, '_createHttpRequest', array('*', new IsAExpectation('SimplePostEncoding'))); |
|---|
| 299 | $agent->setReturnReferenceAt( |
|---|
| 300 | 1, |
|---|
| 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()); |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | class TestOfBadHosts extends UnitTestCase { |
|---|
| 312 | |
|---|
| 313 | function &_createSimulatedBadHost() { |
|---|
| 314 | $response = &new MockSimpleHttpResponse(); |
|---|
| 315 | $response->setReturnValue('isError', true); |
|---|
| 316 | $response->setReturnValue('getError', 'Bad socket'); |
|---|
| 317 | $response->setReturnValue('getContent', false); |
|---|
| 318 | |
|---|
| 319 | $request = &new MockSimpleHttpRequest(); |
|---|
| 320 | $request->setReturnReference('fetch', $response); |
|---|
| 321 | return $request; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | function testUntestedHost() { |
|---|
| 325 | $request = &$this->_createSimulatedBadHost(); |
|---|
| 326 | |
|---|
| 327 | $agent = &new MockRequestUserAgent(); |
|---|
| 328 | $agent->setReturnReference('_createHttpRequest', $request); |
|---|
| 329 | $agent->SimpleUserAgent(); |
|---|
| 330 | |
|---|
| 331 | $response = &$agent->fetchResponse( |
|---|
| 332 | new SimpleUrl('http://this.host/this/path/page.html'), |
|---|
| 333 | new SimpleGetEncoding()); |
|---|
| 334 | $this->assertTrue($response->isError()); |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | class TestOfAuthorisation extends UnitTestCase { |
|---|
| 339 | |
|---|
| 340 | function testAuthenticateHeaderAdded() { |
|---|
| 341 | $response = &new MockSimpleHttpResponse(); |
|---|
| 342 | $response->setReturnReference('getHeaders', new MockSimpleHttpHeaders()); |
|---|
| 343 | |
|---|
| 344 | $request = &new MockSimpleHttpRequest(); |
|---|
| 345 | $request->setReturnReference('fetch', $response); |
|---|
| 346 | $request->expectOnce( |
|---|
| 347 | 'addHeaderLine', |
|---|
| 348 | array('Authorization: Basic ' . base64_encode('test:secret'))); |
|---|
| 349 | |
|---|
| 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()); |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | ?> |
|---|