Skip to content

Commit

Permalink
Add openAbsoluteURI func to cheroot.test.webtest
Browse files Browse the repository at this point in the history
This exposes nicer public API for test writers and
fixes cherrypy/cherrypy#1621.

Usage:
>>> openAbsoluteURI('http://localhost:35100/api/account/users/nothere/')
  • Loading branch information
webknjaz committed Nov 28, 2017
1 parent 4acd776 commit e012eb6
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion cheroot/test/webtest.py
Expand Up @@ -25,7 +25,7 @@
import unittest
from imp import reload

from six.moves import range, http_client, map
from six.moves import range, http_client, map, urllib_parse
import six


Expand Down Expand Up @@ -586,6 +586,36 @@ def openURL(url, headers=None, method='GET', body=None,
raise


def openAbsoluteURI(url, **kwargs):
"""Open the given HTTP URL provided in the form of Absolute-URI.
Ref: https://tools.ietf.org/html/rfc3986#section-4.3
"""
INVALID_ARGS = ('host', 'port')
for arg in INVALID_ARGS:
if arg in kwargs:
raise TypeError(
"openAbsoluteURI() got an unexpected keyword argument '{arg}'".
format(**locals())
)

parsed_url = urllib_parse.urlparse(url)
scheme, netloc, path, params, query, fragment = parsed_url
host = parsed_url.hostname
port = parsed_url.port or 80

stripped_url = urllib_parse.urlunparse(
(None, None, path, params, query, None)
)

if not (host and scheme):
raise TypeError(
'url must be in form of Absolute-URI (RFC3986, section 4.3)'
)

return openURL(url=stripped_url, **kwargs)


# Add any exceptions which your web framework handles
# normally (that you don't want server_error to trap).
ignored_exceptions = []
Expand Down

0 comments on commit e012eb6

Please sign in to comment.