root/test_session.txt

Revision 297:0df29400e6b8, 3.9 kB (checked in by Stefan Schwarzer <sschwarzer@sschwarzer.net>, 1 year ago)
Made all imports relative (apart from those in the
`wsbrowser`/`wsbrowser.py` scripts, where they're necessary).

The former approach of using only absolute imports was kind of
cleaner, especially with respect to future Python versions. - However,
the absolute imports were also _very_ confusing (even for me) and
required a funny workaround for development if a Websourcebrowser
package was already installed somewhere in `sys.path`.
Line 
1 Testing the ``session`` module
2 ==============================
3
4 The class ``Session`` has one data attribute: The list ``params``
5 contains a list of all query parameter names which should be taken
6 into account. The attribute is set from the first argument of the
7 constructor.
8
9 The method ``get_from_url`` takes a full encoded URL and returns
10 a dictionary with the parameters from the query string of the URL.
11
12     >>> import session
13
14 Testing the constructor
15 -----------------------
16
17 The constructor takes one obligatory and one optional argument. The
18 first is a list of names (strings) which are part of the session. The
19 second argument, `defaults` is a mapping from the names to their
20 default string values.
21
22     >>> s = session.Session([])   #doctest: +ELLIPSIS
23     Traceback (most recent call last):
24         ...
25     SessionError: ...
26
27 This is ok::
28
29     >>> s = session.Session(['var'])
30     >>> s = session.Session(['var'], {'var': "3"})
31
32 Of course, the mapping can only contain names which are in the
33 parameter list. Otherwise, a `SessionError` will be raised::
34
35     >>> s = session.Session(['var'], {'var2': "7"})
36     ... #doctest: +ELLIPSIS
37     Traceback (most recent call last):
38         ...
39     SessionError: ...
40
41 Testing the ``get_from_url`` method
42 -----------------------------------
43
44 The keys are usually strings, the values are always unicode strings,
45 even if a parameter could be converted to a number::
46
47     >>> s = session.Session(['a'])
48     >>> s.get_from_url("http://localhost/page?a=1&b=2")
49     {'a': u'1'}
50
51 URLs without host part work, too::
52
53     >>> s = session.Session(['a'])
54     >>> s.get_from_url("/page?a=1&b=2")
55     {'a': u'1'}
56
57 If the URL contains encoded characters, they are decoded::
58
59     >>> s = session.Session(['a'])
60     >>> s.get_from_url("/page?a=Test%20string&b=2")
61     {'a': u'Test string'}
62
63 If one of the parameters isn't in the URL, the corresponding
64 dictionary key is missing::
65
66     >>> s = session.Session(['a', 'b'])
67     >>> s.get_from_url("/page?b=2")
68     {'b': u'2'}
69
70 Unless there's a default that can be used instead::
71
72     >>> s = session.Session(['a', 'b'], defaults={'a': "abc"})
73     >>> params = s.get_from_url("/page?b=2")
74     >>> params['a'] == "abc"
75     True
76     >>> params['b'] == u"2"
77     True
78
79 If a parameter is given multiple times, a list is used as the
80 dictionary value::
81
82     >>> s = session.Session(['a'])
83     >>> params = s.get_from_url("/page?a=Test&a=1")
84     >>> params.keys()
85     ['a']
86     >>> sorted(params['a'])
87     [u'1', u'Test']
88
89 Testing the ``add_to_url`` method
90 ---------------------------------
91
92 The ``add_to_url`` is the reverse of ``get_from_url``. That is, the
93 query string of the URL is updated to contain the values from the
94 passed-in dictionary. The return value is an URL (unicode string).
95
96 A simple example::
97
98     >>> s = session.Session(['a'])
99     >>> s.add_to_url("/page", {'a': '1'})
100     '/page?a=1'
101
102 If the parameter is already present in the URL, it's overwritten::
103
104     >>> s = session.Session(['a'])
105     >>> s.add_to_url("/page?a=1", {'a': u'2'})
106     '/page?a=2'
107
108 Parameters which are not in the instance variable ``params`` are
109 ignored::
110
111     >>> s = session.Session(['a'])
112     >>> s.add_to_url("/page", {'b': '1'})
113     '/page'
114
115 Parameter values containing special characters are properly encoded::
116
117     >>> s = session.Session(['a'])
118     >>> s.add_to_url("/page?a=1", {'a': u'Test & string'})
119     '/page?a=Test%20%26%20string'
120
121 Parameters which are lists are converted to several "assignments" in
122 the query string::
123
124     >>> s = session.Session(['a'])
125     >>> s.add_to_url("/page?b=1", {'a': [u'1', u"2"]})
126     '/page?a=1&a=2&b=1'
127
128 (The last test may fail for another Python version since it depends
129 on dictionary "order" and module implementations.)
130
131 If a parameter value matches its default (i. e. compares equal), it's
132 omitted from the query string of the constructed URL::
133
134     >>> s = session.Session(['a'], defaults={'a': "7"})
135     >>> s.add_to_url("/page?c=1", {'a': "7"})
136     '/page?c=1'
137
Note: See TracBrowser for help on using the browser.