Exemplo n.º 1
0
    def test_remove_bucket_works(self, mock_connection):

        mock_data = ('<ListAllMyBucketsResult '
                     'xmlns="http://s3.amazonaws.com/doc/2006-03-01/">'
                     '<Buckets><Bucket><Name>hello</Name>'
                     '<CreationDate>2015-06-22T23:07:43.240Z</CreationDate>'
                     '</Bucket><Bucket><Name>world</Name>'
                     '<CreationDate>2015-06-22T23:07:56.766Z</CreationDate>'
                     '</Bucket></Buckets><Owner><ID>minio</ID>'
                     '<DisplayName>minio</DisplayName></Owner>'
                     '</ListAllMyBucketsResult>')
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('HEAD', 'http://localhost:9000/hello/',
                         {'User-Agent': _DEFAULT_USER_AGENT}, 200))
        mock_server.mock_add_request(
            MockResponse('DELETE', 'http://localhost:9000/hello/',
                         {'User-Agent': _DEFAULT_USER_AGENT}, 204))
        config = {
            'HOSTNAME': 'localhost:9000',
            "AWS_ACCESS_KEY_ID": None,
            'AWS_SECRET_ACCESS_KEY': None
        }
        service = MinioAdapter(config)
        service.remove_bucket('hello')
Exemplo n.º 2
0
 def test_bucket_exists_works(self, mock_connection):
     mock_server = MockConnection()
     mock_connection.return_value = mock_server
     mock_server.mock_add_request(
         MockResponse('HEAD', 'http://localhost:9000/hello/',
                      {'User-Agent': _DEFAULT_USER_AGENT}, 200))
     config = {
         'HOSTNAME': 'localhost:9000',
         "AWS_ACCESS_KEY_ID": None,
         'AWS_SECRET_ACCESS_KEY': None
     }
     client = MinioAdapter(config)
     result = client.bucket_exists('hello')
     self.assertEquals(True, result)
     mock_server.mock_add_request(
         MockResponse('HEAD', 'http://localhost:9000/goodbye/',
                      {'User-Agent': _DEFAULT_USER_AGENT}, 404))
     false_result = client.bucket_exists('goodbye')
     self.assertEquals(False, false_result)
Exemplo n.º 3
0
    def prepare_response(self, req, resp):
        """Builds a :class:`Response <trip.Response>` object from a tornado
        response. This should not be called from user code, and is only exposed
        for use when subclassing the
        :class:`HTTPAdapter <trip.adapters.HTTPAdapter>`

        :param req: The :class:`PreparedRequest <PreparedRequest>` used to
        generate the response.
        :param resp: The :class:`MessageDelegate <MessageDelegate>` response
        object.
        :rtype: requests.Response
        """

        response = Response()

        # Fallback to None if there's no status_code, for whatever reason.
        response.status_code = getattr(resp, 'code', None)

        # Make headers case-insensitive.
        response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))

        # Set encoding.
        response.encoding = get_encoding_from_headers(response.headers)
        response.raw = resp
        response.reason = getattr(resp, 'reason', '')

        if isinstance(req.url, bytes):
            response.url = req.url.decode('utf-8')
        else:
            response.url = req.url

        # Add new cookies from the server
        headerDict = HTTPHeaderDict(response.headers)
        response.cookies.extract_cookies(MockResponse(headerDict),
                                         MockRequest(req))
        self.cookies.extract_cookies(MockResponse(headerDict),
                                     MockRequest(req))

        response.request = req
        # response.connection = self

        return response
Exemplo n.º 4
0
    def test_make_bucket_works(self, mock_connection):
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('PUT', 'http://localhost:9000/hello/',
                         {'User-Agent': _DEFAULT_USER_AGENT}, 200))
        config = {
            'HOSTNAME': 'localhost:9000',
            "AWS_ACCESS_KEY_ID": None,
            'AWS_SECRET_ACCESS_KEY': None
        }

        service = MinioAdapter(config)
Exemplo n.º 5
0
def _extract_cookies(request, response, cookies):
    """Add cookies to the response.

    Cookies in requests are extracted from the headers in the original_response
    httplib.HTTPMessage which we don't create so we have to do this step
    manually.
    """
    # This will add cookies set manually via the Set-Cookie or Set-Cookie2
    # header but this only allows 1 cookie to be set.
    http_message = compat._FakeHTTPMessage(response.headers)
    response.cookies.extract_cookies(MockResponse(http_message),
                                     MockRequest(request))

    # This allows you to pass either a CookieJar or a dictionary to request_uri
    # or directly to create_response. To allow more than one cookie to be set.
    if cookies:
        merge_cookies(response.cookies, cookies)
