-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cookie extractor from Set-Cookie response. #184
base: master
Are you sure you want to change the base?
Changes from all commits
faecfdc
7d4c005
476b264
1c03753
35d861b
fc1232d
5814230
e4761cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import logging | ||
import json | ||
import Cookie | ||
import operator | ||
import traceback | ||
import string | ||
|
@@ -289,6 +290,24 @@ def parse(cls, config, extractor_base=None): | |
return cls.configure_base(config, base) | ||
|
||
|
||
class CookieExtractor(AbstractExtractor): | ||
""" Extractor that pulls out a named cookie """ | ||
extractor_type = 'cookie' | ||
is_header_extractor = True | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent! Glad you picked up and added this. |
||
|
||
def extract_internal(self, query = None, args = None, body = None, headers = None): | ||
extracted = [y[1] for y in filter(lambda x: x[0] == 'set-cookie', headers) if y[1].startswith(query + '=')] | ||
if len(extracted) == 0: | ||
raise ValueError("Cookie named {0} not known".format(query)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good. |
||
else: | ||
return Cookie.SimpleCookie(extracted[-1])[query].value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not as familiar with the syntax of the cookie library, but this seems solid. Would have to test it a bit though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it return the last index of cookies. The behaviors may be strange.
Is this better? fast fail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@alswl What do you mean by this? The -1 array index is fine, and we should be using the last cookie specified, I'm just wondering about the Cookie.SimpleCookie there and what its failure modes are. I think you also meant this, which doesn't end up any different than the original in truth (it is slightly cleaner): if len(extracted) == 0:
raise ValueError('xxx')
return Cookie.SimpleCookie(extracted[-1])[query].value |
||
|
||
@classmethod | ||
def parse(cls, config, extractor_base = None): | ||
base = CookieExtractor() | ||
return cls.configure_base(config, base) | ||
|
||
|
||
class RawBodyExtractor(AbstractExtractor): | ||
""" Extractor that returns the full request body """ | ||
extractor_type = 'raw_body' | ||
|
@@ -595,6 +614,7 @@ def register_comparator(comparator_name, comparator_function): | |
# --- REGISTRY OF EXTRACTORS AND VALIDATORS --- | ||
register_extractor('jsonpath_mini', MiniJsonExtractor.parse) | ||
register_extractor('header', HeaderExtractor.parse) | ||
register_extractor('cookie', CookieExtractor.parse) | ||
register_extractor('raw_body', RawBodyExtractor.parse) | ||
# ENHANCEME: add JsonPath-rw support for full JsonPath syntax | ||
# ENHANCEME: add elementree support for xpath extract on XML, very simple no? | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair, but we need to do the following for python 3 compatibility, since the library changed in Py3 😢 https://docs.python.org/2/library/cookie.html
if 'sys' is not in imports, add it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even easier, just do this:
I'm testing this patch with the above and it works perfectly well on both Py2 and Py3: