Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
185e8359
Commit
185e8359
authored
Oct 29, 2006
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Completely convert test_httplib to unittest.
parent
872bcfbd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
103 deletions
+63
-103
Lib/test/output/test_httplib
Lib/test/output/test_httplib
+0
-13
Lib/test/test_httplib.py
Lib/test/test_httplib.py
+63
-90
No files found.
Lib/test/output/test_httplib
deleted
100644 → 0
View file @
872bcfbd
test_httplib
reply: 'HTTP/1.1 200 Ok\r\n'
Text
reply: 'HTTP/1.1 400.100 Not Ok\r\n'
BadStatusLine raised as expected
InvalidURL raised as expected
InvalidURL raised as expected
reply: 'HTTP/1.1 200 OK\r\n'
header: Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
header: Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"
reply: 'HTTP/1.1 200 OK\r\n'
header: Content-Length: 14432
Lib/test/test_httplib.py
View file @
185e8359
...
...
@@ -70,95 +70,68 @@ class HeaderTests(TestCase):
conn
.
request
(
'POST'
,
'/'
,
body
,
headers
)
self
.
assertEqual
(
conn
.
_buffer
.
count
[
header
.
lower
()],
1
)
# Collect output to a buffer so that we don't have to cope with line-ending
# issues across platforms. Specifically, the headers will have \r\n pairs
# and some platforms will strip them from the output file.
def
test
():
buf
=
StringIO
.
StringIO
()
_stdout
=
sys
.
stdout
try
:
sys
.
stdout
=
buf
_test
()
finally
:
sys
.
stdout
=
_stdout
# print individual lines with endings stripped
s
=
buf
.
getvalue
()
for
line
in
s
.
split
(
"
\
n
"
):
print
line
.
strip
()
def
_test
():
# Test HTTP status lines
body
=
"HTTP/1.1 200 Ok
\
r
\
n
\
r
\
n
Text"
sock
=
FakeSocket
(
body
)
resp
=
httplib
.
HTTPResponse
(
sock
,
1
)
resp
.
begin
()
print
resp
.
read
()
resp
.
close
()
body
=
"HTTP/1.1 400.100 Not Ok
\
r
\
n
\
r
\
n
Text"
sock
=
FakeSocket
(
body
)
resp
=
httplib
.
HTTPResponse
(
sock
,
1
)
try
:
class
BasicTest
(
TestCase
):
def
test_status_lines
(
self
):
# Test HTTP status lines
body
=
"HTTP/1.1 200 Ok
\
r
\
n
\
r
\
n
Text"
sock
=
FakeSocket
(
body
)
resp
=
httplib
.
HTTPResponse
(
sock
)
resp
.
begin
()
except
httplib
.
BadStatusLine
:
print
"BadStatusLine raised as expected"
else
:
print
"Expect BadStatusLine"
# Check invalid host_port
for
hp
in
(
"www.python.org:abc"
,
"www.python.org:"
):
try
:
h
=
httplib
.
HTTP
(
hp
)
except
httplib
.
InvalidURL
:
print
"InvalidURL raised as expected"
else
:
print
"Expect InvalidURL"
for
hp
,
h
,
p
in
((
"[fe80::207:e9ff:fe9b]:8000"
,
"fe80::207:e9ff:fe9b"
,
8000
),
(
"www.python.org:80"
,
"www.python.org"
,
80
),
(
"www.python.org"
,
"www.python.org"
,
80
),
(
"[fe80::207:e9ff:fe9b]"
,
"fe80::207:e9ff:fe9b"
,
80
)):
try
:
self
.
assertEqual
(
resp
.
read
(),
'Text'
)
resp
.
close
()
body
=
"HTTP/1.1 400.100 Not Ok
\
r
\
n
\
r
\
n
Text"
sock
=
FakeSocket
(
body
)
resp
=
httplib
.
HTTPResponse
(
sock
)
self
.
assertRaises
(
httplib
.
BadStatusLine
,
resp
.
begin
)
def
test_host_port
(
self
):
# Check invalid host_port
for
hp
in
(
"www.python.org:abc"
,
"www.python.org:"
):
self
.
assertRaises
(
httplib
.
InvalidURL
,
httplib
.
HTTP
,
hp
)
for
hp
,
h
,
p
in
((
"[fe80::207:e9ff:fe9b]:8000"
,
"fe80::207:e9ff:fe9b"
,
8000
),
(
"www.python.org:80"
,
"www.python.org"
,
80
),
(
"www.python.org"
,
"www.python.org"
,
80
),
(
"[fe80::207:e9ff:fe9b]"
,
"fe80::207:e9ff:fe9b"
,
80
)):
http
=
httplib
.
HTTP
(
hp
)
except
httplib
.
InvalidURL
:
print
"InvalidURL raised erroneously"
c
=
http
.
_conn
if
h
!=
c
.
host
:
raise
AssertionError
,
(
"Host incorrectly parsed"
,
h
,
c
.
host
)
if
p
!=
c
.
port
:
raise
AssertionError
,
(
"Port incorrectly parsed"
,
p
,
c
.
host
)
# test response with multiple message headers with the same field name.
text
=
(
'HTTP/1.1 200 OK
\
r
\
n
'
'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
\
r
\
n
'
'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";
'
' Path="/acme"
\
r
\
n
'
'
\
r
\
n
'
'No body
\
r
\
n
'
)
hdr
=
(
'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
'
', '
'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"'
)
s
=
FakeSocket
(
text
)
r
=
httplib
.
HTTPResponse
(
s
,
1
)
r
.
begin
(
)
cookies
=
r
.
getheader
(
"Set-Cookie"
)
if
cookies
!=
hdr
:
raise
AssertionError
,
"multiple headers not combined properly"
# Test that the library doesn't attempt to read any data
# from a HEAD request. (Tickles SF bug #622042.)
sock
=
FakeSocket
(
'HTTP/1.1 200 OK
\
r
\
n
'
'Content-Length: 14432
\
r
\
n
'
'
\
r
\
n
'
,
NoEOFStringIO
)
resp
=
httplib
.
HTTPResponse
(
sock
,
1
,
method
=
"HEAD"
)
resp
.
begin
()
if
resp
.
read
()
!=
""
:
raise
AssertionError
,
"Did not expect response from HEAD request"
resp
.
close
()
c
=
http
.
_conn
if
h
!=
c
.
host
:
self
.
fail
(
"Host incorrectly parsed: %s != %s"
%
(
h
,
c
.
host
))
if
p
!=
c
.
port
:
self
.
fail
(
"Port incorrectly parsed: %s != %s"
%
(
p
,
c
.
host
))
def
test_response_headers
(
self
):
# test response with multiple message headers with the same field name.
text
=
(
'HTTP/1.1 200 OK
\
r
\
n
'
'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
\
r
\
n
'
'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";
'
' Path="/acme"
\
r
\
n
'
'
\
r
\
n
'
'No body
\
r
\
n
'
)
hdr
=
(
'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
',
'
'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"'
)
s
=
FakeSocket
(
text
)
r
=
httplib
.
HTTPResponse
(
s
)
r
.
begin
(
)
cookies
=
r
.
getheader
(
"Set-Cookie"
)
if
cookies
!=
hdr
:
self
.
fail
(
"multiple headers not combined properly"
)
def
test_read_head
(
self
):
# Test that the library doesn't attempt to read any data
# from a HEAD request. (Tickles SF bug #622042.)
sock
=
FakeSocket
(
'HTTP/1.1 200 OK
\
r
\
n
'
'Content-Length: 14432
\
r
\
n
'
'
\
r
\
n
'
,
NoEOFStringIO
)
resp
=
httplib
.
HTTPResponse
(
sock
,
method
=
"HEAD"
)
resp
.
begin
()
if
resp
.
read
()
!=
""
:
self
.
fail
(
"Did not expect response from HEAD request"
)
resp
.
close
()
class
OfflineTest
(
TestCase
):
...
...
@@ -166,7 +139,7 @@ class OfflineTest(TestCase):
self
.
assertEquals
(
httplib
.
responses
[
httplib
.
NOT_FOUND
],
"Not Found"
)
def
test_main
(
verbose
=
None
):
tests
=
[
HeaderTests
,
OfflineTest
]
test_support
.
run_unittest
(
*
tests
)
test_support
.
run_unittest
(
HeaderTests
,
OfflineTest
,
BasicTest
)
test
()
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