forked from suds-community/suds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sax_encoder.py
182 lines (159 loc) · 6.18 KB
/
test_sax_encoder.py
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
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
# This program is free software; you can redistribute it and/or modify it under
# the terms of the (LGPL) GNU Lesser General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Library Lesser General Public License
# for more details at ( http://www.gnu.org/licenses/lgpl.html ).
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# written by: Jurko Gospodnetić ( [email protected] )
"""
Suds SAX module's special character encoder unit tests.
Implemented using the 'pytest' testing framework.
"""
if __name__ == "__main__":
import testutils
testutils.run_using_pytest(globals())
import suds.sax.enc
import pytest
"""Data not affected by encode()/decode() operations."""
invariant_decoded_encoded_test_data = (
# Empty text.
"",
# Pure text.
"x",
"xyz",
"Devil take the hindmost",
# Whitespace handling.
"spaces kept",
" ",
" ",
"\t",
"\v",
"\f",
"\r",
"\n",
" \t\t\v\v \f\f \v\v\f\r\n\n\r \t \t\t",
" \t\f\v something \r\r\n\freal\f\f\r\n\f\t\f")
"""
Data that should not be affected by encode()/decode() operations but for which
the current encode()/decode() operation implementations are broken.
"""
invariant_decoded_encoded_test_data__broken = (
# CDATA handling.
"<![CDATA['\"&<> \t\r\n\v\f < > ' " &]]>",
"<![CDATA[\\'\\\"\\&\\<\\> \\< \\> \\' \\" ]\\&]]>",
"<![CDATA[&&<><><><>>>>><<<< This is !!!&& & a test...."
"<<>>>>>>>>}}>]?]>>>>]]>")
"""
Data that should not be affected by encode()/decode() operations but for which
the current encode() operation implementation is broken.
"""
invariant_decoded_encoded_test_data__broken_encode = (
# CDATA handling.
"<![CDATA[]]>",
"<![CDATA[wonderful]]>",
"<![CDATA[&]] >]]>",
"<![CDATA[\"It's a wonderful life!\", said the smurf.]]>",
"<![CDATA[<![CDATA[<![CDATA[<![CDATA[]]>",
"<![CDATA[<![CDATA[]]]]><![CDATA[>]]>")
"""
Decoded/encoded data convertible in either direction using encode()/decode()
operations.
"""
symmetric_decoded_encoded_test_data = [
# Simple replacements.
("<", "<"),
(">", ">"),
("'", "'"),
('"', """),
("&", "&"),
# Mixed replacements.
("abcd&&<<", "abcd&&<<"),
# Character reference lookalikes.
("& lt;", "& lt;"),
("> ;", "&gt ;"),
("&a pos;", "&a pos;"),
("&walle;", "&walle;"),
(""", "&quot"),
("&quo", "&quo"),
("amp;", "amp;"),
# XML markup.
("<a>unga-bunga</a>", "<a>unga-bunga</a>"),
("<a></b>", "<a></b>"),
("<&></\n>", "<&></\n>"),
("<a id=\"fluffy's\"> && </a>",
"<a id="fluffy's"> && </a>"),
# CDATA handling.
("< ![CDATA[&]]>", "< ![CDATA[&]]>"),
("<! [CDATA[&]]>", "<! [CDATA[&]]>"),
("<![ CDATA[&]]>", "<![ CDATA[&]]>"),
("<![CDATA [&]]>", "<![CDATA [&]]>")] + [
# Invarant data.
(x, x) for x in invariant_decoded_encoded_test_data]
"""
Decoded/encoded data that should be convertible in either direction using
encode()/decode() operations but for which the current encode()/decode()
operation implementations are broken.
"""
symmetric_decoded_encoded_test_data__broken = [
# CDATA handling.
(x, x) for x in invariant_decoded_encoded_test_data__broken]
"""
Decoded/encoded data that should be convertible in either direction using
encode()/decode() operations but for which the current encode() operation
implementation is broken.
"""
symmetric_decoded_encoded_test_data__broken_encode = [
# CDATA handling.
(x, x) for x in invariant_decoded_encoded_test_data__broken_encode] + [
("<a><![CDATA[<a></a>]]></a>", "<a><![CDATA[<a></a>]]></a>"),
("&<![CDATA[&<![CDATA[&]]>&]]>&",
"&<![CDATA[&<![CDATA[&]]>&]]>&")]
@pytest.mark.parametrize(("input", "expected"), [
(e, d) for d, e in
symmetric_decoded_encoded_test_data +
symmetric_decoded_encoded_test_data__broken_encode] + [
pytest.param(e, d, marks=pytest.mark.xfail(reason="CDATA encoding not supported yet"))
for d, e in symmetric_decoded_encoded_test_data__broken] + [
# Character reference lookalikes.
(x, x) for x in (
"& lt;",
"> ;",
"&a pos;",
"&walle;",
""",
"&quo",
"amp;")] + [
# Double decode.
("&amp;", "&"),
("&lt;", "<"),
("&gt;", ">"),
("&apos;", "'"),
("&quot;", """)])
def test_decode(input, expected):
assert suds.sax.enc.Encoder().decode(input) == expected
@pytest.mark.parametrize(("input", "expected"),
symmetric_decoded_encoded_test_data + [
pytest.param(x, y, marks=pytest.mark.xfail(reason="CDATA encoding not supported yet")) for x, y in
symmetric_decoded_encoded_test_data__broken +
symmetric_decoded_encoded_test_data__broken_encode] + [
# Double encoding.
#TODO: See whether this 'avoid double encoding' behaviour is actually
# desirable. That is how XML entity reference encoding has been implemented
# in the original suds implementation, but it makes encode/decode
# operations asymmetric and prevents the user from actually encoding data
# like '&' that should not be interpreted as '&'.
("&", "&"),
("<", "<"),
(">", ">"),
("'", "'"),
(""", """)])
def test_encode(input, expected):
assert suds.sax.enc.Encoder().encode(input) == expected