From d4f911fd0923c106e3a54411ebc7afeb9b24d37a Mon Sep 17 00:00:00 2001 From: Lucky <25218389+luckyvs1@users.noreply.github.com> Date: Tue, 30 Apr 2019 13:00:05 -0700 Subject: [PATCH 1/3] Add support for case insensitive header parameter and update README --- README.md | 2 +- statcode/statcode.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2369e38..1e063cd 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,6 @@ You can install the [`statcode`](https://aur.archlinux.org/packages/statcode/) p ## Contributing -This is a pretty small project (something I put together on a plane ride), but with enough help it could turn into a go-to manual for everything HTTP-related. For example, it should be possible to look up different [request headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers), e.g. running `$ statcode cache-control` and getting "Specifies directives for caching mechanisms in both requests and responses." If you'd like to help make this happen, feel free to fork the repo and contribute. +This is a pretty small project (something I put together on a plane ride), but with enough help it could turn into a go-to manual for everything HTTP-related. If you'd like to help make this happen, feel free to fork the repo and contribute. If you've discovered a bug or have a feature request, create an [issue](https://github.com/shobrook/statcode/issues/new) and I'll take care of it! diff --git a/statcode/statcode.py b/statcode/statcode.py index d642b4a..1401532 100644 --- a/statcode/statcode.py +++ b/statcode/statcode.py @@ -250,7 +250,11 @@ def output_without_ui(content): def generate_content(status_code): try: code_descriptions, num, status_code = get_yaml_dictionary(status_code) - content = code_descriptions[status_code] + if type(status_code) is not int: + # Do a case insensitive key comparison for HTTP header values + content = [code_descriptions[key] for key in code_descriptions if key.lower() == status_code.lower()][0] + else: + content = code_descriptions[status_code] pile = urwid.Pile([ urwid.Text("STATCODE: The Manual for HTTP Status Codes and Headers\n", align="center"), urwid.Text(("title", "STATUS MESSAGE" if num else "HEADER INFO")), From 6f44469f74feaabecba5d6c88ed4bd20c496100f Mon Sep 17 00:00:00 2001 From: Lucky <25218389+luckyvs1@users.noreply.github.com> Date: Tue, 30 Apr 2019 15:11:51 -0700 Subject: [PATCH 2/3] Refactor get content and add unit tests using pytest --- statcode/statcode.py | 14 +++++++--- tests/__init__.py | 0 tests/test_parameters.py | 58 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_parameters.py diff --git a/statcode/statcode.py b/statcode/statcode.py index 1401532..aefe04b 100644 --- a/statcode/statcode.py +++ b/statcode/statcode.py @@ -247,14 +247,22 @@ def output_without_ui(content): text = ("\n".join(text.decode("utf-8") for text in canvas.text)).rstrip() print(text) -def generate_content(status_code): +def get_content(code_descriptions, status_code): try: - code_descriptions, num, status_code = get_yaml_dictionary(status_code) if type(status_code) is not int: # Do a case insensitive key comparison for HTTP header values content = [code_descriptions[key] for key in code_descriptions if key.lower() == status_code.lower()][0] else: content = code_descriptions[status_code] + return content + except (KeyError, IndexError): # IndexError is due to looking for an invalid key + raise KeyError + +def generate_content(status_code): + try: + code_descriptions, num, status_code = get_yaml_dictionary(status_code) + content = get_content(code_descriptions, status_code) + pile = urwid.Pile([ urwid.Text("STATCODE: The Manual for HTTP Status Codes and Headers\n", align="center"), urwid.Text(("title", "STATUS MESSAGE" if num else "HEADER INFO")), @@ -271,7 +279,7 @@ def generate_content(status_code): padding = urwid.Padding(Scrollable(pile), left=1, right=1) return padding - except KeyError: # None is used to print "not recognized", so KeyError. Other errors have nothing to do with it + except (KeyError): # None is used to print "not recognized", so KeyError. Other errors have nothing to do with it return None def __load_file_data(num): diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_parameters.py b/tests/test_parameters.py new file mode 100644 index 0000000..e219659 --- /dev/null +++ b/tests/test_parameters.py @@ -0,0 +1,58 @@ +import statcode.statcode +import pytest + + +def test_valid_numeric_param(): + content = None + code_descriptions, num, status_code = statcode.statcode.get_yaml_dictionary("200") + content = statcode.statcode.get_content(code_descriptions, status_code) + assert content is not None + return + + +def test_invalid_numeric_param(): + content = None + code_descriptions, num, status_code = statcode.statcode.get_yaml_dictionary("999") + try: + content = statcode.statcode.get_content(code_descriptions, status_code) + except KeyError: + assert content is None + return + + +def test_header_param(): + content = None + code_descriptions, num, status_code = statcode.statcode.get_yaml_dictionary("Content-Length") + content = statcode.statcode.get_content(code_descriptions, status_code) + assert content is not None + return + + +def test_lower_case_header_param(): + content = None + code_descriptions, num, status_code = statcode.statcode.get_yaml_dictionary("content-md5") + content = statcode.statcode.get_content(code_descriptions, status_code) + assert content is not None + return + + +def test_case_insensitive_header_params(): + code_descriptions, num, status_code = statcode.statcode.get_yaml_dictionary("cache-control") + first_content = statcode.statcode.get_content(code_descriptions, status_code) + + code_descriptions, num, status_code = statcode.statcode.get_yaml_dictionary("Cache-Control") + second_content = statcode.statcode.get_content(code_descriptions, status_code) + + assert first_content == second_content + return + + +def test_invalid_header_param(): + content = None + code_descriptions, num, status_code = statcode.statcode.get_yaml_dictionary("invalid_parameter") + try: + content = statcode.statcode.get_content(code_descriptions, status_code) + except KeyError: + assert content is None + return + From 6f55859bff759116a6d447d9864c4b543b33562b Mon Sep 17 00:00:00 2001 From: Lucky <25218389+luckyvs1@users.noreply.github.com> Date: Tue, 30 Apr 2019 15:15:19 -0700 Subject: [PATCH 3/3] Remove redundant parentheses --- statcode/statcode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/statcode/statcode.py b/statcode/statcode.py index aefe04b..0fadfb4 100644 --- a/statcode/statcode.py +++ b/statcode/statcode.py @@ -279,7 +279,7 @@ def generate_content(status_code): padding = urwid.Padding(Scrollable(pile), left=1, right=1) return padding - except (KeyError): # None is used to print "not recognized", so KeyError. Other errors have nothing to do with it + except KeyError: # None is used to print "not recognized", so KeyError. Other errors have nothing to do with it return None def __load_file_data(num):