Exemplo n.º 1
0
    def _prepare(self, body, serializer=None,
            content_type=None, content_encoding=None, compression=None,
            headers=None):

        # No content_type? Then we're serializing the data internally.
        if not content_type:
            serializer = serializer or self.serializer
            (content_type, content_encoding,
             body) = encode(body, serializer=serializer)
        else:
            # If the programmer doesn't want us to serialize,
            # make sure content_encoding is set.
            if isinstance(body, unicode):
                if not content_encoding:
                    content_encoding = 'utf-8'
                body = body.encode(content_encoding)

            # If they passed in a string, we can't know anything
            # about it. So assume it's binary data.
            elif not content_encoding:
                content_encoding = 'binary'

        if compression:
            body, headers["compression"] = compress(body, compression)

        return body, content_type, content_encoding
Exemplo n.º 2
0
    def _prepare(self, body, serializer=None, content_type=None,
                 content_encoding=None, compression=None, headers=None):
        # Copied from kombu.messaging.Producer._prepare.

        # No content_type? Then we're serializing the data internally.
        if not content_type:
            serializer = serializer or self.serializer
            (content_type, content_encoding,
             body) = dumps(body, serializer=serializer)
        else:
            # If the programmer doesn't want us to serialize,
            # make sure content_encoding is set.
            if isinstance(body, text_t):
                if not content_encoding:
                    content_encoding = 'utf-8'
                body = body.encode(content_encoding)

            # If they passed in a string, we can't know anything
            # about it. So assume it's binary data.
            elif not content_encoding:
                content_encoding = 'binary'

        if compression:
            body, headers['compression'] = compress(body, compression)

        return body, content_type, content_encoding
Exemplo n.º 3
0
    def test_compress__decompress__zstd(self):
        pytest.importorskip('zstandard')

        text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
        c, ctype = compression.compress(text, 'zstd')
        assert text != c
        d = compression.decompress(c, ctype)
        assert d == text
Exemplo n.º 4
0
    def test_compress__decompress__backports_lzma(self):
        pytest.importorskip('backports.lzma')

        text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
        c, ctype = compression.compress(text, 'lzma')
        assert text != c
        d = compression.decompress(c, ctype)
        assert d == text
Exemplo n.º 5
0
 def test_compress__decompress__bzip2(self):
     if not self.has_bzip2:
         raise SkipTest('bzip2 not available')
     text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
     c, ctype = compression.compress(text, 'bzip2')
     self.assertNotEqual(text, c)
     d = compression.decompress(c, ctype)
     self.assertEqual(d, text)
Exemplo n.º 6
0
    def test_compress__decompress__zstd(self):
        pytest.importorskip('zstandard')

        text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
        c, ctype = compression.compress(text, 'zstd')
        assert text != c
        d = compression.decompress(c, ctype)
        assert d == text
Exemplo n.º 7
0
    def test_compress__decompress__backports_lzma(self):
        pytest.importorskip('backports.lzma')

        text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
        c, ctype = compression.compress(text, 'lzma')
        assert text != c
        d = compression.decompress(c, ctype)
        assert d == text
Exemplo n.º 8
0
 def test_compress__decompress__bzip2(self):
     if not self.has_bzip2:
         raise SkipTest('bzip2 not available')
     text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
     c, ctype = compression.compress(text, 'bzip2')
     self.assertNotEqual(text, c)
     d = compression.decompress(c, ctype)
     self.assertEqual(d, text)
Exemplo n.º 9
0
 def test_serializable(self):
     c = client().channel()
     body, content_type = compress('the quick brown fox...', 'gzip')
     data = c.prepare_message(body, headers={'compression': content_type})
     tag = data['properties']['delivery_tag'] = uuid()
     message = c.message_to_python(data)
     dict_ = message.serializable()
     assert dict_['body'] == 'the quick brown fox...'.encode('utf-8')
     assert dict_['properties']['delivery_tag'] == tag
     assert 'compression' not in dict_['headers']
Exemplo n.º 10
0
 def test_serializable(self):
     c = client().channel()
     body, content_type = compress('the quick brown fox...', 'gzip')
     data = c.prepare_message(body, headers={'compression': content_type})
     tag = data['properties']['delivery_tag'] = uuid()
     message = c.message_to_python(data)
     dict_ = message.serializable()
     assert dict_['body'] == b'the quick brown fox...'
     assert dict_['properties']['delivery_tag'] == tag
     assert 'compression' not in dict_['headers']
