From 561c76ccc5fbb002d846a8ddd806f617c7a00e63 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Wed, 22 Feb 2017 10:23:26 -0500 Subject: [PATCH 1/3] Avoid duplicating prefix when re-using urls If a reponse includes an absolute link, re-using that link as the url in a subsequent test when a prefix is being used in the tests can result in a 404 because the prefix will be duplicated. This change ensures that the prefix will not be duplicated if the URL already begins with the prefix. Fixes #165 --- gabbi/tests/gabbits_intercept/prefix.yaml | 16 ++++++++++++++++ gabbi/utils.py | 3 ++- tox.ini | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 gabbi/tests/gabbits_intercept/prefix.yaml diff --git a/gabbi/tests/gabbits_intercept/prefix.yaml b/gabbi/tests/gabbits_intercept/prefix.yaml new file mode 100644 index 0000000..d005c21 --- /dev/null +++ b/gabbi/tests/gabbits_intercept/prefix.yaml @@ -0,0 +1,16 @@ + +tests: + +- name: provide a link in response + POST: / + request_headers: + content-type: application/json + data: + link: $ENVIRON['GABBI_PREFIX']/barnabas + +- name: get that link + GET: $RESPONSE['$.link'] + response_headers: + x-gabbi-url: "///[a-f0-9:-]+$ENVIRON['GABBI_PREFIX']/barnabas/" + + diff --git a/gabbi/utils.py b/gabbi/utils.py index ab1f4cf..8542f5d 100644 --- a/gabbi/utils.py +++ b/gabbi/utils.py @@ -53,7 +53,8 @@ def create_url(base_url, host, port=None, prefix='', ssl=False): path = parsed_url.path # Guard against a prefix of None - if prefix: + # Without the startswith check, the tests in prefix.yaml fail. + if prefix and not path.startswith(prefix): path = '%s%s' % (prefix, path) return urlparse.urlunsplit((scheme, netloc, path, query_string, '')) diff --git a/tox.ini b/tox.ini index ec25b0a..0b3660e 100644 --- a/tox.ini +++ b/tox.ini @@ -8,6 +8,7 @@ deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt install_command = pip install -U {opts} {packages} commands = python setup.py testr --testr-args="{posargs}" +setenv = GABBI_PREFIX= passenv = GABBI_* HOME [testenv:venv] From cc79b5fd98aefea99ce9af8f7862091f040655a4 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Fri, 24 Feb 2017 11:59:35 -0500 Subject: [PATCH 2/3] Add a test for a relative link and add a comment The comment explains how this is a pragmatic fix that could be done in a more robust way by changing data structures to distinguish between server and gabbi test generated urls. --- gabbi/tests/gabbits_intercept/prefix.yaml | 10 +++++++--- gabbi/utils.py | 12 ++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/gabbi/tests/gabbits_intercept/prefix.yaml b/gabbi/tests/gabbits_intercept/prefix.yaml index d005c21..3d6d0dc 100644 --- a/gabbi/tests/gabbits_intercept/prefix.yaml +++ b/gabbi/tests/gabbits_intercept/prefix.yaml @@ -1,16 +1,20 @@ tests: -- name: provide a link in response +- name: provide a link POST: / request_headers: content-type: application/json data: link: $ENVIRON['GABBI_PREFIX']/barnabas + relative: link - name: get that link GET: $RESPONSE['$.link'] - response_headers: + response_headers: x-gabbi-url: "///[a-f0-9:-]+$ENVIRON['GABBI_PREFIX']/barnabas/" - +- name: get relative link + GET: $HISTORY['provide a link'].$RESPONSE['$.relative'] + response_headers: + x-gabbi-url: "///[a-f0-9:-]+$ENVIRON['GABBI_PREFIX']/link/" diff --git a/gabbi/utils.py b/gabbi/utils.py index 8542f5d..39e0331 100644 --- a/gabbi/utils.py +++ b/gabbi/utils.py @@ -52,8 +52,16 @@ def create_url(base_url, host, port=None, prefix='', ssl=False): query_string = parsed_url.query path = parsed_url.path - # Guard against a prefix of None - # Without the startswith check, the tests in prefix.yaml fail. + # Guard against a prefix of None or the url already having the + # prefix. Without the startswith check, the tests in prefix.yaml + # fail. This is a pragmatic fix which does this for any URL in a + # test request that does not have a scheme and does not + # distinguish between URLs in a gabbi test file and those + # generated by the server. Idealy we would not mutate nor need + # to check URLs returned from the server. Doing that, however, + # would require more complex data handling than we have now and + # this covers most common cases and will be okay until someone + # reports a bug. if prefix and not path.startswith(prefix): path = '%s%s' % (prefix, path) From 22bef30f815408f99893b996042f6cc9d9f2df57 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Fri, 24 Feb 2017 12:43:26 -0500 Subject: [PATCH 3/3] Fix prefix handling for relative urls Basically we just remove the trailing prefix / and leading path / and then join with /. --- gabbi/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gabbi/utils.py b/gabbi/utils.py index 39e0331..ef1462c 100644 --- a/gabbi/utils.py +++ b/gabbi/utils.py @@ -63,7 +63,9 @@ def create_url(base_url, host, port=None, prefix='', ssl=False): # this covers most common cases and will be okay until someone # reports a bug. if prefix and not path.startswith(prefix): - path = '%s%s' % (prefix, path) + prefix = prefix.rstrip('/') + path = path.lstrip('/') + path = '%s/%s' % (prefix, path) return urlparse.urlunsplit((scheme, netloc, path, query_string, ''))