From ca4d8b7d93aa5222ff8b410c752749cbd9de3495 Mon Sep 17 00:00:00 2001 From: Felipe Zipitria Date: Tue, 24 Nov 2020 13:28:46 -0300 Subject: [PATCH] feat(py3): apply 2to3 to move away from py2 syntax where needed Signed-off-by: Felipe Zipitria --- ftw/http.py | 19 ++++++++++--------- ftw/pytest_plugin.py | 6 +++--- ftw/ruleset.py | 16 +++++----------- ftw/testrunner.py | 2 +- ftw/util.py | 2 +- ftw/util/ironbee.py | 2 +- test/integration/test_http.py | 4 ++-- test/unit/test_ruleset.py | 2 +- 8 files changed, 24 insertions(+), 29 deletions(-) diff --git a/ftw/http.py b/ftw/http.py index aaa9119..bea9acd 100644 --- a/ftw/http.py +++ b/ftw/http.py @@ -1,4 +1,4 @@ -from __future__ import print_function + import brotli import io import socket @@ -19,6 +19,7 @@ from six.moves import http_cookies from . import errors +import importlib # Fallback to PROTOCOL_SSLv23 if PROTOCOL_TLS is not available. @@ -26,7 +27,7 @@ if PY2: - reload(sys) # pragma: no flakes + importlib.reload(sys) # pragma: no flakes sys.setdefaultencoding('utf8') escape_codec = 'string_escape' else: @@ -106,7 +107,7 @@ def check_for_cookie(self, cookie): IP(self.dest_addr) except ValueError: origin_is_ip = False - for cookie_morsals in cookie.values(): + for cookie_morsals in list(cookie.values()): # If the coverdomain is blank or the domain is an IP # set the domain to be the origin if cookie_morsals['domain'] == '' or origin_is_ip: @@ -206,7 +207,7 @@ def process_response(self): }) header = ensure_str(header[0]), ensure_str(header[1]) response_headers[header[0].lower()] = header[1].lstrip() - if 'set-cookie' in response_headers.keys(): + if 'set-cookie' in list(response_headers.keys()): try: cookie = http_cookies.SimpleCookie() cookie.load(response_headers['set-cookie']) @@ -232,7 +233,7 @@ def process_response(self): response_data = self.CRLF.join(split_response[data_line:]) # if the output headers say there is encoding - if 'content-encoding' in response_headers.keys(): + if 'content-encoding' in list(response_headers.keys()): response_data = self.parse_content_encoding( response_headers, response_data) if len(response_line.split(' ', 2)) != 3: @@ -333,7 +334,7 @@ def find_cookie(self): return_cookies = [] origin_domain = self.request_object.dest_addr for cookie in self.cookiejar: - for cookie_morsals in cookie[0].values(): + for cookie_morsals in list(cookie[0].values()): cover_domain = cookie_morsals['domain'] if cover_domain == '': if origin_domain == cookie[1]: @@ -361,7 +362,7 @@ def build_request(self): # If the user has requested a tracked cookie and we have one set it if available_cookies: cookie_value = '' - if 'cookie' in self.request_object.headers.keys(): + if 'cookie' in list(self.request_object.headers.keys()): # Create a SimpleCookie out of our provided cookie try: provided_cookie = http_cookies.SimpleCookie() @@ -382,7 +383,7 @@ def build_request(self): provided_cookie[cookie_key].value for cookie in available_cookies: for cookie_key, cookie_morsal in iteritems(cookie): - if cookie_key in result_cookie.keys(): + if cookie_key in list(result_cookie.keys()): # we don't overwrite a user specified # cookie with a saved one pass @@ -419,7 +420,7 @@ def build_request(self): encoding = "utf-8" # Check to see if we have a content type and magic is # off (otherwise UTF-8) - if 'Content-Type' in self.request_object.headers.keys() and \ + if 'Content-Type' in list(self.request_object.headers.keys()) and \ self.request_object.stop_magic is False: pattern = re.compile(r'\;\s{0,1}?charset\=(.*?)(?:$|\;|\s)') m = re.search(pattern, diff --git a/ftw/pytest_plugin.py b/ftw/pytest_plugin.py index 79cf0c8..710e81f 100644 --- a/ftw/pytest_plugin.py +++ b/ftw/pytest_plugin.py @@ -27,10 +27,10 @@ def test_id(val): Dynamically names tests, useful for when we are running dozens to hundreds of tests """ - if isinstance(val, (dict, Test,)): + if isinstance(val, (dict, Test)): # We must be carful here because errors are swallowed and # defaults returned - if 'name' in val.ruleset_meta.keys(): + if 'name' in list(val.ruleset_meta.keys()): return '%s -- %s' % (val.ruleset_meta['name'], val.test_title) else: return '%s -- %s' % ("Unnamed_Test", val.test_title) @@ -104,7 +104,7 @@ def pytest_addoption(parser): help='pass in a tablename to parse journal results') parser.addoption('--port', action='store', default=None, help='destination port to direct tests towards', - choices=range(1, 65536), + choices=list(range(1, 65536)), type=int) parser.addoption('--protocol', action='store', default=None, help='destination protocol to direct tests towards', diff --git a/ftw/ruleset.py b/ftw/ruleset.py index 83d6721..640f8f9 100644 --- a/ftw/ruleset.py +++ b/ftw/ruleset.py @@ -115,10 +115,10 @@ def __init__(self, raw_request=None, # Check if there is any data and do defaults if self.data != '': # Default values for content length and header - if 'Content-Type' not in headers.keys() and stop_magic is False: + if 'Content-Type' not in list(headers.keys()) and stop_magic is False: headers['Content-Type'] = 'application/x-www-form-urlencoded' # check if encoded and encode if it should be - if 'Content-Type' in headers.keys(): + if 'Content-Type' in list(headers.keys()): if headers['Content-Type'] == \ 'application/x-www-form-urlencoded' and stop_magic is False: if ensure_str(unquote(self.data)) == self.data: @@ -126,7 +126,7 @@ def __init__(self, raw_request=None, if len(query_string) != 0: encoded_args = urlencode(query_string) self.data = encoded_args - if 'Content-Length' not in headers.keys() and stop_magic is False: + if 'Content-Length' not in list(headers.keys()) and stop_magic is False: # The two is for the trailing CRLF and the one after headers['Content-Length'] = len(self.data) @@ -159,10 +159,7 @@ def build_stages(self): """ Processes and loads an array of stages from the test dictionary """ - return map( - lambda stage_dict: Stage(stage_dict['stage']), - self.test_dict['stages'] - ) + return [Stage(stage_dict['stage']) for stage_dict in self.test_dict['stages']] class Ruleset(object): @@ -184,10 +181,7 @@ def extract_tests(self): creates test objects based on input """ try: - return map( - lambda test_dict: Test(test_dict, self.meta), - self.yaml_file['tests'] - ) + return [Test(test_dict, self.meta) for test_dict in self.yaml_file['tests']] except errors.TestError as e: e.args[1]['meta'] = self.meta raise e diff --git a/ftw/testrunner.py b/ftw/testrunner.py index fb0d0ba..12fbda0 100644 --- a/ftw/testrunner.py +++ b/ftw/testrunner.py @@ -1,4 +1,4 @@ -from __future__ import print_function + import datetime from dateutil import parser import pytest diff --git a/ftw/util.py b/ftw/util.py index b5f8f0b..ce7fdea 100644 --- a/ftw/util.py +++ b/ftw/util.py @@ -1,4 +1,4 @@ -from __future__ import print_function + import io import yaml import os diff --git a/ftw/util/ironbee.py b/ftw/util/ironbee.py index cb36089..59de95b 100644 --- a/ftw/util/ironbee.py +++ b/ftw/util/ironbee.py @@ -1,4 +1,4 @@ -from __future__ import print_function + import os import request_to_yaml diff --git a/test/integration/test_http.py b/test/integration/test_http.py index 54e41f1..d5d439e 100644 --- a/test/integration/test_http.py +++ b/test/integration/test_http.py @@ -1,4 +1,4 @@ -from __future__ import print_function + from ftw import ruleset, http, errors import pytest @@ -13,7 +13,7 @@ def test_cookies1(): http_ua.send_request(x) with pytest.raises(KeyError): print(http_ua.request_object.headers["cookie"]) - assert("set-cookie" in http_ua.response_object.headers.keys()) + assert("set-cookie" in list(http_ua.response_object.headers.keys())) cookie_data = http_ua.response_object.headers["set-cookie"] cookie_var = cookie_data.split("=")[0] x = ruleset.Input(protocol="https", port=443, dest_addr="www.ieee.org", diff --git a/test/unit/test_ruleset.py b/test/unit/test_ruleset.py index d2d5035..ac15c97 100644 --- a/test/unit/test_ruleset.py +++ b/test/unit/test_ruleset.py @@ -25,7 +25,7 @@ def test_input(): dictionary = {} dictionary['headers'] = headers input_2 = ruleset.Input(**dictionary) - assert(len(input_2.headers.keys()) == 2) + assert(len(list(input_2.headers.keys())) == 2) dictionary_2 = {'random_key': 'bar'} with pytest.raises(TypeError): ruleset.Input(**dictionary_2)