FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
querystring-parser/querystring_parser/tests.py at master · ashl1/querystring-parser · GitHub
ashl1
/
querystring-parser
Public
forked from
bernii/querystring-parser
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
querystring-parser
/
querystring_parser
/
tests.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
177 lines (154 loc) · 11.2 KB
Breadcrumbs
querystring-parser
/
querystring_parser
/
tests.py
Copy path
File metadata and controls
177 lines (154 loc) · 11.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# coding: utf-8
'''
Created on 2011-05-13
@author: berni
Updated 2012-03-28 Tomasz 'Doppler' Najdek
Updated 2012-09-24 Bernard 'berni' Kobos
'''
import
sys
from
parser
import
parse
,
MalformedQueryStringError
from
builder
import
build
import
unittest
class
KnownValues
(
unittest
.
TestCase
):
'''
Test output for known query string values
'''
knownValuesClean
=
(
({
u'omg'
: {
0
:
u'0001212'
}}),
(
# "packetname=fd§ion[0]['words'][2]=§ion[0]['words'][2]=&language=1&packetdesc=sdfsd&newlanguage=proponowany jezyk..&newsectionname=§ion[0]['words'][1]=§ion[0]['words'][1]=&packettype=radio§ion[0]['words'][0]=sdfsd§ion[0]['words'][0]=ds",
{
u"packetname"
:
u"fd"
,
u"section"
: {
0
: {
u"words"
: {
0
: [
u"sdfsd"
,
u"ds"
],
1
: [
u""
,
u""
],
2
: [
u""
,
u""
]}}},
u"language"
:
u"1"
,
u"packetdesc"
:
u"sdfsd"
,
u"newlanguage"
:
u"proponowany jezyk.."
,
u"newsectionname"
:
u""
,
u"packettype"
:
u"radio"
}
),
(
# "language=1&newlanguage=proponowany jezyk..&newsectionname=&packetdesc=Zajebiste slowka na jutrzejszy sprawdzian z chemii&packetid=4&packetname=Chemia spr&packettype=1§ion[10]['name']=sekcja siatkarska§ion[10]['words'][-1]=§ion[10]['words'][-1]=§ion[10]['words'][-2]=§ion[10]['words'][-2]=§ion[10]['words'][30]=noga§ion[10]['words'][30]=leg§ion[11]['del_words'][32]=kciuk§ion[11]['del_words'][32]=thimb§ion[11]['del_words'][33]=oko§ion[11]['del_words'][33]=an eye§ion[11]['name']=sekcja siatkarska1§ion[11]['words'][-1]=§ion[11]['words'][-1]=§ion[11]['words'][-2]=§ion[11]['words'][-2]=§ion[11]['words'][31]=renca§ion[11]['words'][31]=rukka§ion[12]['name']=sekcja siatkarska2§ion[12]['words'][-1]=§ion[12]['words'][-1]=§ion[12]['words'][-2]=§ion[12]['words'][-2]=§ion[12]['words'][34]=wlos§ion[12]['words'][34]=a hair§ionnew=sekcja siatkarska§ionnew=sekcja siatkarska1§ionnew=sekcja siatkarska2&tags=dance, angielski, taniec",
{
u"packetdesc"
:
u"Zajebiste slowka na jutrzejszy sprawdzian z chemii"
,
u"packetid"
:
u"4"
,
u"packetname"
:
u"Chemia spr"
,
u"section"
: {
10
: {
u"words"
: {
-
1
: [
u""
,
u""
],
-
2
: [
u""
,
u""
],
30
: [
u"noga"
,
u"leg"
]},
u"name"
:
u"sekcja siatkarska"
},
11
: {
u"words"
: {
-
1
: [
u""
,
u""
],
-
2
: [
u""
,
u""
],
31
: [
u"renca"
,
u"rukka"
]},
u"del_words"
: {
32
: [
u"kciuk"
,
u"thimb"
],
33
: [
u"oko"
,
u"an eye"
]},
u"name"
:
u"sekcja siatkarska1"
},
12
: {
u"words"
: {
-
1
: [
u""
,
u""
],
-
2
: [
u""
,
u""
],
34
: [
u"wlos"
,
u"a hair"
]},
u"name"
:
u"sekcja siatkarska2"
}},
u"language"
:
u"1"
,
u"newlanguage"
:
u"proponowany jezyk.."
,
u"packettype"
:
u"1"
,
u"tags"
:
u"dance, angielski, taniec"
,
u"newsectionname"
:
u""
,
u"sectionnew"
: [
u"sekcja siatkarska"
,
u"sekcja siatkarska1"
,
u"sekcja siatkarska2"
]}
),
(
# "f=a hair§ionnew[]=sekcja siatkarska§ionnew[]=sekcja siatkarska1§ionnew[]=sekcja siatkarska2",
{
u"f"
:
u"a hair"
,
u"sectionnew"
: {
u""
: [
u"sekcja siatkarska"
,
u"sekcja siatkarska1"
,
u"sekcja siatkarska2"
]}}
),
# f = a
({
u"f"
:
u"a"
}),
# ""
({}),
)
knownValues
=
(
({
u'omg'
: {
0
:
u'0001212'
}}),
(
# "packetname=f%26d§ion%5B0%5D%5B%27words%27%5D%5B2%5D=§ion%5B0%5D%5B%27words%27%5D%5B2%5D=&language=1&packetdesc=sdfsd&newlanguage=proponowany+jezyk..&newsectionname=§ion%5B0%5D%5B%27words%27%5D%5B1%5D=§ion%5B0%5D%5B%27words%27%5D%5B1%5D=&packettype=radio§ion%5B0%5D%5B%27words%27%5D%5B0%5D=sdfsd§ion%5B0%5D%5B%27words%27%5D%5B0%5D=ds",
{
u"packetname"
:
u"f&d"
,
u"section"
: {
0
: {
u"words"
: {
0
: [
u"sdfsd"
,
u"ds"
],
1
: [
u""
,
u""
],
2
: [
u""
,
u""
]}}},
u"language"
:
u"1"
,
u"packetdesc"
:
u"sdfsd"
,
u"newlanguage"
:
u"proponowany jezyk.."
,
u"newsectionname"
:
u""
,
u"packettype"
:
u"radio"
}
),
(
# "language=1&newlanguage=proponowany+jezyk..&newsectionname=&packetdesc=Zajebiste+slowka+na+jutrzejszy+sprawdzian+z+chemii&packetid=4&packetname=Chemia+spr&packettype=1§ion%5B10%5D%5B%27name%27%5D=sekcja+siatkarska§ion%5B10%5D%5B%27words%27%5D%5B-1%5D=§ion%5B10%5D%5B%27words%27%5D%5B-1%5D=§ion%5B10%5D%5B%27words%27%5D%5B-2%5D=§ion%5B10%5D%5B%27words%27%5D%5B-2%5D=§ion%5B10%5D%5B%27words%27%5D%5B30%5D=noga§ion%5B10%5D%5B%27words%27%5D%5B30%5D=leg§ion%5B11%5D%5B%27del_words%27%5D%5B32%5D=kciuk§ion%5B11%5D%5B%27del_words%27%5D%5B32%5D=thimb§ion%5B11%5D%5B%27del_words%27%5D%5B33%5D=oko§ion%5B11%5D%5B%27del_words%27%5D%5B33%5D=an+eye§ion%5B11%5D%5B%27name%27%5D=sekcja+siatkarska1§ion%5B11%5D%5B%27words%27%5D%5B-1%5D=§ion%5B11%5D%5B%27words%27%5D%5B-1%5D=§ion%5B11%5D%5B%27words%27%5D%5B-2%5D=§ion%5B11%5D%5B%27words%27%5D%5B-2%5D=§ion%5B11%5D%5B%27words%27%5D%5B31%5D=renca§ion%5B11%5D%5B%27words%27%5D%5B31%5D=rukka§ion%5B12%5D%5B%27name%27%5D=sekcja+siatkarska2§ion%5B12%5D%5B%27words%27%5D%5B-1%5D=§ion%5B12%5D%5B%27words%27%5D%5B-1%5D=§ion%5B12%5D%5B%27words%27%5D%5B-2%5D=§ion%5B12%5D%5B%27words%27%5D%5B-2%5D=§ion%5B12%5D%5B%27words%27%5D%5B34%5D=wlos§ion%5B12%5D%5B%27words%27%5D%5B34%5D=a+hair§ionnew=sekcja%3Dsiatkarska§ionnew=sekcja+siatkarska1§ionnew=sekcja+siatkarska2&tags=dance%2C+angielski%2C+taniec",
{
u"packetdesc"
:
u"Zajebiste slowka na jutrzejszy sprawdzian z chemii"
,
u"packetid"
:
u"4"
,
u"packetname"
:
u"Chemia spr"
,
u"section"
: {
10
: {
u"words"
: {
-
1
: [
u""
,
u""
],
-
2
: [
u""
,
u""
],
30
: [
u"noga"
,
u"leg"
]},
u"name"
:
u"sekcja siatkarska"
},
11
: {
u"words"
: {
-
1
: [
u""
,
u""
],
-
2
: [
u""
,
u""
],
31
: [
u"renca"
,
u"rukka"
]},
u"del_words"
: {
32
: [
u"kciuk"
,
u"thimb"
],
33
: [
u"oko"
,
u"an eye"
]},
u"name"
:
u"sekcja siatkarska1"
},
12
: {
u"words"
: {
-
1
: [
u""
,
u""
],
-
2
: [
u""
,
u""
],
34
: [
u"wlos"
,
u"a hair"
]},
u"name"
:
u"sekcja siatkarska2"
}},
u"language"
:
u"1"
,
u"newlanguage"
:
u"proponowany jezyk.."
,
u"packettype"
:
u"1"
,
u"tags"
:
u"dance, angielski, taniec"
,
u"newsectionname"
:
""
,
u"sectionnew"
: [
u"sekcja=siatkarska"
,
u"sekcja siatkarska1"
,
u"sekcja siatkarska2"
]}
),
(
# "f=a+hair§ionnew%5B%5D=sekcja+siatkarska§ionnew%5B%5D=sekcja+siatkarska1§ionnew%5B%5D=sekcja+siatkarska2",
{
u"f"
:
u"a hair"
,
u"sectionnew"
: {
u""
: [
u"sekcja siatkarska"
,
u"sekcja siatkarska1"
,
u"sekcja siatkarska2"
]}}
),
# f = a
({
u"f"
:
u"a"
}),
# ""
({}),
)
knownValuesCleanWithUnicode
=
(
# f = some unicode
({
u"f"
:
u"
\u9017
"
}),
)
knownValuesWithUnicode
=
(
# f = some unicode
({
u"f"
:
u"
\u9017
"
}),
)
def
test_parse_known_values_clean
(
self
):
"""parse should give known result with known input"""
self
.
maxDiff
=
None
for
dic
in
self
.
knownValuesClean
:
result
=
parse
(
build
(
dic
),
unquote
=
True
)
self
.
assertEqual
(
dic
,
result
)
def
test_parse_known_values
(
self
):
"""parse should give known result with known input (quoted)"""
self
.
maxDiff
=
None
for
dic
in
self
.
knownValues
:
result
=
parse
(
build
(
dic
))
self
.
assertEqual
(
dic
,
result
)
def
test_parse_known_values_clean_with_unicode
(
self
):
"""parse should give known result with known input"""
self
.
maxDiff
=
None
encoding
=
'utf-8'
if
sys
.
version_info
[
0
]
==
2
else
None
for
dic
in
self
.
knownValuesClean
+
self
.
knownValuesCleanWithUnicode
:
result
=
parse
(
build
(
dic
,
encoding
=
encoding
),
unquote
=
True
,
encoding
=
encoding
)
self
.
assertEqual
(
dic
,
result
)
def
test_parse_known_values_with_unicode
(
self
):
"""parse should give known result with known input (quoted)"""
self
.
maxDiff
=
None
encoding
=
'utf-8'
if
sys
.
version_info
[
0
]
==
2
else
None
for
dic
in
self
.
knownValues
+
self
.
knownValuesWithUnicode
:
result
=
parse
(
build
(
dic
,
encoding
=
encoding
),
encoding
=
encoding
)
self
.
assertEqual
(
dic
,
result
)
def
test_parse_unicode_input_string
(
self
):
"""https://github.com/bernii/querystring-parser/issues/15"""
qs
=
u'first_name=%D8%B9%D9%84%DB%8C'
expected
=
{
u'first_name'
:
u'
\u0639
\u0644
\u06cc
'
}
self
.
assertEqual
(
parse
(
qs
.
encode
(
'ascii'
)),
expected
)
self
.
assertEqual
(
parse
(
qs
),
expected
)
class
ParseBadInput
(
unittest
.
TestCase
):
'''
Test for exceptions when bad input is provided
'''
badQueryStrings
=
(
"f&a hair§ionnew[]=sekcja siatkarska§ionnew[]=sekcja siatkarska1§ionnew[]=sekcja siatkarska2"
,
"f=a hair§ionnew[=sekcja siatkarska§ionnew[]=sekcja siatkarska1§ionnew[]=sekcja siatkarska2"
,
"packetname==fd&newsectionname="
,
"packetname=fd&newsectionname=§ion[0]['words'][1"
,
"packetname=fd&newsectionname=&"
,
)
def
test_bad_input
(
self
):
"""parse should fail with malformed querystring"""
for
qstr
in
self
.
badQueryStrings
:
self
.
assertRaises
(
MalformedQueryStringError
,
parse
,
qstr
,
False
)
class
BuildUrl
(
unittest
.
TestCase
):
'''
Basic test to verify builder's functionality
'''
request_data
=
{
u"word"
:
u"easy"
,
u"more_words"
: [
u"medium"
,
u"average"
],
u"words_with_translation"
: {
u"hard"
:
u"trudny"
,
u"tough"
:
u"twardy"
},
u"words_nested"
: {
u"hard"
: [
u"trudny"
,
u"twardy"
]}
}
def
test_build
(
self
):
result
=
build
(
self
.
request_data
)
self
.
assertEquals
(
parse
(
result
),
self
.
request_data
)
def
test_end_to_end
(
self
):
self
.
maxDiff
=
None
querystring
=
build
(
self
.
request_data
)
result
=
parse
(
querystring
)
self
.
assertEquals
(
result
,
self
.
request_data
)
class
BuilderAndParser
(
unittest
.
TestCase
):
'''
Testing both builder and parser
'''
def
test_end_to_end
(
self
):
parsed
=
parse
(
'a[]=1&a[]=2'
)
result
=
build
(
parsed
)
self
.
assertEquals
(
result
,
"a[]=1&a[]=2"
)
if
__name__
==
"__main__"
:
unittest
.
main
()
Back
|
FazBrowse Home
|
New Git URL