1
from unittest import TestCase
2
from simplestreams.mirrors import UrlMirrorReader
3
from simplestreams.contentsource import URL_READER
4
import simplestreams.mirrors
7
def fake_url_reader(*args, **kwargs):
9
Fake URL reader which returns all the arguments passed in as a dict.
11
Positional arguments are returned under the key "ARGS".
13
all_args = kwargs.copy()
14
all_args["ARGS"] = args
18
class TestUrlMirrorReader(TestCase):
20
def test_source(self):
21
"""source() method returns a ContentSource."""
22
# Verify source() returns a content source constructed using the
23
# appropriate path and mirrors.
24
reader = UrlMirrorReader("/prefix/", mirrors=["a/", "b/"])
25
cs = reader.source("some/path")
27
# Resulting ContentSource is passed an URL as a concatenation of
28
# the prefix and the path.
29
self.assertEqual("/prefix/some/path", cs.url)
30
# Mirror URLs have path appended.
31
self.assertEqual(["a/some/path", "b/some/path"], cs.mirrors)
32
# Default URL_READER is returned.
33
self.assertEqual(URL_READER, cs.url_reader)
35
def test_source_no_trailing_slash(self):
36
"""Even if prefix lacks a trailing slash, it behaves the same."""
37
reader = UrlMirrorReader("/prefix/", mirrors=["a/", "b/"])
38
cs = reader.source("some/path")
40
self.assertEqual("/prefix/some/path", cs.url)
41
self.assertEqual(["a/some/path", "b/some/path"], cs.mirrors)
42
self.assertEqual(URL_READER, cs.url_reader)
44
def test_source_user_agent(self):
45
"""When user_agent is set, it is passed to the ContentSource."""
46
reader = UrlMirrorReader("/prefix/", mirrors=["a/", "b/"],
47
user_agent="test agent")
48
cs = reader.source("some/path")
50
# A factory function is set instead of the URL_READER, and
51
# it constructs a URL_READER with user_agent passed in.
52
url_reader = cs.url_reader
53
self.assertNotEqual(URL_READER, url_reader)
55
# Override the default URL_READER to track arguments being passed.
56
simplestreams.mirrors.cs.URL_READER = fake_url_reader
57
result = url_reader("a", "b", something="c")
59
# It passes all the same arguments, with "user_agent" added in.
61
{"user_agent": "test agent", "something": "c", "ARGS": ("a", "b")},
64
# Restore default UrlReader.
65
simplestreams.mirrors.cs.URL_READER = URL_READER
67
def test_source_user_agent_no_trailing_slash(self):
69
When user_agent is set, it is passed to the ContentSource even
70
if there is no trailing slash.
72
reader = UrlMirrorReader("/prefix", mirrors=["a/", "b/"],
73
user_agent="test agent")
74
cs = reader.source("some/path")
76
# A factory function is set instead of the URL_READER, and
77
# it constructs a URL_READER with user_agent passed in.
78
url_reader = cs.url_reader
79
self.assertNotEqual(URL_READER, url_reader)
81
# Override the default URL_READER to track arguments being passed.
82
simplestreams.mirrors.cs.URL_READER = fake_url_reader
83
result = url_reader("a", "b", something="c")
85
# It passes all the same arguments, with "user_agent" added in.
87
{"user_agent": "test agent", "something": "c", "ARGS": ("a", "b")},
90
# Restore default UrlReader.
91
simplestreams.mirrors.cs.URL_READER = URL_READER