Ejemplo n.º 1
0
 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())
Ejemplo n.º 2
0
 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())
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
    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'))
Ejemplo n.º 6
0
    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())
Ejemplo n.º 7
0
    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.getRandomString()
        content = factory.getRandomString(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'))
Ejemplo n.º 8
0
    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())