2 Tests for the gocookies code.
11 from juju
.client
.gocookies
import GoCookieJar
13 # cookie_content holds the JSON contents of a Go-produced
14 # cookie file (reformatted so it's not all on one line but
15 # otherwise unchanged).
19 "CanonicalHost": "bar.com",
20 "Creation": "2017-11-17T08:53:55.088820092Z",
22 "Expires": "2345-11-15T18:16:08Z",
25 "LastAccess": "2017-11-17T08:53:55.088822562Z",
30 "Updated": "2017-11-17T08:53:55.088822562Z",
34 "CanonicalHost": "x.foo.com",
35 "Creation": "2017-11-17T08:53:55.088814857Z",
36 "Domain": "x.foo.com",
37 "Expires": "2345-11-15T18:16:05Z",
40 "LastAccess": "2017-11-17T08:53:55.088884015Z",
45 "Updated": "2017-11-17T08:53:55.088814857Z",
46 "Value": "foo-path-value"
49 "CanonicalHost": "x.foo.com",
50 "Creation": "2017-11-17T08:53:55.088814857Z",
52 "Expires": "2345-11-15T18:16:06Z",
55 "LastAccess": "2017-11-17T08:53:55.088919437Z",
60 "Updated": "2017-11-17T08:53:55.088814857Z",
64 "CanonicalHost": "x.foo.com",
65 "Creation": "2017-11-17T08:53:55.088790709Z",
66 "Domain": "x.foo.com",
67 "Expires": "2345-11-15T18:16:01Z",
70 "LastAccess": "2017-11-17T08:53:55.088884015Z",
75 "Updated": "2017-11-17T08:53:55.088790709Z",
79 "CanonicalHost": "x.foo.com",
80 "Creation": "2017-11-17T08:53:55.088790709Z",
82 "Expires": "2345-11-15T18:16:02Z",
85 "LastAccess": "2017-11-17T08:53:55.088919437Z",
90 "Updated": "2017-11-17T08:53:55.088790709Z",
94 "CanonicalHost": "x.foo.com",
95 "Creation": "2017-11-17T08:53:55.088790709Z",
96 "Domain": "x.foo.com",
97 "Expires": "2345-11-15T18:16:03Z",
100 "LastAccess": "2017-11-17T08:53:55.088850252Z",
105 "Updated": "2017-11-17T08:53:55.088790709Z",
106 "Value": "foo2-value"
109 "CanonicalHost": "x.foo.com",
110 "Creation": "2017-11-17T08:53:55.088790709Z",
112 "Expires": "2345-11-15T18:16:04Z",
115 "LastAccess": "2017-11-17T08:53:55.088919437Z",
120 "Updated": "2017-11-17T08:53:55.088790709Z",
121 "Value": "foo3-value"
126 # cookie_content_queries holds a set of queries
127 # that were automatically generated by running
128 # the queries on the above cookie_content data
129 # and printing the results.
130 cookie_content_queries
= [
131 ('http://x.foo.com', [
132 ('foo', 'foo-value'),
133 ('foo1', 'foo1-value'),
134 ('foo3', 'foo3-value'),
136 ('https://x.foo.com', [
137 ('foo', 'foo-value'),
138 ('foo1', 'foo1-value'),
139 ('foo2', 'foo2-value'),
140 ('foo3', 'foo3-value'),
142 ('http://arble.foo.com', [
143 ('foo1', 'foo1-value'),
144 ('foo3', 'foo3-value'),
146 ('http://arble.com', [
148 ('http://x.foo.com/path/x', [
149 ('foo', 'foo-path-value'),
150 ('foo4', 'foo4-value'),
151 ('foo', 'foo-value'),
152 ('foo1', 'foo1-value'),
153 ('foo3', 'foo3-value'),
155 ('http://arble.foo.com/path/x', [
156 ('foo4', 'foo4-value'),
157 ('foo1', 'foo1-value'),
158 ('foo3', 'foo3-value'),
160 ('http://foo.com/path/x', [
161 ('foo4', 'foo4-value'),
162 ('foo1', 'foo1-value'),
163 ('foo3', 'foo3-value'),
168 class TestGoCookieJar(unittest
.TestCase
):
170 self
.dir = tempfile
.mkdtemp()
173 shutil
.rmtree(self
.dir)
175 def test_readcookies(self
):
176 jar
= self
.load_jar(cookie_content
)
177 self
.assert_jar_queries(jar
, cookie_content_queries
)
179 def test_roundtrip(self
):
180 jar
= self
.load_jar(cookie_content
)
181 filename2
= os
.path
.join(self
.dir, 'cookies2')
182 jar
.save(filename
=filename2
)
184 jar
.load(filename
=filename2
)
185 self
.assert_jar_queries(jar
, cookie_content_queries
)
187 def test_expiry_time(self
):
190 "CanonicalHost": "bar.com",
191 "Creation": "2017-11-17T08:53:55.088820092Z",
193 "Expires": "2345-11-15T18:16:08Z",
196 "LastAccess": "2017-11-17T08:53:55.088822562Z",
201 "Updated": "2017-11-17T08:53:55.088822562Z",
205 jar
= self
.load_jar(content
)
206 got_expires
= tuple(jar
)[0].expires
207 want_expires
= int(pyrfc3339
.parse('2345-11-15T18:16:08Z').timestamp())
208 self
.assertEqual(got_expires
, want_expires
)
210 def load_jar(self
, content
):
211 filename
= os
.path
.join(self
.dir, 'cookies')
212 with
open(filename
, 'x') as f
:
215 jar
.load(filename
=filename
)
218 def assert_jar_queries(self
, jar
, queries
):
219 '''Assert that all the given queries (see cookie_content_queries)
220 are satisfied when run on the given cookie jar.
221 :param jar CookieJar: the cookie jar to query
222 :param queries: the queries to run.
224 for url
, want_cookies
in queries
:
225 req
= urllib
.request
.Request(url
)
226 jar
.add_cookie_header(req
)
227 # We can't use SimpleCookie to find out what cookies
228 # have been presented, because SimpleCookie
229 # only allows one cookie with a given name,
230 # so we naively parse the cookies ourselves, which
231 # is OK because we know we don't have to deal
232 # with any complex cases.
234 cookie_header
= req
.get_header('Cookie')
236 if cookie_header
is not None:
238 tuple(part
.split('='))
239 for part
in cookie_header
.split('; ')
242 want_cookies
= list(want_cookies
)
244 self
.assertEqual(got_cookies
, want_cookies
, msg
='query {}; got {}; want {}'.format(url
, got_cookies
, want_cookies
))