Exemplo n.º 6
0
 def test_get_policy_for_non_existent_bucket(self, mock_connection):
     with pytest.raises(NoSuchBucket):
         mock_server = MockConnection()
         mock_connection.return_value = mock_server
         bucket_name = 'non-existent-bucket'
         mock_server.mock_add_request(
             MockResponse(
                 'GET',
                 'http://localhost:9000/' + bucket_name + '/?policy=',
                 {'User-Agent': _DEFAULT_USER_AGENT},
                 404,
             ))
         config = {
             'HOSTNAME': 'localhost:9000',
             "AWS_ACCESS_KEY_ID": None,
             'AWS_SECRET_ACCESS_KEY': None,
             'SECURE': True
         }
         service = MinioAdapter(config)
         service.get_policy(bucket_name)
Exemplo n.º 7
0
    def test_list_buckets_works(self, mock_connection):
        mock_data = ('<ListAllMyBucketsResult '
                     'xmlns="http://s3.amazonaws.com/doc/2006-03-01/">'
                     '<Buckets><Bucket><Name>hello</Name>'
                     '<CreationDate>2015-06-22T23:07:43.240Z</CreationDate>'
                     '</Bucket><Bucket><Name>world</Name>'
                     '<CreationDate>2015-06-22T23:07:56.766Z</CreationDate>'
                     '</Bucket></Buckets><Owner><ID>minio</ID>'
                     '<DisplayName>minio</DisplayName></Owner>'
                     '</ListAllMyBucketsResult>')
        mock_server = MockConnection()
        mock_connection.return_value = mock_server
        mock_server.mock_add_request(
            MockResponse('GET',
                         'http://localhost:9000/',
                         {'User-Agent': _DEFAULT_USER_AGENT},
                         200,
                         content=mock_data))

        config = {
            'HOSTNAME': 'localhost:9000',
            "AWS_ACCESS_KEY_ID": None,
            'AWS_SECRET_ACCESS_KEY': None
        }
        service = MinioAdapter(config)
        buckets = service.get_bucket_list()
        buckets_list = []
        count = 0
        for bucket in buckets:
            count += 1
            buckets_list.append(bucket)
        self.assertEquals(2, count)
        self.assertEquals('hello', buckets_list[0].name)
        self.assertEquals(datetime(2015, 6, 22, 23, 7, 43, 240000, pytz.utc),
                          buckets_list[0].creation_date)
        self.assertEquals('world', buckets_list[1].name)
        self.assertEquals(datetime(2015, 6, 22, 23, 7, 56, 766000, pytz.utc),
                          buckets_list[1].creation_date)
Exemplo n.º 8
0
 def test_empty_list_buckets_works(self, mock_connection):
     mock_data = ('<ListAllMyBucketsResult '
                  'xmlns="http://s3.amazonaws.com/doc/2006-03-01/">'
                  '<Buckets></Buckets><Owner><ID>minio</ID><DisplayName>'
                  'minio</DisplayName></Owner></ListAllMyBucketsResult>')
     mock_server = MockConnection()
     mock_connection.return_value = mock_server
     mock_server.mock_add_request(
         MockResponse('GET',
                      'http://localhost:9000/',
                      {'User-Agent': _DEFAULT_USER_AGENT},
                      200,
                      content=mock_data))
     config = {
         'HOSTNAME': 'localhost:9000',
         "AWS_ACCESS_KEY_ID": None,
         'AWS_SECRET_ACCESS_KEY': None
     }
     service = MinioAdapter(config)
     buckets = service.get_bucket_list()
     count = 0
     for bucket in buckets:
         count += 1
     self.assertEquals(0, count)
Exemplo n.º 9
0
    def send(self, req, **kwargs):
        """Send a given PreparedRequest.

        :rtype: trip.gen.Future
        """

        if not isinstance(req, PreparedRequest):
            raise ValueError('You can only send PreparedRequests.')

        allow_redirects = kwargs.pop('allow_redirects', True)
        start_time = preferred_clock()

        r = yield self.adapter.send(req, **kwargs)

        if isinstance(r, Exception):
            raise gen.Return(r)
        else:
            r = self.prepare_response(req, r)

        r.elapsed = timedelta(seconds=(preferred_clock() - start_time))

        # Response manipulation hooks
        r = dispatch_hook('response', req.hooks, r, **kwargs)

        # Persist cookies
        if r.history:
            # If the hooks create history then we want those cookies too
            for resp in r.history:
                self.cookies.extract_cookies(
                    MockResponse(HTTPHeaderDict(resp.headers)),
                    MockRequest(resp.request))

        self.cookies.extract_cookies(MockResponse(HTTPHeaderDict(r.headers)),
                                     MockRequest(req))

        # Redirect resolving generator.
        redirect_gen = self.resolve_redirects(r, req, **kwargs)

        # Resolve redirects if allowed.
        history = []
        if allow_redirects:
            for resp in redirect_gen:
                resp = yield resp
                history.append(resp)

        # Shuffle things around if there's history.
        if history:
            # Insert the first (original) request at the start
            history.insert(0, r)
            # Get the last request made
            r = history.pop()
            r.history = history

        # If redirects aren't being followed, store the response on the Request for Response.next().
        if not allow_redirects:
            try:
                r._next = next(
                    self.resolve_redirects(r,
                                           req,
                                           yield_requests=True,
                                           **kwargs))
            except StopIteration:
                pass

        raise gen.Return(r)
Exemplo n.º 10
0
    def resolve_redirects(self,
                          resp,
                          req,
                          stream=False,
                          timeout=None,
                          verify=True,
                          cert=None,
                          proxies=None,
                          yield_requests=False,
                          **adapter_kwargs):
        """Receives a Response. Returns a generator of Responses or Requests."""

        hist = []  # keep track of history

        url = self.get_redirect_target(resp)
        while url:
            prepared_request = req.copy()

            # Update history and keep track of redirects.
            # resp.history must ignore the original request in this loop
            hist.append(resp)
            resp.history = hist[1:]

            # Consume socket so it can be released
            resp.content

            if self.max_redirects <= len(resp.history):
                raise TooManyRedirects('Exceeded %s redirects.' %
                                       self.max_redirects,
                                       response=resp)

            # Release the connection
            resp.close()

            # Handle redirection without scheme (see: RFC 1808 Section 4)
            if url.startswith('//'):
                parsed_rurl = urlparse(resp.url)
                url = '%s:%s' % (to_native_string(parsed_rurl.scheme), url)

            # The scheme should be lower case...
            parsed = urlparse(url)
            url = parsed.geturl()

            # Facilitate relative 'location' headers, as allowed by RFC 7231.
            # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
            # Compliant with RFC3986, we percent encode the url.
            if not parsed.netloc:
                url = urljoin(resp.url, requote_uri(url))
            else:
                url = requote_uri(url)

            prepared_request.url = to_native_string(url)

            self.rebuild_method(prepared_request, resp)

            # https://github.com/requests/requests/issues/1084
            if resp.status_code not in (codes.temporary_redirect,
                                        codes.permanent_redirect):
                # https://github.com/requests/requests/issues/3490
                purged_headers = ('Content-Length', 'Content-Type',
                                  'Transfer-Encoding')
                for header in purged_headers:
                    prepared_request.headers.pop(header, None)
                prepared_request.body = None

            headers = prepared_request.headers
            try:
                del headers['Cookie']
            except KeyError:
                pass

            # Extract any cookies sent on the response to the cookiejar
            # in the new request. Because we've mutated our copied prepared
            # request, use the old one that we haven't yet touched.
            prepared_request._cookies.extract_cookies(
                MockResponse(HTTPHeaderDict(resp.headers)), MockRequest(req))
            merge_cookies(prepared_request._cookies, self.cookies)
            prepared_request.prepare_cookies(prepared_request._cookies)

            # Rebuild auth and proxy information.
            proxies = self.rebuild_proxies(prepared_request, proxies)
            self.rebuild_auth(prepared_request, resp)

            # Override the original request.
            req = prepared_request
            req.adapt_prepare()

            if yield_requests:
                yield req
            else:
                resp = self.send(req,
                                 stream=stream,
                                 timeout=timeout,
                                 verify=verify,
                                 cert=cert,
                                 proxies=proxies,
                                 allow_redirects=False,
                                 **adapter_kwargs)

                yield resp

                while not resp.done():
                    yield resp
                resp = resp.result()

                self.cookies.extract_cookies(
                    MockResponse(HTTPHeaderDict(resp.headers)),
                    MockRequest(prepared_request))

                # extract redirect url, if any, for the next loop
                url = self.get_redirect_target(resp)