Skip to content
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

Add "cookiejar" option to config. #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions pyresttest/resttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class TestConfig:
verbose = False
ssl_insecure = False
skip_term_colors = False # Turn off output term colors
cookiejar = None

# Binding and creation of generators
variable_binds = None
Expand Down Expand Up @@ -278,6 +279,8 @@ def parse_configuration(node, base_config=None):
test_config.timeout = int(value)
elif key == u'print_bodies':
test_config.print_bodies = safe_to_bool(value)
elif key == u'cookiejar':
test_config.cookiejar = os.path.basename(str(value))
elif key == u'retries':
test_config.retries = int(value)
elif key == u'variable_binds':
Expand Down Expand Up @@ -314,7 +317,7 @@ def run_test(mytest, test_config=TestConfig(), context=None, curl_handle=None, *
mytest.update_context_before(my_context)
templated_test = mytest.realize(my_context)
curl = templated_test.configure_curl(
timeout=test_config.timeout, context=my_context, curl_handle=curl_handle)
timeout=test_config.timeout, cookiejar=test_config.cookiejar, context=my_context, curl_handle=curl_handle)
result = TestResponse()
result.test = templated_test

Expand Down Expand Up @@ -708,6 +711,13 @@ def run_testsets(testsets):
benchmark, test_config=myconfig)
my_file.close()

if myconfig.cookiejar:
curl_handle.close()
try:
os.remove('/'.join([os.getcwd(), myconfig.cookiejar]))
except OSError:
pass

if myinteractive:
# a break for when interactive bits are complete, before summary data
print("===================================")
Expand All @@ -719,10 +729,10 @@ def run_testsets(testsets):
total_failures = total_failures + failures

passfail = {True: u'SUCCEEDED: ', False: u'FAILED: '}
output_string = "Test Group {0} {1}: {2}/{3} Tests Passed!".format(group, passfail[failures == 0], str(test_count - failures), str(test_count))
output_string = "Test Group {0} {1}: {2}/{3} Tests Passed!".format(group, passfail[failures == 0], str(test_count - failures), str(test_count))

if myconfig.skip_term_colors:
print(output_string)
print(output_string)
else:
if failures > 0:
print('\033[91m' + output_string + '\033[0m')
Expand Down
28 changes: 17 additions & 11 deletions pyresttest/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,24 +286,29 @@ def __init__(self):
def __str__(self):
return json.dumps(self, default=safe_to_json)

def configure_curl(self, timeout=DEFAULT_TIMEOUT, context=None, curl_handle=None):
def configure_curl(self, timeout=DEFAULT_TIMEOUT, cookiejar=None, context=None, curl_handle=None):
""" Create and mostly configure a curl object for test, reusing existing if possible """

if curl_handle:
curl = curl_handle

try: # Check the curl handle isn't closed, and reuse it if possible
curl.getinfo(curl.HTTP_CODE)
# Below clears the cookies & curl options for clean run
# But retains the DNS cache and connection pool
curl.getinfo(curl.HTTP_CODE)
curl.reset()
curl.setopt(curl.COOKIELIST, "ALL")
if not cookiejar:
# Below clears the cookies & curl options for clean run
# But retains the DNS cache and connection pool
curl.setopt(curl.COOKIELIST, "ALL")
except pycurl.error:
curl = pycurl.Curl()

else:
curl = pycurl.Curl()

if cookiejar:
curl.setopt(curl.COOKIEJAR, cookiejar)
curl.setopt(curl.COOKIEFILE, cookiejar)
curl.setopt(curl.COOKIELIST, "RELOAD")

# curl.setopt(pycurl.VERBOSE, 1) # Debugging convenience
curl.setopt(curl.URL, str(self.url))
curl.setopt(curl.TIMEOUT, timeout)
Expand All @@ -319,13 +324,14 @@ def configure_curl(self, timeout=DEFAULT_TIMEOUT, context=None, curl_handle=None
curl.setopt(curl.READFUNCTION, MyIO(bod).read)

if self.auth_username and self.auth_password:
curl.setopt(pycurl.USERPWD,
parsing.encode_unicode_bytes(self.auth_username) + b':' +
curl.setopt(pycurl.USERPWD,
parsing.encode_unicode_bytes(self.auth_username) + b':' +
parsing.encode_unicode_bytes(self.auth_password))
if self.auth_type:
curl.setopt(pycurl.HTTPAUTH, self.auth_type)

if self.method == u'POST':
if self.method == u'GET':
curl.setopt(HTTP_METHODS[u'GET'], 1)
elif self.method == u'POST':
curl.setopt(HTTP_METHODS[u'POST'], 1)
# Required for some servers
if bod is not None:
Expand Down