File: test_queue_encodings.py

package info (click to toggle)
python-azure-storage 20181109%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 76,472 kB
  • sloc: python: 28,724; makefile: 204; sh: 1
file content (175 lines) | stat: -rw-r--r-- 5,351 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import unittest

from azure.common import (
    AzureHttpError,
    AzureException,
)

from azure.storage.queue import (
    QueueService,
    QueueMessageFormat,
)
from tests.testcase import (
    StorageTestCase,
    record,
)

# ------------------------------------------------------------------------------
TEST_QUEUE_PREFIX = 'mytestqueue'


# ------------------------------------------------------------------------------

class StorageQueueEncodingTest(StorageTestCase):
    def setUp(self):
        super(StorageQueueEncodingTest, self).setUp()

        self.qs = self._create_storage_service(QueueService, self.settings)
        self.test_queues = []

    def tearDown(self):
        if not self.is_playback():
            for queue_name in self.test_queues:
                try:
                    self.qs.delete_queue(queue_name)
                except:
                    pass
        return super(StorageQueueEncodingTest, self).tearDown()

    # --Helpers-----------------------------------------------------------------
    def _get_queue_reference(self):
        queue_name = self.get_resource_name(TEST_QUEUE_PREFIX + str(len(self.test_queues)))
        self.test_queues.append(queue_name)
        return queue_name

    def _create_queue(self):
        queue_name = self._get_queue_reference()
        self.qs.create_queue(queue_name)
        return queue_name

    def _validate_encoding(self, qs, message):
        # Arrange
        queue_name = self._create_queue()

        # Action.
        qs.put_message(queue_name, message)

        # Asserts
        messages = qs.get_messages(queue_name)
        self.assertEqual(message, messages[0].content)

    # --------------------------------------------------------------------------

    @record
    def test_message_text_xml(self):
        # Arrange.
        message = u'<message1>'

        # Asserts
        self._validate_encoding(self.qs, message)

    @record
    def test_message_text_xml_whitespace(self):
        # Arrange.
        message = u'  mess\t age1\n'

        # Asserts
        self._validate_encoding(self.qs, message)

    @record
    def test_message_text_xml_invalid_chars(self):
        # Action.
        queue_name = self._get_queue_reference()
        message = u'\u0001'

        # Asserts
        with self.assertRaises(AzureHttpError):
            self.qs.put_message(queue_name, message)

    @record
    def test_message_text_base64(self):
        # Arrange.
        qs2 = self._create_storage_service(QueueService, self.settings)
        qs2.encode_function = QueueMessageFormat.text_base64encode
        qs2.decode_function = QueueMessageFormat.text_base64decode
        message = u'\u0001'

        # Asserts
        self._validate_encoding(qs2, message)

    @record
    def test_message_bytes_base64(self):
        # Arrange.
        qs2 = self._create_storage_service(QueueService, self.settings)
        qs2.encode_function = QueueMessageFormat.binary_base64encode
        qs2.decode_function = QueueMessageFormat.binary_base64decode
        message = b'xyz'

        # Asserts
        self._validate_encoding(qs2, message)

    @record
    def test_message_bytes_fails(self):
        # Arrange
        queue_name = self._get_queue_reference()

        # Action.
        try:
            message = b'xyz'
            self.qs.put_message(queue_name, message)
            self.fail('Passing binary to text encoder should fail.')
        except TypeError as e:
            self.assertTrue(str(e).startswith('message should be of type'))

            # Asserts

    @record
    def test_message_text_fails(self):
        # Arrange
        qs2 = self._create_storage_service(QueueService, self.settings)
        qs2.encode_function = QueueMessageFormat.binary_base64encode
        qs2.decode_function = QueueMessageFormat.binary_base64decode

        queue_name = self._get_queue_reference()

        # Action.
        try:
            message = u'xyz'
            qs2.put_message(queue_name, message)
            self.fail('Passing text to binary encoder should fail.')
        except TypeError as e:
            self.assertEqual(str(e), 'message should be of type bytes.')

            # Asserts

    @record
    def test_message_base64_decode_fails(self):
        # Arrange
        qs2 = self._create_storage_service(QueueService, self.settings)
        qs2.encode_function = QueueMessageFormat.text_xmlencode
        qs2.decode_function = QueueMessageFormat.binary_base64decode

        queue_name = self._create_queue()
        message = u'xyz'
        qs2.put_message(queue_name, message)

        # Action.
        try:
            qs2.peek_messages(queue_name)
            self.fail('Decoding unicode string as base64 should fail.')
        except AzureException as e:
            self.assertNotEqual(-1, str(e).find('message is not a valid base64 value.'))

            # Asserts


# ------------------------------------------------------------------------------
if __name__ == '__main__':
    unittest.main()