Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
e1844336
Kaydet (Commit)
e1844336
authored
Eki 29, 2006
tarafından
Georg Brandl
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Convert test_cgi to unittest.
üst
a962eb32
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
135 additions
and
167 deletions
+135
-167
test_cgi
Lib/test/output/test_cgi
+0
-42
test_cgi.py
Lib/test/test_cgi.py
+135
-125
No files found.
Lib/test/output/test_cgi
deleted
100644 → 0
Dosyayı görüntüle @
a962eb32
test_cgi
'' => []
'&' => []
'&&' => []
'=' => [('', '')]
'=a' => [('', 'a')]
'a' => [('a', '')]
'a=' => [('a', '')]
'a=' => [('a', '')]
'&a=b' => [('a', 'b')]
'a=a+b&b=b+c' => [('a', 'a b'), ('b', 'b c')]
'a=1&a=2' => [('a', '1'), ('a', '2')]
''
'&'
'&&'
';'
';&;'
'='
'=&='
'=;='
'=a'
'&=a'
'=a&'
'=&a'
'b=a'
'b+=a'
'a=b=a'
'a=+b=a'
'&b=a'
'b&=a'
'a=a+b&b=b+c'
'a=a+b&a=b+a'
'x=1&y=2.0&z=2-3.%2b0'
'x=1;y=2.0&z=2-3.%2b0'
'x=1;y=2.0;z=2-3.%2b0'
'Hbc5161168c542333633315dee1182227:key_store_seqid=400006&cuyer=r&view=bustomer&order_id=0bb2e248638833d48cb7fed300000f1b&expire=964546263&lobale=en-US&kid=130003.300038&ss=env'
'group_id=5470&set=custom&_assigned_to=31392&_status=1&_category=100&SUBMIT=Browse'
Testing log
Testing initlog 1
Testing log 2
Test FieldStorage methods that use readline
Test basic FieldStorage multipart parsing
Lib/test/test_cgi.py
Dosyayı görüntüle @
e1844336
from
test.test_support
import
verify
,
verbose
from
test.test_support
import
run_unittest
import
cgi
import
os
import
sys
import
tempfile
import
unittest
from
StringIO
import
StringIO
class
HackedSysModule
:
...
...
@@ -129,119 +130,124 @@ def first_elts(list):
def
first_second_elts
(
list
):
return
map
(
lambda
p
:(
p
[
0
],
p
[
1
][
0
]),
list
)
def
main
():
for
orig
,
expect
in
parse_qsl_test_cases
:
result
=
cgi
.
parse_qsl
(
orig
,
keep_blank_values
=
True
)
print
repr
(
orig
),
'=>'
,
result
verify
(
result
==
expect
,
"Error parsing
%
s"
%
repr
(
orig
))
for
orig
,
expect
in
parse_strict_test_cases
:
# Test basic parsing
print
repr
(
orig
)
d
=
do_test
(
orig
,
"GET"
)
verify
(
d
==
expect
,
"Error parsing
%
s"
%
repr
(
orig
))
d
=
do_test
(
orig
,
"POST"
)
verify
(
d
==
expect
,
"Error parsing
%
s"
%
repr
(
orig
))
env
=
{
'QUERY_STRING'
:
orig
}
fcd
=
cgi
.
FormContentDict
(
env
)
sd
=
cgi
.
SvFormContentDict
(
env
)
fs
=
cgi
.
FieldStorage
(
environ
=
env
)
if
type
(
expect
)
==
type
({}):
# test dict interface
verify
(
len
(
expect
)
==
len
(
fcd
))
verify
(
norm
(
expect
.
keys
())
==
norm
(
fcd
.
keys
()))
verify
(
norm
(
expect
.
values
())
==
norm
(
fcd
.
values
()))
verify
(
norm
(
expect
.
items
())
==
norm
(
fcd
.
items
()))
verify
(
fcd
.
get
(
"nonexistent field"
,
"default"
)
==
"default"
)
verify
(
len
(
sd
)
==
len
(
fs
))
verify
(
norm
(
sd
.
keys
())
==
norm
(
fs
.
keys
()))
verify
(
fs
.
getvalue
(
"nonexistent field"
,
"default"
)
==
"default"
)
# test individual fields
for
key
in
expect
.
keys
():
expect_val
=
expect
[
key
]
verify
(
fcd
.
has_key
(
key
))
verify
(
norm
(
fcd
[
key
])
==
norm
(
expect
[
key
]))
verify
(
fcd
.
get
(
key
,
"default"
)
==
fcd
[
key
])
verify
(
fs
.
has_key
(
key
))
if
len
(
expect_val
)
>
1
:
single_value
=
0
class
CgiTests
(
unittest
.
TestCase
):
def
test_qsl
(
self
):
for
orig
,
expect
in
parse_qsl_test_cases
:
result
=
cgi
.
parse_qsl
(
orig
,
keep_blank_values
=
True
)
self
.
assertEqual
(
result
,
expect
,
"Error parsing
%
s"
%
repr
(
orig
))
def
test_strict
(
self
):
for
orig
,
expect
in
parse_strict_test_cases
:
# Test basic parsing
d
=
do_test
(
orig
,
"GET"
)
self
.
assertEqual
(
d
,
expect
,
"Error parsing
%
s"
%
repr
(
orig
))
d
=
do_test
(
orig
,
"POST"
)
self
.
assertEqual
(
d
,
expect
,
"Error parsing
%
s"
%
repr
(
orig
))
env
=
{
'QUERY_STRING'
:
orig
}
fcd
=
cgi
.
FormContentDict
(
env
)
sd
=
cgi
.
SvFormContentDict
(
env
)
fs
=
cgi
.
FieldStorage
(
environ
=
env
)
if
type
(
expect
)
==
type
({}):
# test dict interface
self
.
assertEqual
(
len
(
expect
),
len
(
fcd
))
self
.
assertEqual
(
norm
(
expect
.
keys
()),
norm
(
fcd
.
keys
()))
self
.
assertEqual
(
norm
(
expect
.
values
()),
norm
(
fcd
.
values
()))
self
.
assertEqual
(
norm
(
expect
.
items
()),
norm
(
fcd
.
items
()))
self
.
assertEqual
(
fcd
.
get
(
"nonexistent field"
,
"default"
),
"default"
)
self
.
assertEqual
(
len
(
sd
),
len
(
fs
))
self
.
assertEqual
(
norm
(
sd
.
keys
()),
norm
(
fs
.
keys
()))
self
.
assertEqual
(
fs
.
getvalue
(
"nonexistent field"
,
"default"
),
"default"
)
# test individual fields
for
key
in
expect
.
keys
():
expect_val
=
expect
[
key
]
self
.
assert_
(
fcd
.
has_key
(
key
))
self
.
assertEqual
(
norm
(
fcd
[
key
]),
norm
(
expect
[
key
]))
self
.
assertEqual
(
fcd
.
get
(
key
,
"default"
),
fcd
[
key
])
self
.
assert_
(
fs
.
has_key
(
key
))
if
len
(
expect_val
)
>
1
:
single_value
=
0
else
:
single_value
=
1
try
:
val
=
sd
[
key
]
except
IndexError
:
self
.
failIf
(
single_value
)
self
.
assertEqual
(
fs
.
getvalue
(
key
),
expect_val
)
else
:
self
.
assert_
(
single_value
)
self
.
assertEqual
(
val
,
expect_val
[
0
])
self
.
assertEqual
(
fs
.
getvalue
(
key
),
expect_val
[
0
])
self
.
assertEqual
(
norm
(
sd
.
getlist
(
key
)),
norm
(
expect_val
))
if
single_value
:
self
.
assertEqual
(
norm
(
sd
.
values
()),
first_elts
(
norm
(
expect
.
values
())))
self
.
assertEqual
(
norm
(
sd
.
items
()),
first_second_elts
(
norm
(
expect
.
items
())))
def
test_weird_formcontentdict
(
self
):
# Test the weird FormContentDict classes
env
=
{
'QUERY_STRING'
:
"x=1&y=2.0&z=2-3.
%2
b0&1=1abc"
}
expect
=
{
'x'
:
1
,
'y'
:
2.0
,
'z'
:
'2-3.+0'
,
'1'
:
'1abc'
}
d
=
cgi
.
InterpFormContentDict
(
env
)
for
k
,
v
in
expect
.
items
():
self
.
assertEqual
(
d
[
k
],
v
)
for
k
,
v
in
d
.
items
():
self
.
assertEqual
(
expect
[
k
],
v
)
self
.
assertEqual
(
norm
(
expect
.
values
()),
norm
(
d
.
values
()))
def
test_log
(
self
):
cgi
.
log
(
"Testing"
)
cgi
.
logfp
=
StringIO
()
cgi
.
initlog
(
"
%
s"
,
"Testing initlog 1"
)
cgi
.
log
(
"
%
s"
,
"Testing log 2"
)
self
.
assertEqual
(
cgi
.
logfp
.
getvalue
(),
"Testing initlog 1
\n
Testing log 2
\n
"
)
if
os
.
path
.
exists
(
"/dev/null"
):
cgi
.
logfp
=
None
cgi
.
logfile
=
"/dev/null"
cgi
.
initlog
(
"
%
s"
,
"Testing log 3"
)
cgi
.
log
(
"Testing log 4"
)
def
test_fieldstorage_readline
(
self
):
# FieldStorage uses readline, which has the capacity to read all
# contents of the input file into memory; we use readline's size argument
# to prevent that for files that do not contain any newlines in
# non-GET/HEAD requests
class
TestReadlineFile
:
def
__init__
(
self
,
file
):
self
.
file
=
file
self
.
numcalls
=
0
def
readline
(
self
,
size
=
None
):
self
.
numcalls
+=
1
if
size
:
return
self
.
file
.
readline
(
size
)
else
:
single_value
=
1
try
:
val
=
sd
[
key
]
except
IndexError
:
verify
(
not
single_value
)
verify
(
fs
.
getvalue
(
key
)
==
expect_val
)
else
:
verify
(
single_value
)
verify
(
val
==
expect_val
[
0
])
verify
(
fs
.
getvalue
(
key
)
==
expect_val
[
0
])
verify
(
norm
(
sd
.
getlist
(
key
))
==
norm
(
expect_val
))
if
single_value
:
verify
(
norm
(
sd
.
values
())
==
\
first_elts
(
norm
(
expect
.
values
())))
verify
(
norm
(
sd
.
items
())
==
\
first_second_elts
(
norm
(
expect
.
items
())))
# Test the weird FormContentDict classes
env
=
{
'QUERY_STRING'
:
"x=1&y=2.0&z=2-3.
%2
b0&1=1abc"
}
expect
=
{
'x'
:
1
,
'y'
:
2.0
,
'z'
:
'2-3.+0'
,
'1'
:
'1abc'
}
d
=
cgi
.
InterpFormContentDict
(
env
)
for
k
,
v
in
expect
.
items
():
verify
(
d
[
k
]
==
v
)
for
k
,
v
in
d
.
items
():
verify
(
expect
[
k
]
==
v
)
verify
(
norm
(
expect
.
values
())
==
norm
(
d
.
values
()))
print
"Testing log"
cgi
.
log
(
"Testing"
)
cgi
.
logfp
=
sys
.
stdout
cgi
.
initlog
(
"
%
s"
,
"Testing initlog 1"
)
cgi
.
log
(
"
%
s"
,
"Testing log 2"
)
if
os
.
path
.
exists
(
"/dev/null"
):
cgi
.
logfp
=
None
cgi
.
logfile
=
"/dev/null"
cgi
.
initlog
(
"
%
s"
,
"Testing log 3"
)
cgi
.
log
(
"Testing log 4"
)
print
"Test FieldStorage methods that use readline"
# FieldStorage uses readline, which has the capacity to read all
# contents of the input file into memory; we use readline's size argument
# to prevent that for files that do not contain any newlines in
# non-GET/HEAD requests
class
TestReadlineFile
:
def
__init__
(
self
,
file
):
self
.
file
=
file
self
.
numcalls
=
0
def
readline
(
self
,
size
=
None
):
self
.
numcalls
+=
1
if
size
:
return
self
.
file
.
readline
(
size
)
else
:
return
self
.
file
.
readline
()
def
__getattr__
(
self
,
name
):
file
=
self
.
__dict__
[
'file'
]
a
=
getattr
(
file
,
name
)
if
not
isinstance
(
a
,
int
):
setattr
(
self
,
name
,
a
)
return
a
f
=
TestReadlineFile
(
tempfile
.
TemporaryFile
())
f
.
write
(
'x'
*
256
*
1024
)
f
.
seek
(
0
)
env
=
{
'REQUEST_METHOD'
:
'PUT'
}
fs
=
cgi
.
FieldStorage
(
fp
=
f
,
environ
=
env
)
# if we're not chunking properly, readline is only called twice
# (by read_binary); if we are chunking properly, it will be called 5 times
# as long as the chunksize is 1 << 16.
verify
(
f
.
numcalls
>
2
)
print
"Test basic FieldStorage multipart parsing"
env
=
{
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_TYPE'
:
'multipart/form-data; boundary=---------------------------721837373350705526688164684'
,
'CONTENT_LENGTH'
:
'558'
}
postdata
=
"""-----------------------------721837373350705526688164684
return
self
.
file
.
readline
()
def
__getattr__
(
self
,
name
):
file
=
self
.
__dict__
[
'file'
]
a
=
getattr
(
file
,
name
)
if
not
isinstance
(
a
,
int
):
setattr
(
self
,
name
,
a
)
return
a
f
=
TestReadlineFile
(
tempfile
.
TemporaryFile
())
f
.
write
(
'x'
*
256
*
1024
)
f
.
seek
(
0
)
env
=
{
'REQUEST_METHOD'
:
'PUT'
}
fs
=
cgi
.
FieldStorage
(
fp
=
f
,
environ
=
env
)
# if we're not chunking properly, readline is only called twice
# (by read_binary); if we are chunking properly, it will be called 5 times
# as long as the chunksize is 1 << 16.
self
.
assert_
(
f
.
numcalls
>
2
)
def
test_fieldstorage_multipart
(
self
):
#Test basic FieldStorage multipart parsing
env
=
{
'REQUEST_METHOD'
:
'POST'
,
'CONTENT_TYPE'
:
'multipart/form-data; boundary=---------------------------721837373350705526688164684'
,
'CONTENT_LENGTH'
:
'558'
}
postdata
=
"""-----------------------------721837373350705526688164684
Content-Disposition: form-data; name="id"
1234
...
...
@@ -261,15 +267,19 @@ Content-Disposition: form-data; name="submit"
Add
\x20
-----------------------------721837373350705526688164684--
"""
fs
=
cgi
.
FieldStorage
(
fp
=
StringIO
(
postdata
),
environ
=
env
)
verify
(
len
(
fs
.
list
)
==
4
)
expect
=
[{
'name'
:
'id'
,
'filename'
:
None
,
'value'
:
'1234'
},
{
'name'
:
'title'
,
'filename'
:
None
,
'value'
:
''
},
{
'name'
:
'file'
,
'filename'
:
'test.txt'
,
'value'
:
'Testing 123.
\n
'
},
{
'name'
:
'submit'
,
'filename'
:
None
,
'value'
:
' Add '
}]
for
x
in
range
(
len
(
fs
.
list
)):
for
k
,
exp
in
expect
[
x
]
.
items
():
got
=
getattr
(
fs
.
list
[
x
],
k
)
verify
(
got
==
exp
)
main
()
fs
=
cgi
.
FieldStorage
(
fp
=
StringIO
(
postdata
),
environ
=
env
)
self
.
assertEquals
(
len
(
fs
.
list
),
4
)
expect
=
[{
'name'
:
'id'
,
'filename'
:
None
,
'value'
:
'1234'
},
{
'name'
:
'title'
,
'filename'
:
None
,
'value'
:
''
},
{
'name'
:
'file'
,
'filename'
:
'test.txt'
,
'value'
:
'Testing 123.
\n
'
},
{
'name'
:
'submit'
,
'filename'
:
None
,
'value'
:
' Add '
}]
for
x
in
range
(
len
(
fs
.
list
)):
for
k
,
exp
in
expect
[
x
]
.
items
():
got
=
getattr
(
fs
.
list
[
x
],
k
)
self
.
assertEquals
(
got
,
exp
)
def
test_main
():
run_unittest
(
CgiTests
)
if
__name__
==
'__main__'
:
test_main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment