Skip to content

Commit

Permalink
Merge tag 'v5.11.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 5, 2017
2 parents 2fd065f + 31f97a3 commit 3a34d33
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ v6.0.0
- Also drop built-in SSL support for Python 2.7 earlier
than 2.7.9.

v5.11.0
======

- CherryPy #1621: To support webtest applications that feed
absolute URIs to getPage but expect the scheme/host/port to
be ignored (as cheroot 5.8 and earlier did), provide a
``strip_netloc`` helper and recipe for calling it in a subclass.

v5.10.0
======

Expand Down
60 changes: 34 additions & 26 deletions cheroot/test/webtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ def getPage(self, url, headers=None, method='GET', body=None,
protocol=None, raise_subcls=None):
"""Open the url with debugging support. Return status, headers, body.
url should be the identifier passed to the server, typically a
server-absolute path and query string (sent between method and
protocol), and should only be an absolute URI if proxy support is
enabled in the server.
If the application under test generates absolute URIs, be sure
to wrap them first with strip_netloc::
class MyAppWebCase(WebCase):
def getPage(url, *args, **kwargs):
super(MyAppWebCase, self).getPage(
cheroot.test.webtest.strip_netloc(url),
*args, **kwargs
)
`raise_subcls` must be a tuple with the exceptions classes
or a single exception class that are not going to be considered
a socket.error regardless that they were are subclass of a
Expand Down Expand Up @@ -588,34 +603,27 @@ 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
def strip_netloc(url):
"""
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)'
)
Strip the scheme and host from the URL, returning the
server-absolute portion.
return openURL(url=stripped_url, **kwargs)
Useful for wrapping an absolute-URI for which only the
path is expected (such as in calls to getPage).
>>> strip_netloc('https://google.com/foo/bar?bing#baz')
'/foo/bar?bing'
>>> strip_netloc('//google.com/foo/bar?bing#baz')
'/foo/bar?bing'
>>> strip_netloc('/foo/bar?bing#baz')
'/foo/bar?bing'
"""
parsed = urllib_parse.urlparse(url)
scheme, netloc, path, params, query, fragment = parsed
stripped = '', '', path, params, query, ''
return urllib_parse.urlunparse(stripped)


# Add any exceptions which your web framework handles
Expand Down

0 comments on commit 3a34d33

Please sign in to comment.