Exemplo n.º 11
0
 def test_serializable(self):
     c = client().channel()
     body, content_type = compress('the quick brown fox...', 'gzip')
     data = c.prepare_message(body, headers={'compression': content_type})
     tag = data['properties']['delivery_tag'] = uuid()
     message = c.message_to_python(data)
     dict_ = message.serializable()
     self.assertEqual(dict_['body'],
                      'the quick brown fox...'.encode('utf-8'))
     self.assertEqual(dict_['properties']['delivery_tag'], tag)
     self.assertNotIn('compression', dict_['headers'])
Exemplo n.º 12
0
def get_encoded_test_message(method='foo', args={'bar': 'foo_arg'},
                             class_name=A.__class__.__name__,
                             reply_to=None, delivery_tag=None):
    with Connection('memory://') as conn:
        ch = conn.channel()

        body = {'method': method, 'args': args, 'class': class_name}
        c_body, compression = compress(str(body), 'gzip')
        data = ch.prepare_message(c_body, content_type='application/json',
                                  content_encoding='utf-8',
                                  headers={'compression': compression})
        data['properties']['reply_to'] = reply_to
        delivery_tag = delivery_tag or uuid()
        data['properties']['delivery_tag'] = delivery_tag
        return body, ch.message_to_python(data)
Exemplo n.º 13
0
def get_encoded_test_message(method='foo',
                             args={'bar': 'foo_arg'},
                             class_name=A.__class__.__name__,
                             reply_to=None,
                             delivery_tag=None):
    with Connection('memory://') as conn:
        ch = conn.channel()

        body = {'method': method, 'args': args, 'class': class_name}
        c_body, compression = compress(str(body), 'gzip')
        data = ch.prepare_message(c_body,
                                  content_type='application/json',
                                  content_encoding='utf-8',
                                  headers={'compression': compression})
        data['properties']['reply_to'] = reply_to
        delivery_tag = delivery_tag or uuid()
        data['properties']['delivery_tag'] = delivery_tag
        return body, ch.message_to_python(data)
Exemplo n.º 14
0
 def test_compress__decompress__zlib(self):
     text = b"The Quick Brown Fox Jumps Over The Lazy Dog"
     c, ctype = compression.compress(text, "zlib")
     assert text != c
     d = compression.decompress(c, ctype)
     assert d == text
Exemplo n.º 15
0
 def test_compress__decompress__zlib(self):
     text = b'The Quick Brown Fox Jumps Over The Lazy Dog'
     c, ctype = compression.compress(text, 'zlib')
     self.assertNotEqual(text, c)
     d = compression.decompress(c, ctype)
     self.assertEqual(d, text)
Exemplo n.º 16
0
    def test_compress__decompress__brotli(self):
        pytest.importorskip('brotli')

        text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
        c, ctype = compression.compress(text, 'brotli')
Exemplo n.º 17
0
 def test_compress__decompress__bzip2(self):
     text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
     c, ctype = compression.compress(text, 'bzip2')
     assert text != c
     d = compression.decompress(c, ctype)
     assert d == text
Exemplo n.º 18
0
 def test_compress__decompress__zlib(self):
     text = b'The Quick Brown Fox Jumps Over The Lazy Dog'
     c, ctype = compression.compress(text, 'zlib')
     assert text != c
     d = compression.decompress(c, ctype)
     assert d == text
Exemplo n.º 19
0
 def test_compress__decompress__bzip2(self):
     text = b"The Brown Quick Fox Over The Lazy Dog Jumps"
     c, ctype = compression.compress(text, "bzip2")
     assert text != c
     d = compression.decompress(c, ctype)
     assert d == text
Exemplo n.º 20
0
 def test_compress__decompress__zlib(self):
     text = "The Quick Brown Fox Jumps Over The Lazy Dog"
     c, ctype = compression.compress(text, "zlib")
     self.assertNotEqual(text, c)
     d = compression.decompress(c, ctype)
     self.assertEqual(d, text)
Exemplo n.º 21
0
 def test_compress__decompress__bzip2(self):
     text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
     c, ctype = compression.compress(text, 'bzip2')
     self.assertNotEqual(text, c)
     d = compression.decompress(c, ctype)
     self.assertEqual(d, text)
Exemplo n.º 22
0
 def test_compress__decompress__bzip2(self):
     text = "The Brown Quick Fox Over The Lazy Dog Jumps"
     c, ctype = compression.compress(text, "bzip2")
     self.assertNotEqual(text, c)
     d = compression.decompress(c, ctype)
     self.assertEqual(d, text)
Exemplo n.º 23
0
    def test_compress__decompress__brotli(self):
        pytest.importorskip('brotli')

        text = b'The Brown Quick Fox Over The Lazy Dog Jumps'
        c, ctype = compression.compress(text, 'brotli')