def test_init(self): host = gethostname() fixture = HTTPServerFixture(host=host) self.assertIsInstance(fixture.server, ThreadingHTTPServer) expected_url = "http://%s:%d/" % ( gethostbyname(host), fixture.server.server_port) self.assertEqual(expected_url, fixture.url)
def test_use(self): filename = relpath(__file__) self.assertThat(filename, FileExists()) with HTTPServerFixture() as httpd: url = urljoin(httpd.url, filename) with closing(urlopen(url)) as http_in: http_data_in = http_in.read() with open(filename, "rb") as file_in: file_data_in = file_in.read() self.assertEqual( file_data_in, http_data_in, "The content of %s differs from %s." % (url, filename))
def test_request_from_http(self): # We can't just call self.make_file because HTTPServerFixture will only # serve content from the current WD. And we don't want to create random # content in the original WD. self.useFixture(TempWDFixture()) name = factory.getRandomString() content = factory.getRandomString().encode('ascii') factory.make_file(location='.', name=name, contents=content) with HTTPServerFixture() as httpd: url = urljoin(httpd.url, name) response = MAASDispatcher().dispatch_query(url, {}) self.assertEqual(200, response.code) self.assertEqual(content, response.read())
def test_supports_gzip(self): filename = relpath(__file__) with HTTPServerFixture() as httpd: url = urljoin(httpd.url, filename) headers = {'Accept-Encoding': 'gzip, deflate'} request = Request(url, None, headers=headers) with closing(urlopen(request)) as http_in: http_headers = http_in.info() http_data_in = http_in.read() self.assertEqual('gzip', http_headers['Content-Encoding']) with open(filename, "rb") as file_in: file_data_in = file_in.read() http_data_decompressed = self.ungzip(http_data_in) self.assertEqual( file_data_in, http_data_decompressed, "The content of %s differs from %s." % (url, filename))
def test_retries_three_times_raises_503_service_unavailable(self): self.useFixture(TempWDFixture()) name = factory.make_string() content = factory.make_string().encode('ascii') factory.make_file(location='.', name=name, contents=content) with HTTPServerFixture() as httpd: url = urljoin(httpd.url, name) def _wrap_urlopen(*args, **kwargs): raise urllib.error.HTTPError(url, 503, "service unavailable", {}, None) self.patch(urllib.request, "urlopen").side_effect = _wrap_urlopen err = self.assertRaises(urllib.error.HTTPError, MAASDispatcher().dispatch_query, url, {}) self.assertEqual(503, err.code)
def test_doesnt_override_accept_encoding_headers(self): # If someone passes their own Accept-Encoding header, then dispatch # just passes it through. self.useFixture(TempWDFixture()) name = factory.getRandomString() content = factory.getRandomString(300).encode('ascii') factory.make_file(location='.', name=name, contents=content) with HTTPServerFixture() as httpd: url = urljoin(httpd.url, name) headers = {'Accept-encoding': 'gzip'} res = MAASDispatcher().dispatch_query(url, headers) self.assertEqual(200, res.code) self.assertEqual('gzip', res.info().get('Content-Encoding')) raw_content = res.read() read_content = gzip.GzipFile(mode='rb', fileobj=BytesIO(raw_content)).read() self.assertEqual(content, read_content)
def test_supports_any_method(self): # urllib2, which MAASDispatcher uses, only supports POST and # GET. There is some extra code that makes sure the passed # method is honoured which is tested here. self.useFixture(TempWDFixture()) name = factory.make_string() content = factory.make_string(300).encode('ascii') factory.make_file(location='.', name=name, contents=content) method = "PUT" # The test httpd doesn't like PUT, so we'll look for it bitching # about that for the purposes of this test. with HTTPServerFixture() as httpd: url = urljoin(httpd.url, name) e = self.assertRaises( urllib2.HTTPError, MAASDispatcher().dispatch_query, url, {}, method=method) self.assertIn("Unsupported method ('PUT')", e.reason)
def test_supports_content_encoding_gzip(self): # The client will set the Accept-Encoding: gzip header, and it will # also decompress the response if it comes back with Content-Encoding: # gzip. self.useFixture(TempWDFixture()) name = factory.make_string() content = factory.make_string(300).encode('ascii') factory.make_file(location='.', name=name, contents=content) called = [] orig_urllib = urllib2.urlopen def logging_urlopen(*args, **kwargs): called.append((args, kwargs)) return orig_urllib(*args, **kwargs) self.patch(urllib2, 'urlopen', logging_urlopen) with HTTPServerFixture() as httpd: url = urljoin(httpd.url, name) res = MAASDispatcher().dispatch_query(url, {}) self.assertEqual(200, res.code) self.assertEqual(content, res.read()) request = called[0][0][0] self.assertEqual([((request,), {})], called) self.assertEqual('gzip', request.headers.get('Accept-encoding'))
def test_retries_three_times_on_503_service_unavailable(self): self.useFixture(TempWDFixture()) name = factory.make_string() content = factory.make_string().encode("ascii") factory.make_file(location=".", name=name, contents=content) with HTTPServerFixture() as httpd: url = urljoin(httpd.url, name) counter = {"count": 0} def _wrap_open(*args, **kwargs): if counter["count"] < 2: counter["count"] += 1 raise urllib.error.HTTPError(url, 503, "service unavailable", {}, None) else: return self.orig_open_func(*args, **kwargs) self.open_func = _wrap_open response = MAASDispatcher().dispatch_query(url, {}) self.assertEqual(200, response.code) self.assertEqual(content, response.read())
def multiply(self, result): # Now run this test remotely for each requested Sauce OnDemand # browser requested. browser_names = get_remote_browser_names_from_env() if len(browser_names) == 0: return # A web server is needed so the OnDemand service can obtain local # tests. Be careful when choosing web server ports; only a scattering # are proxied. See <https://saucelabs.com/docs/ondemand/connect>. with HTTPServerFixture(port=5555) as httpd: scenarios = tuple((relpath(path, root), { "test_url": urljoin(httpd.url, path) }) for path in self.test_paths) with SauceConnectFixture() as sauce_connect: for browser_name in browser_names: capabilities = remote_browsers[browser_name] sst_ondemand = SSTOnDemandFixture( capabilities, sauce_connect.control_url) with sst_ondemand: browser_test = self.clone("remote:%s" % browser_name) browser_test.scenarios = scenarios browser_test(result)
def test_retries_three_times_on_503_service_unavailable(self): self.useFixture(TempWDFixture()) name = factory.make_string() content = factory.make_string().encode('ascii') factory.make_file(location='.', name=name, contents=content) with HTTPServerFixture() as httpd: url = urljoin(httpd.url, name) original_urlopen = urllib.request.urlopen counter = {'count': 0} def _wrap_urlopen(*args, **kwargs): if counter['count'] < 2: counter['count'] += 1 raise urllib.error.HTTPError(url, 503, "service unavailable", {}, None) else: return original_urlopen(*args, **kwargs) self.patch(urllib.request, "urlopen").side_effect = _wrap_urlopen response = MAASDispatcher().dispatch_query(url, {}) self.assertEqual(200, response.code) self.assertEqual(content, response.read())