Experiment with implementation of a $COOKIE

$COOKIE is replaced by the cookie key and value pairs parsed out of
the set-cookie headers from the previous request.

This was straightforward to do and matches the behavior in
http://sandbox.tiddlyspace.com/_/979d0866-8959-4fed-9b3b-403002c20aa2
but for some reason it doesn't feel right. Needs more thought.
This commit is contained in:
Chris Dent 2016-05-15 18:56:09 +01:00
parent e31f9b4f62
commit 00c119241b
3 changed files with 27 additions and 0 deletions
gabbi
case.py
tests
gabbits_intercept
simple_wsgi.py

@ -30,6 +30,7 @@ from unittest import case
import six
from six.moves.urllib import parse as urlparse
from six.moves import http_cookies
import wsgi_intercept
from gabbi import __version__
@ -44,6 +45,7 @@ REPLACERS = [
'NETLOC',
'ENVIRON',
'LOCATION',
'COOKIE',
'LAST_URL',
'HEADERS',
'RESPONSE',
@ -208,6 +210,17 @@ class HTTPTestCase(unittest.TestCase):
raise ValueError(
"JSONPath '%s' failed to match on data: '%s'" % (path, data))
def _cookie_replace(self, message):
"""Replace $COOKIE in a message.
With cookie data from set-cookie in the prior request.
"""
response_cookies = self.prior.response['set-cookie']
cookies = http_cookies.SimpleCookie()
cookies.load(response_cookies)
cookie_string = cookies.output(attrs=[], header='', sep=',').strip()
return message.replace('$COOKIE', cookie_string)
def _headers_replace(self, message):
"""Replace a header indicator in a message with that headers value from
the prior request.

@ -0,0 +1,12 @@
tests:
- name: get a cookie
GET: /cookie
response_headers:
set-cookie: session=1234; domain=.example.com
- name: use that cookie in a URL
desc: just to get it somewhere we can test it and confirm domain is dropped
GET: /cookie?$COOKIE
response_headers:
x-gabbi-url: $SCHEME://$NETLOC/cookie?session=1234

@ -105,6 +105,8 @@ class SimpleWsgi(object):
else:
CURRENT_POLL = 0
# fall through if we've ended the loop
elif path_info == '/cookie':
headers.append(('Set-Cookie', 'session=1234; domain=.example.com'))
start_response('200 OK', headers)