~powersj/simplestreams/verbose_tox

« back to all changes in this revision

Viewing changes to tests/unittests/test_mirrorreaders.py

  • Committer: Scott Moser
  • Date: 2016-05-05 04:06:49 UTC
  • mfrom: (423.2.8 custom-user-agent)
  • Revision ID: smoser@ubuntu.com-20160505040649-hgwl8q237m3hs4us
Add user-agent support

This adds user-agent support.  The library user may want to
provide a specific user agent when querying a remote mirror.

Because of the code structure, there's some not-very-nice code in here, mainly
the use of url_reader_factory function, but without knowing what parts of
simplestreams API we want to keep backward compatible, I refrained from
changing anything of the existing code too much.

There are tests for all the new code, and for some of the old code that was
untested.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from unittest import TestCase
 
2
from simplestreams.mirrors import UrlMirrorReader
 
3
from simplestreams.contentsource import URL_READER
 
4
import simplestreams.mirrors
 
5
 
 
6
 
 
7
def fake_url_reader(*args, **kwargs):
 
8
    """
 
9
    Fake URL reader which returns all the arguments passed in as a dict.
 
10
 
 
11
    Positional arguments are returned under the key "ARGS".
 
12
    """
 
13
    all_args = kwargs.copy()
 
14
    all_args["ARGS"] = args
 
15
    return all_args
 
16
 
 
17
 
 
18
class TestUrlMirrorReader(TestCase):
 
19
 
 
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")
 
26
 
 
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)
 
34
 
 
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")
 
39
 
 
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)
 
43
 
 
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")
 
49
 
 
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)
 
54
 
 
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")
 
58
 
 
59
        # It passes all the same arguments, with "user_agent" added in.
 
60
        self.assertEqual(
 
61
            {"user_agent": "test agent", "something": "c", "ARGS": ("a", "b")},
 
62
            result)
 
63
 
 
64
        # Restore default UrlReader.
 
65
        simplestreams.mirrors.cs.URL_READER = URL_READER
 
66
 
 
67
    def test_source_user_agent_no_trailing_slash(self):
 
68
        """
 
69
        When user_agent is set, it is passed to the ContentSource even
 
70
        if there is no trailing slash.
 
71
        """
 
72
        reader = UrlMirrorReader("/prefix", mirrors=["a/", "b/"],
 
73
                                 user_agent="test agent")
 
74
        cs = reader.source("some/path")
 
75
 
 
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)
 
80
 
 
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")
 
84
 
 
85
        # It passes all the same arguments, with "user_agent" added in.
 
86
        self.assertEqual(
 
87
            {"user_agent": "test agent", "something": "c", "ARGS": ("a", "b")},
 
88
            result)
 
89
 
 
90
        # Restore default UrlReader.
 
91
        simplestreams.mirrors.cs.URL_READER = URL_READER