diff --git a/pyresttest/resttest.py b/pyresttest/resttest.py index 7bfb33c1..a56c7671 100644 --- a/pyresttest/resttest.py +++ b/pyresttest/resttest.py @@ -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 @@ -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': @@ -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 @@ -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("===================================") @@ -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') diff --git a/pyresttest/tests.py b/pyresttest/tests.py index 56365fce..03aded24 100644 --- a/pyresttest/tests.py +++ b/pyresttest/tests.py @@ -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) @@ -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: