def test_head_object(self):
     http_response = Mock()
     http_response.encoding = "utf-8"
     http_response.headers = {
         "Date": "Thu, 22 Aug 2013 02:11:57 GMT",
         "Content-Length": "265",
         "x-amz-request-id": "2B74ECB010FF029E",
         "ETag": '"40d06eb6194712ac1c915783004ef730"',
         "Server": "AmazonS3",
         "content-type": "binary/octet-stream",
         "Content-Type": "binary/octet-stream",
         "accept-ranges": "bytes",
         "Last-Modified": "Tue, 20 Aug 2013 18:33:25 GMT",
         "x-amz-server-side-encryption": "AES256",
     }
     http_response.content = ""
     put_object = self.s3.get_operation("HeadObject")
     expected = {
         "AcceptRanges": "bytes",
         "ContentType": "binary/octet-stream",
         "LastModified": "Tue, 20 Aug 2013 18:33:25 GMT",
         "ContentLength": "265",
         "ETag": '"40d06eb6194712ac1c915783004ef730"',
         "ServerSideEncryption": "AES256",
     }
     response_data = get_response(self.session, put_object, http_response)[1]
     self.assertEqual(response_data, expected)
Example #2
0
 def test_non_unicode_string_error_strict(self):
     my_action = action.CmdAction("", decode_error='strict')
     not_unicode = BytesIO('\xa9'.encode("latin-1"))
     realtime = Mock()
     realtime.encoding = 'utf-8'
     pytest.raises(UnicodeDecodeError, my_action._print_process_output,
                   Mock(), not_unicode, Mock(), realtime)
    def test_decode_gzip(self, r_g):
        def compressed_gzip_body():
            stream = cStringIO.StringIO()
            compressor = gzip.GzipFile(fileobj=stream, mode="w")
            compressor.write(u"é".encode("utf-8") * 100)
            compressor.close()
            stream.seek(0)
            return stream

        normal_response_object = Mock()
        # Build an HTTPResponse object like the one requests uses, with
        # a gzip compressed body. `decode_content` needs to be False to
        # properly emulate requests behaviour : it's the caller's
        # responsability to decode, since it's supposed to be a raw stream.
        body = compressed_gzip_body()
        normal_response_object.raw = HTTPResponse(
            status=200,
            preload_content=False,
            headers={"content-encoding": "gzip", "content-type": "application/blah; charset=utf-8"},
            body=body,
            decode_content=False,
        )
        normal_response_object.encoding = "UTF-8"
        normal_response_object.status_code = 200
        r_g.return_value = normal_response_object

        eq_(appbase.try_get_resource(self.err, None, "http://foo.bar/", ""), u"é" * 100)
        self.assert_silent()
Example #4
0
 def test_list_objects_with_invalid_content_length(self):
     http_response = Mock()
     http_response.encoding = "utf-8"
     http_response.headers = CaseInsensitiveDict(
         {
             "Date": "Thu, 22 Aug 2013 02:11:57 GMT",
             # We say we have 265 bytes but we're returning 0,
             # this should raise an exception because this is not
             # a HEAD request.
             "Content-Length": "265",
             "x-amz-request-id": "2B74ECB010FF029E",
             "ETag": '"40d06eb6194712ac1c915783004ef730"',
             "Server": "AmazonS3",
             "content-type": "binary/octet-stream",
             "Content-Type": "binary/octet-stream",
             "accept-ranges": "bytes",
             "Last-Modified": "Tue, 20 Aug 2013 18:33:25 GMT",
             "x-amz-server-side-encryption": "AES256",
         }
     )
     http_response.content = ""
     http_response.request.method = "GET"
     list_objects = self.s3.get_operation("ListObjects")
     expected = {
         "AcceptRanges": "bytes",
         "ContentType": "binary/octet-stream",
         "LastModified": "Tue, 20 Aug 2013 18:33:25 GMT",
         "ContentLength": "265",
         "ETag": '"40d06eb6194712ac1c915783004ef730"',
         "ServerSideEncryption": "AES256",
     }
     with self.assertRaises(IncompleteReadError):
         response_data = get_response(self.session, list_objects, http_response)[1]
    def test_eventual_404(self, r_g):
        error_response = Mock()
        error_response.raw.read.side_effect = ["x" * 100, ""]
        error_response.encoding = ""
        error_response.status_code = 404
        r_g.return_value = error_response

        appbase.try_get_resource(self.err, None, "http://foo.bar/", "")
        self.assert_failed(with_errors=True)
    def test_empty(self, r_g):
        empty_response = Mock()
        empty_response.raw.read.return_value = ""
        empty_response.encoding = ""
        empty_response.status_code = 200
        r_g.return_value = empty_response

        eq_(appbase.try_get_resource(self.err, None, "http://foo.bar/", ""), "")
        self.assert_failed(with_errors=True)
    def test_unicodeness(self, r_g):
        normal_response_object = Mock()
        normal_response_object.raw.read.side_effect = [u"é".encode("utf-8") * 100, ""]
        normal_response_object.encoding = "UTF-8"
        normal_response_object.status_code = 200
        r_g.return_value = normal_response_object

        eq_(appbase.try_get_resource(self.err, None, "http://foo.bar/", ""), u"é" * 100)
        self.assert_silent()
    def test_just_right(self, r_g):
        normal_response_object = Mock()
        normal_response_object.raw.read.side_effect = ["x" * 100, ""]
        normal_response_object.encoding = ""
        normal_response_object.status_code = 200
        r_g.return_value = normal_response_object

        eq_(appbase.try_get_resource(self.err, None, "http://foo.bar/", ""), "x" * 100)
        self.assert_silent()
Example #9
0
 def test_non_unicode_string_ok(self):
     my_action = action.CmdAction("", encoding="iso-8859-1")
     not_unicode = BytesIO(six.b("\xa9"))
     realtime = Mock()
     realtime.encoding = "utf-8"
     capture = StringIO()
     my_action._print_process_output(Mock(), not_unicode, capture, realtime)
     # get the correct char from latin-1 encoding
     expected = "©" if six.PY3 else "©".decode("utf-8")
     assert expected == capture.getvalue()
Example #10
0
 def test_non_unicode_string_error_replace(self):
     my_action = action.CmdAction("")  # default is decode_error = 'replace'
     not_unicode = BytesIO(six.b("\xa9"))
     realtime = Mock()
     realtime.encoding = "utf-8"
     capture = StringIO()
     my_action._print_process_output(Mock(), not_unicode, capture, realtime)
     # get the replacement char
     expected = "�" if six.PY3 else "�".decode("utf-8")
     assert expected == capture.getvalue()
Example #11
0
 def test_non_unicode_string_ok(self):
     my_action = action.CmdAction("", encoding='iso-8859-1')
     not_unicode = BytesIO('\xa9'.encode("latin-1"))
     realtime = Mock()
     realtime.encoding = 'utf-8'
     capture = StringIO()
     my_action._print_process_output(Mock(), not_unicode, capture, realtime)
     # get the correct char from latin-1 encoding
     expected = '©'
     assert expected == capture.getvalue()
Example #12
0
 def test_non_unicode_string_error_replace(self):
     my_action = action.CmdAction("") # default is decode_error = 'replace'
     not_unicode = BytesIO('\xa9'.encode("latin-1"))
     realtime = Mock()
     realtime.encoding = 'utf-8'
     capture = StringIO()
     my_action._print_process_output(Mock(), not_unicode, capture, realtime)
     # get the replacement char
     expected = '�'
     assert expected == capture.getvalue()
    def test_unicode_binary(self, r_g):
        normal_response_object = Mock()
        # The u"é" is *not* encoded in UTF-8 this time, so it would throw an
        # UnicodeEncodeError if we'd try to decode it.
        normal_response_object.raw.read.side_effect = [u"é" * 100, ""]
        normal_response_object.encoding = "UTF-8"
        normal_response_object.status_code = 200
        r_g.return_value = normal_response_object

        eq_(appbase.try_get_resource(self.err, None, "http://foo.bar/", "", binary=True), u"é" * 100)
        self.assert_silent()
Example #14
0
            def request_generator(*args, **kwargs):
                url = kwargs.get("url", args[0])
                if "://" not in url:
                    raise requests.exceptions.MissingSchema

                request = Mock()
                request.text = "foo bar"
                request.status_code = 200
                request.encoding = "UTF-8"
                # The first bit is the return value. The second bit tells whatever
                # is requesting the data that there's no more data.
                request.raw.read.side_effect = [request.text, ""]
                return request
 def test_put_object(self):
     http_response = Mock()
     http_response.encoding = "utf-8"
     http_response.headers = {
         "Date": "Thu, 22 Aug 2013 02:11:57 GMT",
         "Content-Length": "0",
         "x-amz-request-id": "2B74ECB010FF029E",
         "ETag": '"b081e66e7e0c314285c655cafb4d1e71"',
         "x-amz-id-2": "bKECRRBFttBRVbJPIVBLQwwipI0i+s9HMvNFdttR17ouR0pvQSKEJUR+1c6cW1nQ",
         "Server": "AmazonS3",
         "content-type": "text/xml",
     }
     http_response.content = ""
     put_object = self.s3.get_operation("PutObject")
     expected = {"ETag": '"b081e66e7e0c314285c655cafb4d1e71"'}
     response_data = get_response(self.session, put_object, http_response)[1]
     self.assertEqual(response_data, expected)
Example #16
0
 def test_non_unicode_string_error_strict(self):
     my_action = action.CmdAction("", decode_error="strict")
     not_unicode = BytesIO(six.b("\xa9"))
     realtime = Mock()
     realtime.encoding = "utf-8"
     pytest.raises(UnicodeDecodeError, my_action._print_process_output, Mock(), not_unicode, Mock(), realtime)