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
d281c735
Kaydet (Commit)
d281c735
authored
Nis 09, 2013
tarafından
Senthil Kumaran
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
#17648 - convert test_urllib2.py doctests to unittests
üst
0803a395
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
175 additions
and
199 deletions
+175
-199
test_urllib2.py
Lib/test/test_urllib2.py
+175
-199
No files found.
Lib/test/test_urllib2.py
Dosyayı görüntüle @
d281c735
...
@@ -50,7 +50,7 @@ class TrivialTests(unittest.TestCase):
...
@@ -50,7 +50,7 @@ class TrivialTests(unittest.TestCase):
f
=
urllib
.
request
.
urlopen
(
file_url
)
f
=
urllib
.
request
.
urlopen
(
file_url
)
buf
=
f
.
read
()
f
.
read
()
f
.
close
()
f
.
close
()
def
test_parse_http_list
(
self
):
def
test_parse_http_list
(
self
):
...
@@ -68,180 +68,158 @@ class TrivialTests(unittest.TestCase):
...
@@ -68,180 +68,158 @@ class TrivialTests(unittest.TestCase):
err
=
urllib
.
error
.
URLError
(
'reason'
)
err
=
urllib
.
error
.
URLError
(
'reason'
)
self
.
assertIn
(
err
.
reason
,
str
(
err
))
self
.
assertIn
(
err
.
reason
,
str
(
err
))
def
test_request_headers_dict
():
class
RequestHdrsTests
(
unittest
.
TestCase
):
"""
The Request.headers dictionary is not a documented interface. It should
stay that way, because the complete set of headers are only accessible
through the .get_header(), .has_header(), .header_items() interface.
However, .headers pre-dates those methods, and so real code will be using
the dictionary.
The introduction in 2.4 of those methods was a mistake for the same reason:
code that previously saw all (urllib2 user)-provided headers in .headers
now sees only a subset (and the function interface is ugly and incomplete).
A better change would have been to replace .headers dict with a dict
subclass (or UserDict.DictMixin instance?) that preserved the .headers
interface and also provided access to the "unredirected" headers. It's
probably too late to fix that, though.
Check .capitalize() case normalization:
>>> url = "http://example.com"
def
test_request_headers_dict
(
self
):
>>> Request(url, headers={"Spam-eggs": "blah"}).headers["Spam-eggs"]
"""
'blah'
The Request.headers dictionary is not a documented interface. It
>>> Request(url, headers={"spam-EggS": "blah"}).headers["Spam-eggs"]
should stay that way, because the complete set of headers are only
'blah'
accessible through the .get_header(), .has_header(), .header_items()
interface. However, .headers pre-dates those methods, and so real code
will be using the dictionary.
Currently, Request(url, "Spam-eggs").headers["Spam-Eggs"] raises KeyError,
The introduction in 2.4 of those methods was a mistake for the same
but that could be changed in future.
reason: code that previously saw all (urllib2 user)-provided headers in
.headers now sees only a subset.
"""
"""
url
=
"http://example.com"
def
test_request_headers_methods
():
self
.
assertEqual
(
Request
(
url
,
headers
=
{
"Spam-eggs"
:
"blah"
}
)
.
headers
[
"Spam-eggs"
],
"blah"
)
self
.
assertEqual
(
Request
(
url
,
headers
=
{
"spam-EggS"
:
"blah"
}
)
.
headers
[
"Spam-eggs"
],
"blah"
)
def
test_request_headers_methods
(
self
):
"""
"""
Note the case normalization of header names here, to .capitalize()-case.
Note the case normalization of header names here, to
This should be preserved for backwards-compatibility. (In the HTTP case,
.capitalize()-case. This should be preserved for
normalization to .title()-case is done by urllib2 before sending headers to
backwards-compatibility. (In the HTTP case, normalization to
.title()-case is done by urllib2 before sending headers to
http.client).
http.client).
>>> url = "http://example.com"
>>> r = Request(url, headers={"Spam-eggs": "blah"})
>>> r.has_header("Spam-eggs")
True
>>> r.header_items()
[('Spam-eggs', 'blah')]
>>> r.add_header("Foo-Bar", "baz")
>>> items = sorted(r.header_items())
>>> items
[('Foo-bar', 'baz'), ('Spam-eggs', 'blah')]
Note that e.g. r.has_header("spam-EggS") is currently False, and
Note that e.g. r.has_header("spam-EggS") is currently False, and
r.get_header("spam-EggS") returns None, but that could be changed in
r.get_header("spam-EggS") returns None, but that could be changed in
future.
future.
>>> r.has_header("Not-there")
Method r.remove_header should remove items both from r.headers and
False
r.unredirected_hdrs dictionaries
>>> print(r.get_header("Not-there"))
None
>>> r.get_header("Not-there", "default")
'default'
"""
"""
url
=
"http://example.com"
req
=
Request
(
url
,
headers
=
{
"Spam-eggs"
:
"blah"
})
def
test_password_manager
(
self
):
self
.
assertTrue
(
req
.
has_header
(
"Spam-eggs"
))
self
.
assertEqual
(
req
.
header_items
(),
[(
'Spam-eggs'
,
'blah'
)])
req
.
add_header
(
"Foo-Bar"
,
"baz"
)
self
.
assertEqual
(
sorted
(
req
.
header_items
()),
[(
'Foo-bar'
,
'baz'
),
(
'Spam-eggs'
,
'blah'
)])
self
.
assertFalse
(
req
.
has_header
(
"Not-there"
))
self
.
assertIsNone
(
req
.
get_header
(
"Not-there"
))
self
.
assertEqual
(
req
.
get_header
(
"Not-there"
,
"default"
),
"default"
)
def
test_password_manager
(
self
):
mgr
=
urllib
.
request
.
HTTPPasswordMgr
()
add
=
mgr
.
add_password
find_user_pass
=
mgr
.
find_user_password
add
(
"Some Realm"
,
"http://example.com/"
,
"joe"
,
"password"
)
add
(
"Some Realm"
,
"http://example.com/ni"
,
"ni"
,
"ni"
)
add
(
"c"
,
"http://example.com/foo"
,
"foo"
,
"ni"
)
add
(
"c"
,
"http://example.com/bar"
,
"bar"
,
"nini"
)
add
(
"b"
,
"http://example.com/"
,
"first"
,
"blah"
)
add
(
"b"
,
"http://example.com/"
,
"second"
,
"spam"
)
add
(
"a"
,
"http://example.com"
,
"1"
,
"a"
)
add
(
"Some Realm"
,
"http://c.example.com:3128"
,
"3"
,
"c"
)
add
(
"Some Realm"
,
"d.example.com"
,
"4"
,
"d"
)
add
(
"Some Realm"
,
"e.example.com:3128"
,
"5"
,
"e"
)
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"example.com"
),
(
'joe'
,
'password'
))
#self.assertEqual(find_user_pass("Some Realm", "http://example.com/ni"),
# ('ni', 'ni'))
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"http://example.com"
),
(
'joe'
,
'password'
))
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"http://example.com/"
),
(
'joe'
,
'password'
))
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"http://example.com/spam"
),
(
'joe'
,
'password'
))
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"http://example.com/spam/spam"
),
(
'joe'
,
'password'
))
self
.
assertEqual
(
find_user_pass
(
"c"
,
"http://example.com/foo"
),
(
'foo'
,
'ni'
))
self
.
assertEqual
(
find_user_pass
(
"c"
,
"http://example.com/bar"
),
(
'bar'
,
'nini'
))
self
.
assertEqual
(
find_user_pass
(
"b"
,
"http://example.com/"
),
(
'second'
,
'spam'
))
# No special relationship between a.example.com and example.com:
self
.
assertEqual
(
find_user_pass
(
"a"
,
"http://example.com/"
),
(
'1'
,
'a'
))
self
.
assertEqual
(
find_user_pass
(
"a"
,
"http://a.example.com/"
),
(
None
,
None
))
# Ports:
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"c.example.com"
),
(
None
,
None
))
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"c.example.com:3128"
),
(
'3'
,
'c'
))
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"http://c.example.com:3128"
),
(
'3'
,
'c'
))
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"d.example.com"
),
(
'4'
,
'd'
))
self
.
assertEqual
(
find_user_pass
(
"Some Realm"
,
"e.example.com:3128"
),
(
'5'
,
'e'
))
def
test_password_manager_default_port
(
self
):
"""
"""
>>> mgr = urllib.request.HTTPPasswordMgr()
The point to note here is that we can't guess the default port if
>>> add = mgr.add_password
there's no scheme. This applies to both add_password and
>>> add("Some Realm", "http://example.com/", "joe", "password")
find_user_password.
>>> add("Some Realm", "http://example.com/ni", "ni", "ni")
>>> add("c", "http://example.com/foo", "foo", "ni")
>>> add("c", "http://example.com/bar", "bar", "nini")
>>> add("b", "http://example.com/", "first", "blah")
>>> add("b", "http://example.com/", "second", "spam")
>>> add("a", "http://example.com", "1", "a")
>>> add("Some Realm", "http://c.example.com:3128", "3", "c")
>>> add("Some Realm", "d.example.com", "4", "d")
>>> add("Some Realm", "e.example.com:3128", "5", "e")
>>> mgr.find_user_password("Some Realm", "example.com")
('joe', 'password')
>>> mgr.find_user_password("Some Realm", "http://example.com")
('joe', 'password')
>>> mgr.find_user_password("Some Realm", "http://example.com/")
('joe', 'password')
>>> mgr.find_user_password("Some Realm", "http://example.com/spam")
('joe', 'password')
>>> mgr.find_user_password("Some Realm", "http://example.com/spam/spam")
('joe', 'password')
>>> mgr.find_user_password("c", "http://example.com/foo")
('foo', 'ni')
>>> mgr.find_user_password("c", "http://example.com/bar")
('bar', 'nini')
Actually, this is really undefined ATM
## Currently, we use the highest-level path where more than one match:
## >>> mgr.find_user_password("Some Realm", "http://example.com/ni")
## ('joe', 'password')
Use latest add_password() in case of conflict:
>>> mgr.find_user_password("b", "http://example.com/")
('second', 'spam')
No special relationship between a.example.com and example.com:
>>> mgr.find_user_password("a", "http://example.com/")
('1', 'a')
>>> mgr.find_user_password("a", "http://a.example.com/")
(None, None)
Ports:
>>> mgr.find_user_password("Some Realm", "c.example.com")
(None, None)
>>> mgr.find_user_password("Some Realm", "c.example.com:3128")
('3', 'c')
>>> mgr.find_user_password("Some Realm", "http://c.example.com:3128")
('3', 'c')
>>> mgr.find_user_password("Some Realm", "d.example.com")
('4', 'd')
>>> mgr.find_user_password("Some Realm", "e.example.com:3128")
('5', 'e')
"""
"""
pass
mgr
=
urllib
.
request
.
HTTPPasswordMgr
()
add
=
mgr
.
add_password
find_user_pass
=
mgr
.
find_user_password
add
(
"f"
,
"http://g.example.com:80"
,
"10"
,
"j"
)
add
(
"g"
,
"http://h.example.com"
,
"11"
,
"k"
)
add
(
"h"
,
"i.example.com:80"
,
"12"
,
"l"
)
add
(
"i"
,
"j.example.com"
,
"13"
,
"m"
)
self
.
assertEqual
(
find_user_pass
(
"f"
,
"g.example.com:100"
),
(
None
,
None
))
self
.
assertEqual
(
find_user_pass
(
"f"
,
"g.example.com:80"
),
(
'10'
,
'j'
))
self
.
assertEqual
(
find_user_pass
(
"f"
,
"g.example.com"
),
(
None
,
None
))
self
.
assertEqual
(
find_user_pass
(
"f"
,
"http://g.example.com:100"
),
(
None
,
None
))
self
.
assertEqual
(
find_user_pass
(
"f"
,
"http://g.example.com:80"
),
(
'10'
,
'j'
))
self
.
assertEqual
(
find_user_pass
(
"f"
,
"http://g.example.com"
),
(
'10'
,
'j'
))
self
.
assertEqual
(
find_user_pass
(
"g"
,
"h.example.com"
),
(
'11'
,
'k'
))
self
.
assertEqual
(
find_user_pass
(
"g"
,
"h.example.com:80"
),
(
'11'
,
'k'
))
self
.
assertEqual
(
find_user_pass
(
"g"
,
"http://h.example.com:80"
),
(
'11'
,
'k'
))
self
.
assertEqual
(
find_user_pass
(
"h"
,
"i.example.com"
),
(
None
,
None
))
self
.
assertEqual
(
find_user_pass
(
"h"
,
"i.example.com:80"
),
(
'12'
,
'l'
))
self
.
assertEqual
(
find_user_pass
(
"h"
,
"http://i.example.com:80"
),
(
'12'
,
'l'
))
self
.
assertEqual
(
find_user_pass
(
"i"
,
"j.example.com"
),
(
'13'
,
'm'
))
self
.
assertEqual
(
find_user_pass
(
"i"
,
"j.example.com:80"
),
(
None
,
None
))
self
.
assertEqual
(
find_user_pass
(
"i"
,
"http://j.example.com"
),
(
'13'
,
'm'
))
self
.
assertEqual
(
find_user_pass
(
"i"
,
"http://j.example.com:80"
),
(
None
,
None
))
def
test_password_manager_default_port
(
self
):
"""
>>> mgr = urllib.request.HTTPPasswordMgr()
>>> add = mgr.add_password
The point to note here is that we can't guess the default port if there's
no scheme. This applies to both add_password and find_user_password.
>>> add("f", "http://g.example.com:80", "10", "j")
>>> add("g", "http://h.example.com", "11", "k")
>>> add("h", "i.example.com:80", "12", "l")
>>> add("i", "j.example.com", "13", "m")
>>> mgr.find_user_password("f", "g.example.com:100")
(None, None)
>>> mgr.find_user_password("f", "g.example.com:80")
('10', 'j')
>>> mgr.find_user_password("f", "g.example.com")
(None, None)
>>> mgr.find_user_password("f", "http://g.example.com:100")
(None, None)
>>> mgr.find_user_password("f", "http://g.example.com:80")
('10', 'j')
>>> mgr.find_user_password("f", "http://g.example.com")
('10', 'j')
>>> mgr.find_user_password("g", "h.example.com")
('11', 'k')
>>> mgr.find_user_password("g", "h.example.com:80")
('11', 'k')
>>> mgr.find_user_password("g", "http://h.example.com:80")
('11', 'k')
>>> mgr.find_user_password("h", "i.example.com")
(None, None)
>>> mgr.find_user_password("h", "i.example.com:80")
('12', 'l')
>>> mgr.find_user_password("h", "http://i.example.com:80")
('12', 'l')
>>> mgr.find_user_password("i", "j.example.com")
('13', 'm')
>>> mgr.find_user_password("i", "j.example.com:80")
(None, None)
>>> mgr.find_user_password("i", "http://j.example.com")
('13', 'm')
>>> mgr.find_user_password("i", "http://j.example.com:80")
(None, None)
"""
class
MockOpener
:
class
MockOpener
:
addheaders
=
[]
addheaders
=
[]
def
open
(
self
,
req
,
data
=
None
,
timeout
=
socket
.
_GLOBAL_DEFAULT_TIMEOUT
):
def
open
(
self
,
req
,
data
=
None
,
timeout
=
socket
.
_GLOBAL_DEFAULT_TIMEOUT
):
...
@@ -434,7 +412,6 @@ class MockHTTPHandler(urllib.request.BaseHandler):
...
@@ -434,7 +412,6 @@ class MockHTTPHandler(urllib.request.BaseHandler):
self
.
requests
=
[]
self
.
requests
=
[]
def
http_open
(
self
,
req
):
def
http_open
(
self
,
req
):
import
email
,
http
.
client
,
copy
import
email
,
http
.
client
,
copy
from
io
import
StringIO
self
.
requests
.
append
(
copy
.
deepcopy
(
req
))
self
.
requests
.
append
(
copy
.
deepcopy
(
req
))
if
self
.
_count
==
0
:
if
self
.
_count
==
0
:
self
.
_count
=
self
.
_count
+
1
self
.
_count
=
self
.
_count
+
1
...
@@ -494,7 +471,7 @@ class OpenerDirectorTests(unittest.TestCase):
...
@@ -494,7 +471,7 @@ class OpenerDirectorTests(unittest.TestCase):
[(
"do_open"
,
"return self"
),
(
"proxy_open"
,
"return self"
)],
[(
"do_open"
,
"return self"
),
(
"proxy_open"
,
"return self"
)],
[(
"redirect_request"
,
"return self"
)],
[(
"redirect_request"
,
"return self"
)],
]
]
handlers
=
add_ordered_mock_handlers
(
o
,
meth_spec
)
add_ordered_mock_handlers
(
o
,
meth_spec
)
o
.
add_handler
(
urllib
.
request
.
UnknownHandler
())
o
.
add_handler
(
urllib
.
request
.
UnknownHandler
())
for
scheme
in
"do"
,
"proxy"
,
"redirect"
:
for
scheme
in
"do"
,
"proxy"
,
"redirect"
:
self
.
assertRaises
(
URLError
,
o
.
open
,
scheme
+
"://example.com/"
)
self
.
assertRaises
(
URLError
,
o
.
open
,
scheme
+
"://example.com/"
)
...
@@ -538,7 +515,7 @@ class OpenerDirectorTests(unittest.TestCase):
...
@@ -538,7 +515,7 @@ class OpenerDirectorTests(unittest.TestCase):
handlers
.
append
(
h
)
handlers
.
append
(
h
)
o
.
add_handler
(
h
)
o
.
add_handler
(
h
)
r
=
o
.
open
(
"http://example.com/"
)
o
.
open
(
"http://example.com/"
)
# handlers called in reverse order, thanks to their sort order
# handlers called in reverse order, thanks to their sort order
self
.
assertEqual
(
o
.
calls
[
0
][
0
],
handlers
[
1
])
self
.
assertEqual
(
o
.
calls
[
0
][
0
],
handlers
[
1
])
self
.
assertEqual
(
o
.
calls
[
1
][
0
],
handlers
[
0
])
self
.
assertEqual
(
o
.
calls
[
1
][
0
],
handlers
[
0
])
...
@@ -573,7 +550,7 @@ class OpenerDirectorTests(unittest.TestCase):
...
@@ -573,7 +550,7 @@ class OpenerDirectorTests(unittest.TestCase):
def
__eq__
(
self
,
other
):
return
True
def
__eq__
(
self
,
other
):
return
True
req
=
Request
(
"http://example.com/"
)
req
=
Request
(
"http://example.com/"
)
r
=
o
.
open
(
req
)
o
.
open
(
req
)
assert
len
(
o
.
calls
)
==
2
assert
len
(
o
.
calls
)
==
2
calls
=
[(
handlers
[
0
],
"http_open"
,
(
req
,)),
calls
=
[(
handlers
[
0
],
"http_open"
,
(
req
,)),
(
handlers
[
2
],
"http_error_302"
,
(
handlers
[
2
],
"http_error_302"
,
...
@@ -596,7 +573,7 @@ class OpenerDirectorTests(unittest.TestCase):
...
@@ -596,7 +573,7 @@ class OpenerDirectorTests(unittest.TestCase):
handlers
=
add_ordered_mock_handlers
(
o
,
meth_spec
)
handlers
=
add_ordered_mock_handlers
(
o
,
meth_spec
)
req
=
Request
(
"http://example.com/"
)
req
=
Request
(
"http://example.com/"
)
r
=
o
.
open
(
req
)
o
.
open
(
req
)
# processor methods are called on *all* handlers that define them,
# processor methods are called on *all* handlers that define them,
# not just the first handler that handles the request
# not just the first handler that handles the request
calls
=
[
calls
=
[
...
@@ -674,7 +651,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -674,7 +651,7 @@ class HandlerTests(unittest.TestCase):
import
ftplib
import
ftplib
data
=
"rheum rhaponicum"
data
=
"rheum rhaponicum"
h
=
NullFTPHandler
(
data
)
h
=
NullFTPHandler
(
data
)
o
=
h
.
parent
=
MockOpener
()
h
.
parent
=
MockOpener
()
for
url
,
host
,
port
,
user
,
passwd
,
type_
,
dirs
,
filename
,
mimetype
in
[
for
url
,
host
,
port
,
user
,
passwd
,
type_
,
dirs
,
filename
,
mimetype
in
[
(
"ftp://localhost/foo/bar/baz.html"
,
(
"ftp://localhost/foo/bar/baz.html"
,
...
@@ -917,7 +894,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -917,7 +894,7 @@ class HandlerTests(unittest.TestCase):
# break anything. Previously, a double slash directly after the host
# break anything. Previously, a double slash directly after the host
# could cause incorrect parsing.
# could cause incorrect parsing.
h
=
urllib
.
request
.
AbstractHTTPHandler
()
h
=
urllib
.
request
.
AbstractHTTPHandler
()
o
=
h
.
parent
=
MockOpener
()
h
.
parent
=
MockOpener
()
data
=
b
""
data
=
b
""
ds_urls
=
[
ds_urls
=
[
...
@@ -944,7 +921,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -944,7 +921,7 @@ class HandlerTests(unittest.TestCase):
# start with'/'
# start with'/'
h
=
urllib
.
request
.
AbstractHTTPHandler
()
h
=
urllib
.
request
.
AbstractHTTPHandler
()
o
=
h
.
parent
=
MockOpener
()
h
.
parent
=
MockOpener
()
weird_url
=
'http://www.python.org?getspam'
weird_url
=
'http://www.python.org?getspam'
req
=
Request
(
weird_url
)
req
=
Request
(
weird_url
)
...
@@ -987,7 +964,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -987,7 +964,7 @@ class HandlerTests(unittest.TestCase):
def
test_cookies
(
self
):
def
test_cookies
(
self
):
cj
=
MockCookieJar
()
cj
=
MockCookieJar
()
h
=
urllib
.
request
.
HTTPCookieProcessor
(
cj
)
h
=
urllib
.
request
.
HTTPCookieProcessor
(
cj
)
o
=
h
.
parent
=
MockOpener
()
h
.
parent
=
MockOpener
()
req
=
Request
(
"http://example.com/"
)
req
=
Request
(
"http://example.com/"
)
r
=
MockResponse
(
200
,
"OK"
,
{},
""
)
r
=
MockResponse
(
200
,
"OK"
,
{},
""
)
...
@@ -1144,7 +1121,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -1144,7 +1121,7 @@ class HandlerTests(unittest.TestCase):
req
=
Request
(
"http://acme.example.com/"
)
req
=
Request
(
"http://acme.example.com/"
)
self
.
assertEqual
(
req
.
host
,
"acme.example.com"
)
self
.
assertEqual
(
req
.
host
,
"acme.example.com"
)
r
=
o
.
open
(
req
)
o
.
open
(
req
)
self
.
assertEqual
(
req
.
host
,
"proxy.example.com:3128"
)
self
.
assertEqual
(
req
.
host
,
"proxy.example.com:3128"
)
self
.
assertEqual
([(
handlers
[
0
],
"http_open"
)],
self
.
assertEqual
([(
handlers
[
0
],
"http_open"
)],
...
@@ -1157,11 +1134,11 @@ class HandlerTests(unittest.TestCase):
...
@@ -1157,11 +1134,11 @@ class HandlerTests(unittest.TestCase):
o
.
add_handler
(
ph
)
o
.
add_handler
(
ph
)
req
=
Request
(
"http://www.perl.org/"
)
req
=
Request
(
"http://www.perl.org/"
)
self
.
assertEqual
(
req
.
host
,
"www.perl.org"
)
self
.
assertEqual
(
req
.
host
,
"www.perl.org"
)
r
=
o
.
open
(
req
)
o
.
open
(
req
)
self
.
assertEqual
(
req
.
host
,
"proxy.example.com"
)
self
.
assertEqual
(
req
.
host
,
"proxy.example.com"
)
req
=
Request
(
"http://www.python.org"
)
req
=
Request
(
"http://www.python.org"
)
self
.
assertEqual
(
req
.
host
,
"www.python.org"
)
self
.
assertEqual
(
req
.
host
,
"www.python.org"
)
r
=
o
.
open
(
req
)
o
.
open
(
req
)
self
.
assertEqual
(
req
.
host
,
"www.python.org"
)
self
.
assertEqual
(
req
.
host
,
"www.python.org"
)
del
os
.
environ
[
'no_proxy'
]
del
os
.
environ
[
'no_proxy'
]
...
@@ -1172,7 +1149,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -1172,7 +1149,7 @@ class HandlerTests(unittest.TestCase):
o
.
add_handler
(
ph
)
o
.
add_handler
(
ph
)
req
=
Request
(
"http://www.python.org"
)
req
=
Request
(
"http://www.python.org"
)
self
.
assertEqual
(
req
.
host
,
"www.python.org"
)
self
.
assertEqual
(
req
.
host
,
"www.python.org"
)
r
=
o
.
open
(
req
)
o
.
open
(
req
)
self
.
assertEqual
(
req
.
host
,
"www.python.org"
)
self
.
assertEqual
(
req
.
host
,
"www.python.org"
)
del
os
.
environ
[
'no_proxy'
]
del
os
.
environ
[
'no_proxy'
]
...
@@ -1188,7 +1165,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -1188,7 +1165,7 @@ class HandlerTests(unittest.TestCase):
req
=
Request
(
"https://www.example.com/"
)
req
=
Request
(
"https://www.example.com/"
)
self
.
assertEqual
(
req
.
host
,
"www.example.com"
)
self
.
assertEqual
(
req
.
host
,
"www.example.com"
)
r
=
o
.
open
(
req
)
o
.
open
(
req
)
self
.
assertEqual
(
req
.
host
,
"proxy.example.com:3128"
)
self
.
assertEqual
(
req
.
host
,
"proxy.example.com:3128"
)
self
.
assertEqual
([(
handlers
[
0
],
"https_open"
)],
self
.
assertEqual
([(
handlers
[
0
],
"https_open"
)],
[
tup
[
0
:
2
]
for
tup
in
o
.
calls
])
[
tup
[
0
:
2
]
for
tup
in
o
.
calls
])
...
@@ -1204,7 +1181,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -1204,7 +1181,7 @@ class HandlerTests(unittest.TestCase):
req
.
add_header
(
"User-Agent"
,
"Grail"
)
req
.
add_header
(
"User-Agent"
,
"Grail"
)
self
.
assertEqual
(
req
.
host
,
"www.example.com"
)
self
.
assertEqual
(
req
.
host
,
"www.example.com"
)
self
.
assertIsNone
(
req
.
_tunnel_host
)
self
.
assertIsNone
(
req
.
_tunnel_host
)
r
=
o
.
open
(
req
)
o
.
open
(
req
)
# Verify Proxy-Authorization gets tunneled to request.
# Verify Proxy-Authorization gets tunneled to request.
# httpsconn req_headers do not have the Proxy-Authorization header but
# httpsconn req_headers do not have the Proxy-Authorization header but
# the req will have.
# the req will have.
...
@@ -1370,7 +1347,7 @@ class HandlerTests(unittest.TestCase):
...
@@ -1370,7 +1347,7 @@ class HandlerTests(unittest.TestCase):
self
.
assertEqual
(
user
,
password_manager
.
user
)
self
.
assertEqual
(
user
,
password_manager
.
user
)
self
.
assertEqual
(
password
,
password_manager
.
password
)
self
.
assertEqual
(
password
,
password_manager
.
password
)
r
=
opener
.
open
(
request_url
)
opener
.
open
(
request_url
)
# should have asked the password manager for the username/password
# should have asked the password manager for the username/password
self
.
assertEqual
(
password_manager
.
target_realm
,
realm
)
self
.
assertEqual
(
password_manager
.
target_realm
,
realm
)
...
@@ -1390,13 +1367,17 @@ class HandlerTests(unittest.TestCase):
...
@@ -1390,13 +1367,17 @@ class HandlerTests(unittest.TestCase):
# handle the HTTP auth error
# handle the HTTP auth error
password_manager
.
user
=
password_manager
.
password
=
None
password_manager
.
user
=
password_manager
.
password
=
None
http_handler
.
reset
()
http_handler
.
reset
()
r
=
opener
.
open
(
request_url
)
opener
.
open
(
request_url
)
self
.
assertEqual
(
len
(
http_handler
.
requests
),
1
)
self
.
assertEqual
(
len
(
http_handler
.
requests
),
1
)
self
.
assertFalse
(
http_handler
.
requests
[
0
]
.
has_header
(
auth_header
))
self
.
assertFalse
(
http_handler
.
requests
[
0
]
.
has_header
(
auth_header
))
class
MiscTests
(
unittest
.
TestCase
):
class
MiscTests
(
unittest
.
TestCase
):
def
opener_has_handler
(
self
,
opener
,
handler_class
):
self
.
assertTrue
(
any
(
h
.
__class__
==
handler_class
for
h
in
opener
.
handlers
))
def
test_build_opener
(
self
):
def
test_build_opener
(
self
):
class
MyHTTPHandler
(
urllib
.
request
.
HTTPHandler
):
pass
class
MyHTTPHandler
(
urllib
.
request
.
HTTPHandler
):
pass
class
FooHandler
(
urllib
.
request
.
BaseHandler
):
class
FooHandler
(
urllib
.
request
.
BaseHandler
):
...
@@ -1434,9 +1415,22 @@ class MiscTests(unittest.TestCase):
...
@@ -1434,9 +1415,22 @@ class MiscTests(unittest.TestCase):
self
.
opener_has_handler
(
o
,
MyHTTPHandler
)
self
.
opener_has_handler
(
o
,
MyHTTPHandler
)
self
.
opener_has_handler
(
o
,
MyOtherHTTPHandler
)
self
.
opener_has_handler
(
o
,
MyOtherHTTPHandler
)
def
opener_has_handler
(
self
,
opener
,
handler_class
):
def
test_HTTPError_interface
(
self
):
self
.
assertTrue
(
any
(
h
.
__class__
==
handler_class
"""
for
h
in
opener
.
handlers
))
Issue 13211 reveals that HTTPError didn't implement the URLError
interface even though HTTPError is a subclass of URLError.
"""
msg
=
'something bad happened'
url
=
code
=
fp
=
None
hdrs
=
'Content-Length: 42'
err
=
urllib
.
error
.
HTTPError
(
url
,
code
,
msg
,
hdrs
,
fp
)
self
.
assertTrue
(
hasattr
(
err
,
'reason'
))
self
.
assertEqual
(
err
.
reason
,
'something bad happened'
)
self
.
assertTrue
(
hasattr
(
err
,
'hdrs'
))
self
.
assertEqual
(
err
.
hdrs
,
'Content-Length: 42'
)
expected_errmsg
=
'HTTP Error
%
s:
%
s'
%
(
err
.
code
,
err
.
msg
)
self
.
assertEqual
(
str
(
err
),
expected_errmsg
)
class
RequestTests
(
unittest
.
TestCase
):
class
RequestTests
(
unittest
.
TestCase
):
...
@@ -1498,25 +1492,6 @@ class RequestTests(unittest.TestCase):
...
@@ -1498,25 +1492,6 @@ class RequestTests(unittest.TestCase):
req
=
Request
(
url
)
req
=
Request
(
url
)
self
.
assertEqual
(
req
.
get_full_url
(),
url
)
self
.
assertEqual
(
req
.
get_full_url
(),
url
)
def
test_HTTPError_interface
(
self
):
"""
Issue 13211 reveals that HTTPError didn't implement the URLError
interface even though HTTPError is a subclass of URLError.
>>> msg = 'something bad happened'
>>> url = code = fp = None
>>> hdrs = 'Content-Length: 42'
>>> err = urllib.error.HTTPError(url, code, msg, hdrs, fp)
>>> assert hasattr(err, 'reason')
>>> err.reason
'something bad happened'
>>> assert hasattr(err, 'hdrs')
>>> err.hdrs
'Content-Length: 42'
>>> expected_errmsg = 'HTTP Error
%
s:
%
s'
%
(err.code, err.msg)
>>> assert str(err) == expected_errmsg
"""
def
test_HTTPError_interface_call
(
self
):
def
test_HTTPError_interface_call
(
self
):
"""
"""
Issue 15701 - HTTPError interface has info method available from URLError
Issue 15701 - HTTPError interface has info method available from URLError
...
@@ -1541,7 +1516,8 @@ def test_main(verbose=None):
...
@@ -1541,7 +1516,8 @@ def test_main(verbose=None):
OpenerDirectorTests
,
OpenerDirectorTests
,
HandlerTests
,
HandlerTests
,
MiscTests
,
MiscTests
,
RequestTests
)
RequestTests
,
RequestHdrsTests
)
support
.
run_unittest
(
*
tests
)
support
.
run_unittest
(
*
tests
)
if
__name__
==
"__main__"
:
if
__name__
==
"__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