216
267
d.addErrback(got_error)
217
268
self.assertFailure(d, HTTPCodeError)
271
def test_fetch_many_async(self):
273
L{fetch_many_async} retrieves multiple URLs, and returns a
274
C{DeferredList} firing its callback when all the URLs have
275
successfully completed.
277
url_results = {"http://good/": "good",
278
"http://better/": "better"}
280
def callback(result, url):
281
self.assertIn(result, url_results.values())
282
self.assertIn(url, url_results)
285
def errback(failure, url):
288
curl = CurlManyStub(url_results)
289
d = fetch_many_async(url_results.keys(), callback=callback,
290
errback=errback, curl=curl)
292
def completed(result):
293
self.assertEquals(url_results, {})
295
return d.addCallback(completed)
297
def test_fetch_many_async_with_error(self):
299
L{fetch_many_async} aborts as soon as one URL fails.
301
url_results = {"http://right/": "right",
302
"http://wrong/": ("wrong", 501),
303
"http://impossilbe/": "impossible"}
306
def errback(failure, url):
307
failed_urls.append(url)
308
self.assertEquals(failure.value.body, "wrong")
309
self.assertEquals(failure.value.http_code, 501)
312
curl = CurlManyStub(url_results)
313
result = fetch_many_async(url_results.keys(), callback=None,
314
errback=errback, curl=curl)
316
def check_failure(failure):
317
self.assertTrue(isinstance(failure.subFailure.value,
319
self.assertEquals(failed_urls, ["http://wrong/"])
321
self.assertFailure(result, FirstError)
322
return result.addCallback(check_failure)
324
def test_url_to_filename(self):
326
L{url_to_filename} extracts the filename part of an URL, optionally
327
prepending a directory path to it.
329
self.assertEquals(url_to_filename("http://some/file"), "file")
330
self.assertEquals(url_to_filename("http://some/file/"), "file")
331
self.assertEquals(url_to_filename("http://some/file", directory="dir"),
332
os.path.join("dir", "file"))
334
def test_fetch_to_files(self):
336
L{fetch_to_files} fetches a list of URLs and save their content
337
in the given directory.
339
url_results = {"http://good/file": "file",
340
"http://even/better-file": "better-file"}
341
directory = self.makeDir()
342
curl = CurlManyStub(url_results)
344
result = fetch_to_files(url_results.keys(), directory, curl=curl)
346
def check_files(ignored):
347
for result in url_results.itervalues():
348
fd = open(os.path.join(directory, result))
349
self.assertEquals(fd.read(), result)
352
result.addCallback(check_files)
355
def test_fetch_to_files_with_trailing_slash(self):
357
L{fetch_to_files} discards trailing slashes from the final component
358
of the given URLs when saving them as files.
360
directory = self.makeDir()
361
curl = CurlStub("data")
363
result = fetch_to_files(["http:///with/slash/"], directory, curl=curl)
365
def check_files(ignored):
366
os.path.exists(os.path.join(directory, "slash"))
368
result.addCallback(check_files)
371
def test_fetch_to_files_with_errors(self):
373
L{fetch_to_files} optionally logs an error message as soon as one URL
376
url_results = {"http://im/right": "right",
377
"http://im/wrong": ("wrong", 404),
378
"http://im/not": "not"}
379
directory = self.makeDir()
381
logger = lambda message: messages.append(message)
382
curl = CurlManyStub(url_results)
385
result = fetch_to_files(url_results.keys(), directory, logger=logger,
388
def check_messages(failure):
389
self.assertEquals(len(messages), 1)
390
self.assertEquals(messages[0],
391
"Couldn't fetch file from http://im/wrong "
392
"(Server returned HTTP code 404)")
395
def check_files(ignored):
396
self.assertEquals(messages, [])
397
self.assertFalse(os.path.exists(os.path.join(directory, "wrong")))
399
result.addErrback(check_messages)
400
result.addCallback(check_files)
403
def test_fetch_to_files_with_non_existing_directory(self):
405
The deferred list returned by L{fetch_to_files} results in a failure
406
if the destination directory doesn't exist.
408
url_results = {"http://im/right": "right"}
409
directory = "i/dont/exist/"
410
curl = CurlManyStub(url_results)
412
result = fetch_to_files(url_results.keys(), directory, curl=curl)
414
def check_error(failure):
415
error = str(failure.value.subFailure.value)
416
self.assertEquals(error, "[Errno 2] No such file or directory: "
417
"'i/dont/exist/right'")
418
self.assertFalse(os.path.exists(os.path.join(directory, "right")))
420
result.addErrback(check_error)