56
61
contents, MAASDispatcher().dispatch_query(url, {}).read())
63
def test_request_from_http(self):
64
# We can't just call self.make_file because HTTPServerFixture will only
65
# serve content from the current WD. And we don't want to create random
66
# content in the original WD.
67
self.useFixture(TempWDFixture())
68
name = factory.getRandomString()
69
content = factory.getRandomString().encode('ascii')
70
factory.make_file(location='.', name=name, contents=content)
71
with HTTPServerFixture() as httpd:
72
url = urljoin(httpd.url, name)
73
response = MAASDispatcher().dispatch_query(url, {})
74
self.assertEqual(200, response.code)
75
self.assertEqual(content, response.read())
77
def test_supports_content_encoding_gzip(self):
78
# The client will set the Accept-Encoding: gzip header, and it will
79
# also decompress the response if it comes back with Content-Encoding:
81
self.useFixture(TempWDFixture())
82
name = factory.getRandomString()
83
content = factory.getRandomString(300).encode('ascii')
84
factory.make_file(location='.', name=name, contents=content)
86
orig_urllib = urllib2.urlopen
88
def logging_urlopen(*args, **kwargs):
89
called.append((args, kwargs))
90
return orig_urllib(*args, **kwargs)
91
self.patch(urllib2, 'urlopen', logging_urlopen)
92
with HTTPServerFixture() as httpd:
93
url = urljoin(httpd.url, name)
94
res = MAASDispatcher().dispatch_query(url, {})
95
self.assertEqual(200, res.code)
96
self.assertEqual(content, res.read())
97
request = called[0][0][0]
98
self.assertEqual([((request,), {})], called)
99
self.assertEqual('gzip', request.headers.get('Accept-encoding'))
101
def test_doesnt_override_accept_encoding_headers(self):
102
# If someone passes their own Accept-Encoding header, then dispatch
103
# just passes it through.
104
self.useFixture(TempWDFixture())
105
name = factory.getRandomString()
106
content = factory.getRandomString(300).encode('ascii')
107
factory.make_file(location='.', name=name, contents=content)
108
with HTTPServerFixture() as httpd:
109
url = urljoin(httpd.url, name)
110
headers = {'Accept-encoding': 'gzip'}
111
res = MAASDispatcher().dispatch_query(url, headers)
112
self.assertEqual(200, res.code)
113
self.assertEqual('gzip', res.info().get('Content-Encoding'))
114
raw_content = res.read()
115
read_content = gzip.GzipFile(
116
mode='rb', fileobj=BytesIO(raw_content)).read()
117
self.assertEqual(content, read_content)
60
121
"""Create an arbitrary URL."""