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
10973a7a
Commit
10973a7a
authored
Mar 02, 2015
by
Serhiy Storchaka
Browse files
Options
Browse Files
Download
Plain Diff
Added more tests for urllib.parse utility functions.
These functions are not documented but used in third-party code.
parents
ec3e015f
531a4ea8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
129 additions
and
69 deletions
+129
-69
Lib/test/test_urllib.py
Lib/test/test_urllib.py
+0
-15
Lib/test/test_urlparse.py
Lib/test/test_urlparse.py
+129
-54
No files found.
Lib/test/test_urllib.py
View file @
10973a7a
...
...
@@ -1298,21 +1298,6 @@ class Pathname_Tests(unittest.TestCase):
class
Utility_Tests
(
unittest
.
TestCase
):
"""Testcase to test the various utility functions in the urllib."""
def
test_splitpasswd
(
self
):
"""Some of password examples are not sensible, but it is added to
confirming to RFC2617 and addressing issue4675.
"""
self
.
assertEqual
((
'user'
,
'ab'
),
urllib
.
parse
.
splitpasswd
(
'user:ab'
))
self
.
assertEqual
((
'user'
,
'a
\
n
b'
),
urllib
.
parse
.
splitpasswd
(
'user:a
\
n
b'
))
self
.
assertEqual
((
'user'
,
'a
\
t
b'
),
urllib
.
parse
.
splitpasswd
(
'user:a
\
t
b'
))
self
.
assertEqual
((
'user'
,
'a
\
r
b'
),
urllib
.
parse
.
splitpasswd
(
'user:a
\
r
b'
))
self
.
assertEqual
((
'user'
,
'a
\
f
b'
),
urllib
.
parse
.
splitpasswd
(
'user:a
\
f
b'
))
self
.
assertEqual
((
'user'
,
'a
\
v
b'
),
urllib
.
parse
.
splitpasswd
(
'user:a
\
v
b'
))
self
.
assertEqual
((
'user'
,
'a:b'
),
urllib
.
parse
.
splitpasswd
(
'user:a:b'
))
self
.
assertEqual
((
'user'
,
'a b'
),
urllib
.
parse
.
splitpasswd
(
'user:a b'
))
self
.
assertEqual
((
'user 2'
,
'ab'
),
urllib
.
parse
.
splitpasswd
(
'user 2:ab'
))
self
.
assertEqual
((
'user+1'
,
'a+b'
),
urllib
.
parse
.
splitpasswd
(
'user+1:a+b'
))
def
test_thishost
(
self
):
"""Test the urllib.request.thishost utility function returns a tuple"""
self
.
assertIsInstance
(
urllib
.
request
.
thishost
(),
tuple
)
...
...
Lib/test/test_urlparse.py
View file @
10973a7a
from
test
import
support
import
unittest
import
urllib.parse
...
...
@@ -769,52 +768,6 @@ class UrlParseTestCase(unittest.TestCase):
errors
=
"ignore"
)
self
.
assertEqual
(
result
,
[(
'key'
,
'
\
u0141
-'
)])
def
test_splitport
(
self
):
splitport
=
urllib
.
parse
.
splitport
self
.
assertEqual
(
splitport
(
'parrot:88'
),
(
'parrot'
,
'88'
))
self
.
assertEqual
(
splitport
(
'parrot'
),
(
'parrot'
,
None
))
self
.
assertEqual
(
splitport
(
'parrot:'
),
(
'parrot'
,
None
))
self
.
assertEqual
(
splitport
(
'127.0.0.1'
),
(
'127.0.0.1'
,
None
))
self
.
assertEqual
(
splitport
(
'parrot:cheese'
),
(
'parrot:cheese'
,
None
))
def
test_splitnport
(
self
):
splitnport
=
urllib
.
parse
.
splitnport
self
.
assertEqual
(
splitnport
(
'parrot:88'
),
(
'parrot'
,
88
))
self
.
assertEqual
(
splitnport
(
'parrot'
),
(
'parrot'
,
-
1
))
self
.
assertEqual
(
splitnport
(
'parrot'
,
55
),
(
'parrot'
,
55
))
self
.
assertEqual
(
splitnport
(
'parrot:'
),
(
'parrot'
,
-
1
))
self
.
assertEqual
(
splitnport
(
'parrot:'
,
55
),
(
'parrot'
,
55
))
self
.
assertEqual
(
splitnport
(
'127.0.0.1'
),
(
'127.0.0.1'
,
-
1
))
self
.
assertEqual
(
splitnport
(
'127.0.0.1'
,
55
),
(
'127.0.0.1'
,
55
))
self
.
assertEqual
(
splitnport
(
'parrot:cheese'
),
(
'parrot'
,
None
))
self
.
assertEqual
(
splitnport
(
'parrot:cheese'
,
55
),
(
'parrot'
,
None
))
def
test_splitquery
(
self
):
# Normal cases are exercised by other tests; ensure that we also
# catch cases with no port specified (testcase ensuring coverage)
result
=
urllib
.
parse
.
splitquery
(
'http://python.org/fake?foo=bar'
)
self
.
assertEqual
(
result
,
(
'http://python.org/fake'
,
'foo=bar'
))
result
=
urllib
.
parse
.
splitquery
(
'http://python.org/fake?foo=bar?'
)
self
.
assertEqual
(
result
,
(
'http://python.org/fake?foo=bar'
,
''
))
result
=
urllib
.
parse
.
splitquery
(
'http://python.org/fake'
)
self
.
assertEqual
(
result
,
(
'http://python.org/fake'
,
None
))
def
test_splitvalue
(
self
):
# Normal cases are exercised by other tests; test pathological cases
# with no key/value pairs. (testcase ensuring coverage)
result
=
urllib
.
parse
.
splitvalue
(
'foo=bar'
)
self
.
assertEqual
(
result
,
(
'foo'
,
'bar'
))
result
=
urllib
.
parse
.
splitvalue
(
'foo='
)
self
.
assertEqual
(
result
,
(
'foo'
,
''
))
result
=
urllib
.
parse
.
splitvalue
(
'foobar'
)
self
.
assertEqual
(
result
,
(
'foobar'
,
None
))
def
test_to_bytes
(
self
):
result
=
urllib
.
parse
.
to_bytes
(
'http://www.python.org'
)
self
.
assertEqual
(
result
,
'http://www.python.org'
)
self
.
assertRaises
(
UnicodeError
,
urllib
.
parse
.
to_bytes
,
'http://www.python.org/medi
\
u00e6
val'
)
def
test_urlencode_sequences
(
self
):
# Other tests incidentally urlencode things; test non-covered cases:
# Sequence and object values.
...
...
@@ -883,17 +836,139 @@ class UrlParseTestCase(unittest.TestCase):
self
.
assertEqual
(
p1
.
path
,
'863-1234'
)
self
.
assertEqual
(
p1
.
params
,
'phone-context=+1-914-555'
)
def
test_unwrap
(
self
):
url
=
urllib
.
parse
.
unwrap
(
'<URL:type://host/path>'
)
self
.
assertEqual
(
url
,
'type://host/path'
)
def
test_Quoter_repr
(
self
):
quoter
=
urllib
.
parse
.
Quoter
(
urllib
.
parse
.
_ALWAYS_SAFE
)
self
.
assertIn
(
'Quoter'
,
repr
(
quoter
))
def
test_main
():
support
.
run_unittest
(
UrlParseTestCase
)
class
Utility_Tests
(
unittest
.
TestCase
):
"""Testcase to test the various utility functions in the urllib."""
# In Python 2 this test class was in test_urllib.
def
test_splittype
(
self
):
splittype
=
urllib
.
parse
.
splittype
self
.
assertEqual
(
splittype
(
'type:opaquestring'
),
(
'type'
,
'opaquestring'
))
self
.
assertEqual
(
splittype
(
'opaquestring'
),
(
None
,
'opaquestring'
))
self
.
assertEqual
(
splittype
(
':opaquestring'
),
(
None
,
':opaquestring'
))
self
.
assertEqual
(
splittype
(
'type:'
),
(
'type'
,
''
))
self
.
assertEqual
(
splittype
(
'type:opaque:string'
),
(
'type'
,
'opaque:string'
))
def
test_splithost
(
self
):
splithost
=
urllib
.
parse
.
splithost
self
.
assertEqual
(
splithost
(
'//www.example.org:80/foo/bar/baz.html'
),
(
'www.example.org:80'
,
'/foo/bar/baz.html'
))
self
.
assertEqual
(
splithost
(
'//www.example.org:80'
),
(
'www.example.org:80'
,
''
))
self
.
assertEqual
(
splithost
(
'/foo/bar/baz.html'
),
(
None
,
'/foo/bar/baz.html'
))
def
test_splituser
(
self
):
splituser
=
urllib
.
parse
.
splituser
self
.
assertEqual
(
splituser
(
'User:Pass@www.python.org:080'
),
(
'User:Pass'
,
'www.python.org:080'
))
self
.
assertEqual
(
splituser
(
'@www.python.org:080'
),
(
''
,
'www.python.org:080'
))
self
.
assertEqual
(
splituser
(
'www.python.org:080'
),
(
None
,
'www.python.org:080'
))
self
.
assertEqual
(
splituser
(
'User:Pass@'
),
(
'User:Pass'
,
''
))
self
.
assertEqual
(
splituser
(
'User@example.com:Pass@www.python.org:080'
),
(
'User@example.com:Pass'
,
'www.python.org:080'
))
def
test_splitpasswd
(
self
):
# Some of the password examples are not sensible, but it is added to
# confirming to RFC2617 and addressing issue4675.
splitpasswd
=
urllib
.
parse
.
splitpasswd
self
.
assertEqual
(
splitpasswd
(
'user:ab'
),
(
'user'
,
'ab'
))
self
.
assertEqual
(
splitpasswd
(
'user:a
\
n
b'
),
(
'user'
,
'a
\
n
b'
))
self
.
assertEqual
(
splitpasswd
(
'user:a
\
t
b'
),
(
'user'
,
'a
\
t
b'
))
self
.
assertEqual
(
splitpasswd
(
'user:a
\
r
b'
),
(
'user'
,
'a
\
r
b'
))
self
.
assertEqual
(
splitpasswd
(
'user:a
\
f
b'
),
(
'user'
,
'a
\
f
b'
))
self
.
assertEqual
(
splitpasswd
(
'user:a
\
v
b'
),
(
'user'
,
'a
\
v
b'
))
self
.
assertEqual
(
splitpasswd
(
'user:a:b'
),
(
'user'
,
'a:b'
))
self
.
assertEqual
(
splitpasswd
(
'user:a b'
),
(
'user'
,
'a b'
))
self
.
assertEqual
(
splitpasswd
(
'user 2:ab'
),
(
'user 2'
,
'ab'
))
self
.
assertEqual
(
splitpasswd
(
'user+1:a+b'
),
(
'user+1'
,
'a+b'
))
self
.
assertEqual
(
splitpasswd
(
'user:'
),
(
'user'
,
''
))
self
.
assertEqual
(
splitpasswd
(
'user'
),
(
'user'
,
None
))
self
.
assertEqual
(
splitpasswd
(
':ab'
),
(
''
,
'ab'
))
def
test_splitport
(
self
):
splitport
=
urllib
.
parse
.
splitport
self
.
assertEqual
(
splitport
(
'parrot:88'
),
(
'parrot'
,
'88'
))
self
.
assertEqual
(
splitport
(
'parrot'
),
(
'parrot'
,
None
))
self
.
assertEqual
(
splitport
(
'parrot:'
),
(
'parrot'
,
None
))
self
.
assertEqual
(
splitport
(
'127.0.0.1'
),
(
'127.0.0.1'
,
None
))
self
.
assertEqual
(
splitport
(
'parrot:cheese'
),
(
'parrot:cheese'
,
None
))
self
.
assertEqual
(
splitport
(
'[::1]:88'
),
(
'[::1]'
,
'88'
))
self
.
assertEqual
(
splitport
(
'[::1]'
),
(
'[::1]'
,
None
))
self
.
assertEqual
(
splitport
(
':88'
),
(
''
,
'88'
))
def
test_splitnport
(
self
):
splitnport
=
urllib
.
parse
.
splitnport
self
.
assertEqual
(
splitnport
(
'parrot:88'
),
(
'parrot'
,
88
))
self
.
assertEqual
(
splitnport
(
'parrot'
),
(
'parrot'
,
-
1
))
self
.
assertEqual
(
splitnport
(
'parrot'
,
55
),
(
'parrot'
,
55
))
self
.
assertEqual
(
splitnport
(
'parrot:'
),
(
'parrot'
,
-
1
))
self
.
assertEqual
(
splitnport
(
'parrot:'
,
55
),
(
'parrot'
,
55
))
self
.
assertEqual
(
splitnport
(
'127.0.0.1'
),
(
'127.0.0.1'
,
-
1
))
self
.
assertEqual
(
splitnport
(
'127.0.0.1'
,
55
),
(
'127.0.0.1'
,
55
))
self
.
assertEqual
(
splitnport
(
'parrot:cheese'
),
(
'parrot'
,
None
))
self
.
assertEqual
(
splitnport
(
'parrot:cheese'
,
55
),
(
'parrot'
,
None
))
def
test_splitquery
(
self
):
# Normal cases are exercised by other tests; ensure that we also
# catch cases with no port specified (testcase ensuring coverage)
splitquery
=
urllib
.
parse
.
splitquery
self
.
assertEqual
(
splitquery
(
'http://python.org/fake?foo=bar'
),
(
'http://python.org/fake'
,
'foo=bar'
))
self
.
assertEqual
(
splitquery
(
'http://python.org/fake?foo=bar?'
),
(
'http://python.org/fake?foo=bar'
,
''
))
self
.
assertEqual
(
splitquery
(
'http://python.org/fake'
),
(
'http://python.org/fake'
,
None
))
self
.
assertEqual
(
splitquery
(
'?foo=bar'
),
(
''
,
'foo=bar'
))
def
test_splittag
(
self
):
splittag
=
urllib
.
parse
.
splittag
self
.
assertEqual
(
splittag
(
'http://example.com?foo=bar#baz'
),
(
'http://example.com?foo=bar'
,
'baz'
))
self
.
assertEqual
(
splittag
(
'http://example.com?foo=bar#'
),
(
'http://example.com?foo=bar'
,
''
))
self
.
assertEqual
(
splittag
(
'#baz'
),
(
''
,
'baz'
))
self
.
assertEqual
(
splittag
(
'http://example.com?foo=bar'
),
(
'http://example.com?foo=bar'
,
None
))
self
.
assertEqual
(
splittag
(
'http://example.com?foo=bar#baz#boo'
),
(
'http://example.com?foo=bar#baz'
,
'boo'
))
def
test_splitattr
(
self
):
splitattr
=
urllib
.
parse
.
splitattr
self
.
assertEqual
(
splitattr
(
'/path;attr1=value1;attr2=value2'
),
(
'/path'
,
[
'attr1=value1'
,
'attr2=value2'
]))
self
.
assertEqual
(
splitattr
(
'/path;'
),
(
'/path'
,
[
''
]))
self
.
assertEqual
(
splitattr
(
';attr1=value1;attr2=value2'
),
(
''
,
[
'attr1=value1'
,
'attr2=value2'
]))
self
.
assertEqual
(
splitattr
(
'/path'
),
(
'/path'
,
[]))
def
test_splitvalue
(
self
):
# Normal cases are exercised by other tests; test pathological cases
# with no key/value pairs. (testcase ensuring coverage)
splitvalue
=
urllib
.
parse
.
splitvalue
self
.
assertEqual
(
splitvalue
(
'foo=bar'
),
(
'foo'
,
'bar'
))
self
.
assertEqual
(
splitvalue
(
'foo='
),
(
'foo'
,
''
))
self
.
assertEqual
(
splitvalue
(
'=bar'
),
(
''
,
'bar'
))
self
.
assertEqual
(
splitvalue
(
'foobar'
),
(
'foobar'
,
None
))
self
.
assertEqual
(
splitvalue
(
'foo=bar=baz'
),
(
'foo'
,
'bar=baz'
))
def
test_to_bytes
(
self
):
result
=
urllib
.
parse
.
to_bytes
(
'http://www.python.org'
)
self
.
assertEqual
(
result
,
'http://www.python.org'
)
self
.
assertRaises
(
UnicodeError
,
urllib
.
parse
.
to_bytes
,
'http://www.python.org/medi
\
u00e6
val'
)
def
test_unwrap
(
self
):
url
=
urllib
.
parse
.
unwrap
(
'<URL:type://host/path>'
)
self
.
assertEqual
(
url
,
'type://host/path'
)
if
__name__
==
"__main__"
:
test_
main
()
unittest
.
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