mirror of
https://github.com/httpie/cli.git
synced 2025-03-13 06:18:33 +01:00
Merge 4c5ee9b286
into 5b604c37c6
This commit is contained in:
commit
7c2a448da2
126
coverage_after.txt
Normal file
126
coverage_after.txt
Normal file
@ -0,0 +1,126 @@
|
||||
Name Stmts Miss Branch BrPart Cover
|
||||
---------------------------------------------------------------------------------
|
||||
httpie/__init__.py 4 0 0 0 100%
|
||||
httpie/__main__.py 8 0 0 0 100%
|
||||
httpie/adapters.py 7 0 0 0 100%
|
||||
httpie/cli/__init__.py 0 0 0 0 100%
|
||||
httpie/cli/argparser.py 316 20 152 11 92%
|
||||
httpie/cli/argtemplate.py 99 0 38 0 100%
|
||||
httpie/cli/argtypes.py 135 7 36 1 95%
|
||||
httpie/cli/constants.py 46 0 2 0 100%
|
||||
httpie/cli/definition.py 105 8 14 3 89%
|
||||
httpie/cli/dicts.py 40 1 16 2 95%
|
||||
httpie/cli/exceptions.py 2 0 0 0 100%
|
||||
httpie/cli/nested_json/__init__.py 4 0 0 0 100%
|
||||
httpie/cli/nested_json/errors.py 14 0 2 0 100%
|
||||
httpie/cli/nested_json/interpret.py 77 3 40 4 94%
|
||||
httpie/cli/nested_json/parse.py 107 5 44 4 94%
|
||||
httpie/cli/nested_json/tokens.py 49 4 12 1 85%
|
||||
httpie/cli/options.py 124 3 48 1 98%
|
||||
httpie/cli/requestitems.py 100 8 28 0 92%
|
||||
httpie/cli/utils.py 41 2 10 0 96%
|
||||
httpie/client.py 169 0 76 4 98%
|
||||
httpie/compat.py 51 31 16 3 34%
|
||||
httpie/config.py 95 2 28 1 98%
|
||||
httpie/context.py 122 14 38 6 86%
|
||||
httpie/cookies.py 11 0 2 0 100%
|
||||
httpie/core.py 202 18 102 15 89%
|
||||
httpie/downloads.py 175 6 60 9 94%
|
||||
httpie/encoding.py 18 0 6 1 96%
|
||||
httpie/internal/__build_channel__.py 1 0 0 0 100%
|
||||
httpie/internal/__init__.py 0 0 0 0 100%
|
||||
httpie/internal/daemon_runner.py 28 4 4 0 88%
|
||||
httpie/internal/daemons.py 58 30 14 3 43%
|
||||
httpie/internal/update_warnings.py 96 5 42 4 92%
|
||||
httpie/legacy/__init__.py 0 0 0 0 100%
|
||||
httpie/legacy/v3_1_0_session_cookie_format.py 33 1 26 2 95%
|
||||
httpie/legacy/v3_2_0_session_header_format.py 27 1 16 1 95%
|
||||
httpie/manager/__init__.py 0 0 0 0 100%
|
||||
httpie/manager/__main__.py 33 5 6 0 87%
|
||||
httpie/manager/cli.py 39 0 8 0 100%
|
||||
httpie/manager/compat.py 35 13 10 1 51%
|
||||
httpie/manager/core.py 20 1 8 1 93%
|
||||
httpie/manager/tasks/__init__.py 5 0 0 0 100%
|
||||
httpie/manager/tasks/check_updates.py 8 0 0 0 100%
|
||||
httpie/manager/tasks/export_args.py 14 1 2 1 88%
|
||||
httpie/manager/tasks/plugins.py 153 8 64 12 91%
|
||||
httpie/manager/tasks/sessions.py 43 2 18 2 93%
|
||||
httpie/models.py 112 5 44 1 96%
|
||||
httpie/output/__init__.py 0 0 0 0 100%
|
||||
httpie/output/formatters/__init__.py 0 0 0 0 100%
|
||||
httpie/output/formatters/colors.py 140 0 30 1 99%
|
||||
httpie/output/formatters/headers.py 9 0 2 0 100%
|
||||
httpie/output/formatters/json.py 16 0 3 0 100%
|
||||
httpie/output/formatters/xml.py 43 1 16 3 93%
|
||||
httpie/output/lexers/__init__.py 0 0 0 0 100%
|
||||
httpie/output/lexers/common.py 4 0 2 0 100%
|
||||
httpie/output/lexers/http.py 22 3 2 1 83%
|
||||
httpie/output/lexers/json.py 10 0 0 0 100%
|
||||
httpie/output/lexers/metadata.py 17 3 4 2 76%
|
||||
httpie/output/models.py 27 1 8 0 97%
|
||||
httpie/output/processing.py 37 0 22 2 97%
|
||||
httpie/output/streams.py 138 2 54 1 98%
|
||||
httpie/output/ui/__init__.py 0 0 0 0 100%
|
||||
httpie/output/ui/palette.py 72 9 18 3 84%
|
||||
httpie/output/ui/rich_help.py 104 49 38 0 49%
|
||||
httpie/output/ui/rich_palette.py 33 3 8 1 90%
|
||||
httpie/output/ui/rich_progress.py 62 15 14 4 72%
|
||||
httpie/output/utils.py 20 0 0 0 100%
|
||||
httpie/output/writer.py 76 13 30 1 79%
|
||||
httpie/plugins/__init__.py 2 0 0 0 100%
|
||||
httpie/plugins/base.py 38 4 2 0 90%
|
||||
httpie/plugins/builtin.py 39 0 2 0 100%
|
||||
httpie/plugins/manager.py 67 1 22 1 98%
|
||||
httpie/plugins/registry.py 8 0 0 0 100%
|
||||
httpie/sessions.py 155 3 64 2 98%
|
||||
httpie/ssl_.py 37 2 13 0 96%
|
||||
httpie/status.py 20 0 8 0 100%
|
||||
httpie/uploads.py 132 19 56 5 83%
|
||||
httpie/utils.py 131 15 42 3 85%
|
||||
tests/__init__.py 0 0 0 0 100%
|
||||
tests/conftest.py 46 11 20 3 79%
|
||||
tests/fixtures/__init__.py 37 0 4 0 100%
|
||||
tests/test_argtemplate.py 197 0 28 0 100%
|
||||
tests/test_auth.py 79 0 20 0 100%
|
||||
tests/test_auth_plugins.py 76 0 4 1 99%
|
||||
tests/test_binary.py 32 0 2 0 100%
|
||||
tests/test_cli.py 200 0 10 0 100%
|
||||
tests/test_cli_ui.py 20 0 4 0 100%
|
||||
tests/test_cli_utils.py 42 0 4 0 100%
|
||||
tests/test_compress.py 60 0 0 0 100%
|
||||
tests/test_config.py 72 2 10 0 98%
|
||||
tests/test_cookie.py 26 0 0 0 100%
|
||||
tests/test_cookie_on_redirects.py 63 0 26 0 100%
|
||||
tests/test_defaults.py 79 0 0 0 100%
|
||||
tests/test_downloads.py 129 0 30 0 100%
|
||||
tests/test_encoding.py 111 0 40 0 100%
|
||||
tests/test_errors.py 45 0 14 0 100%
|
||||
tests/test_exit_status.py 42 0 4 0 100%
|
||||
tests/test_httpie.py 209 4 30 0 98%
|
||||
tests/test_httpie_cli.py 63 0 28 0 100%
|
||||
tests/test_json.py 76 0 36 2 98%
|
||||
tests/test_meta.py 12 0 2 0 100%
|
||||
tests/test_offline.py 36 0 0 0 100%
|
||||
tests/test_output.py 249 1 65 1 99%
|
||||
tests/test_parser_schema.py 11 0 2 0 100%
|
||||
tests/test_plugins_cli.py 89 0 44 0 100%
|
||||
tests/test_redirects.py 50 1 16 0 98%
|
||||
tests/test_regressions.py 18 0 2 0 100%
|
||||
tests/test_sessions.py 339 1 97 2 99%
|
||||
tests/test_ssl.py 108 8 40 4 91%
|
||||
tests/test_stream.py 66 0 20 0 100%
|
||||
tests/test_tokens.py 63 0 0 0 100%
|
||||
tests/test_transport_plugin.py 30 0 0 0 100%
|
||||
tests/test_update_warnings.py 117 1 36 1 99%
|
||||
tests/test_uploads.py 182 6 40 0 97%
|
||||
tests/test_windows.py 15 1 4 0 95%
|
||||
tests/test_xml.py 41 1 22 1 97%
|
||||
tests/utils/__init__.py 236 18 63 8 90%
|
||||
tests/utils/http_server.py 101 1 42 2 98%
|
||||
tests/utils/matching/__init__.py 10 0 2 0 100%
|
||||
tests/utils/matching/parsing.py 49 1 20 2 96%
|
||||
tests/utils/matching/test_matching.py 46 0 0 0 100%
|
||||
tests/utils/matching/tokens.py 17 0 0 0 100%
|
||||
tests/utils/plugins_cli.py 125 1 50 2 98%
|
||||
---------------------------------------------------------------------------------
|
||||
TOTAL 7999 409 2373 153 94%
|
124
coverage_before.txt
Normal file
124
coverage_before.txt
Normal file
@ -0,0 +1,124 @@
|
||||
Name Stmts Miss Branch BrPart Cover
|
||||
---------------------------------------------------------------------------------
|
||||
httpie/__init__.py 4 0 0 0 100%
|
||||
httpie/__main__.py 8 0 0 0 100%
|
||||
httpie/adapters.py 7 0 0 0 100%
|
||||
httpie/cli/__init__.py 0 0 0 0 100%
|
||||
httpie/cli/argparser.py 316 20 152 11 92%
|
||||
httpie/cli/argtypes.py 135 7 36 1 95%
|
||||
httpie/cli/constants.py 46 0 2 0 100%
|
||||
httpie/cli/definition.py 105 8 14 3 89%
|
||||
httpie/cli/dicts.py 40 1 16 2 95%
|
||||
httpie/cli/exceptions.py 2 0 0 0 100%
|
||||
httpie/cli/nested_json/__init__.py 4 0 0 0 100%
|
||||
httpie/cli/nested_json/errors.py 14 0 2 0 100%
|
||||
httpie/cli/nested_json/interpret.py 77 3 40 4 94%
|
||||
httpie/cli/nested_json/parse.py 107 5 44 4 94%
|
||||
httpie/cli/nested_json/tokens.py 49 4 12 1 85%
|
||||
httpie/cli/options.py 124 3 48 1 98%
|
||||
httpie/cli/requestitems.py 100 8 28 0 92%
|
||||
httpie/cli/utils.py 41 2 10 0 96%
|
||||
httpie/client.py 169 0 76 4 98%
|
||||
httpie/compat.py 51 31 16 3 34%
|
||||
httpie/config.py 95 2 28 1 98%
|
||||
httpie/context.py 122 14 38 6 86%
|
||||
httpie/cookies.py 11 0 2 0 100%
|
||||
httpie/core.py 170 8 80 9 93%
|
||||
httpie/downloads.py 175 6 60 9 94%
|
||||
httpie/encoding.py 18 0 6 1 96%
|
||||
httpie/internal/__build_channel__.py 1 0 0 0 100%
|
||||
httpie/internal/__init__.py 0 0 0 0 100%
|
||||
httpie/internal/daemon_runner.py 28 4 4 0 88%
|
||||
httpie/internal/daemons.py 58 30 14 3 43%
|
||||
httpie/internal/update_warnings.py 96 5 42 4 92%
|
||||
httpie/legacy/__init__.py 0 0 0 0 100%
|
||||
httpie/legacy/v3_1_0_session_cookie_format.py 33 1 26 2 95%
|
||||
httpie/legacy/v3_2_0_session_header_format.py 27 1 16 1 95%
|
||||
httpie/manager/__init__.py 0 0 0 0 100%
|
||||
httpie/manager/__main__.py 33 5 6 0 87%
|
||||
httpie/manager/cli.py 39 0 8 0 100%
|
||||
httpie/manager/compat.py 35 13 10 1 51%
|
||||
httpie/manager/core.py 20 1 8 1 93%
|
||||
httpie/manager/tasks/__init__.py 5 0 0 0 100%
|
||||
httpie/manager/tasks/check_updates.py 8 0 0 0 100%
|
||||
httpie/manager/tasks/export_args.py 14 1 2 1 88%
|
||||
httpie/manager/tasks/plugins.py 153 8 64 12 91%
|
||||
httpie/manager/tasks/sessions.py 43 2 18 2 93%
|
||||
httpie/models.py 112 5 44 1 96%
|
||||
httpie/output/__init__.py 0 0 0 0 100%
|
||||
httpie/output/formatters/__init__.py 0 0 0 0 100%
|
||||
httpie/output/formatters/colors.py 140 0 30 1 99%
|
||||
httpie/output/formatters/headers.py 9 0 2 0 100%
|
||||
httpie/output/formatters/json.py 16 0 3 0 100%
|
||||
httpie/output/formatters/xml.py 43 1 16 3 93%
|
||||
httpie/output/lexers/__init__.py 0 0 0 0 100%
|
||||
httpie/output/lexers/common.py 4 0 2 0 100%
|
||||
httpie/output/lexers/http.py 22 3 2 1 83%
|
||||
httpie/output/lexers/json.py 10 0 0 0 100%
|
||||
httpie/output/lexers/metadata.py 17 3 4 2 76%
|
||||
httpie/output/models.py 27 1 8 0 97%
|
||||
httpie/output/processing.py 37 0 22 2 97%
|
||||
httpie/output/streams.py 138 2 54 1 98%
|
||||
httpie/output/ui/__init__.py 0 0 0 0 100%
|
||||
httpie/output/ui/palette.py 72 9 18 3 84%
|
||||
httpie/output/ui/rich_help.py 104 49 38 0 49%
|
||||
httpie/output/ui/rich_palette.py 33 3 8 1 90%
|
||||
httpie/output/ui/rich_progress.py 62 15 14 4 72%
|
||||
httpie/output/utils.py 20 0 0 0 100%
|
||||
httpie/output/writer.py 76 13 30 1 79%
|
||||
httpie/plugins/__init__.py 2 0 0 0 100%
|
||||
httpie/plugins/base.py 38 4 2 0 90%
|
||||
httpie/plugins/builtin.py 39 0 2 0 100%
|
||||
httpie/plugins/manager.py 67 1 22 1 98%
|
||||
httpie/plugins/registry.py 8 0 0 0 100%
|
||||
httpie/sessions.py 155 3 64 2 98%
|
||||
httpie/ssl_.py 37 2 13 0 96%
|
||||
httpie/status.py 20 0 8 0 100%
|
||||
httpie/uploads.py 132 19 56 5 83%
|
||||
httpie/utils.py 131 15 42 3 85%
|
||||
tests/__init__.py 0 0 0 0 100%
|
||||
tests/conftest.py 46 11 20 3 79%
|
||||
tests/fixtures/__init__.py 37 0 4 0 100%
|
||||
tests/test_auth.py 79 0 20 0 100%
|
||||
tests/test_auth_plugins.py 76 0 4 1 99%
|
||||
tests/test_binary.py 32 0 2 0 100%
|
||||
tests/test_cli.py 200 0 10 0 100%
|
||||
tests/test_cli_ui.py 20 0 4 0 100%
|
||||
tests/test_cli_utils.py 42 0 4 0 100%
|
||||
tests/test_compress.py 60 0 0 0 100%
|
||||
tests/test_config.py 72 2 10 0 98%
|
||||
tests/test_cookie.py 26 0 0 0 100%
|
||||
tests/test_cookie_on_redirects.py 63 0 26 0 100%
|
||||
tests/test_defaults.py 79 0 0 0 100%
|
||||
tests/test_downloads.py 129 0 30 0 100%
|
||||
tests/test_encoding.py 111 0 40 0 100%
|
||||
tests/test_errors.py 45 0 14 0 100%
|
||||
tests/test_exit_status.py 42 0 4 0 100%
|
||||
tests/test_httpie.py 180 4 22 0 98%
|
||||
tests/test_httpie_cli.py 63 0 28 0 100%
|
||||
tests/test_json.py 76 0 36 2 98%
|
||||
tests/test_meta.py 12 0 2 0 100%
|
||||
tests/test_offline.py 36 0 0 0 100%
|
||||
tests/test_output.py 249 1 65 1 99%
|
||||
tests/test_parser_schema.py 11 0 2 0 100%
|
||||
tests/test_plugins_cli.py 89 0 44 0 100%
|
||||
tests/test_redirects.py 50 1 16 0 98%
|
||||
tests/test_regressions.py 18 0 2 0 100%
|
||||
tests/test_sessions.py 339 1 97 2 99%
|
||||
tests/test_ssl.py 108 8 40 4 91%
|
||||
tests/test_stream.py 66 0 20 0 100%
|
||||
tests/test_tokens.py 63 0 0 0 100%
|
||||
tests/test_transport_plugin.py 30 0 0 0 100%
|
||||
tests/test_update_warnings.py 117 1 36 1 99%
|
||||
tests/test_uploads.py 182 6 40 0 97%
|
||||
tests/test_windows.py 15 1 4 0 95%
|
||||
tests/test_xml.py 41 1 22 1 97%
|
||||
tests/utils/__init__.py 236 18 63 8 90%
|
||||
tests/utils/http_server.py 101 1 42 2 98%
|
||||
tests/utils/matching/__init__.py 10 0 2 0 100%
|
||||
tests/utils/matching/parsing.py 49 1 20 2 96%
|
||||
tests/utils/matching/test_matching.py 46 0 0 0 100%
|
||||
tests/utils/matching/tokens.py 17 0 0 0 100%
|
||||
tests/utils/plugins_cli.py 125 1 50 2 98%
|
||||
---------------------------------------------------------------------------------
|
||||
TOTAL 7642 399 2277 147 93%
|
156
httpie/cli/argtemplate.py
Normal file
156
httpie/cli/argtemplate.py
Normal file
@ -0,0 +1,156 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
TEMPLATE_FILE = "httpie/cli/templates.json"
|
||||
|
||||
|
||||
def store_json_template(args):
|
||||
"""
|
||||
Store a template as a string in templates.json, the format for running this is:
|
||||
http template <name> |<method>| <url> login=testuser password=testpassword ...
|
||||
"""
|
||||
template_name = args.pop(0)
|
||||
template_method = None
|
||||
template_url = None
|
||||
template_variables = {}
|
||||
|
||||
if ["CONNECT", "DELETE", "GET", "HEAD", "OPTIONS", "POST", "PUT", "TRACE", "PATCH"].__contains__(args[0].upper()):
|
||||
template_method = args.pop(0)
|
||||
template_url = args.pop(0)
|
||||
else:
|
||||
temp = args.pop(0)
|
||||
print(f"'{temp}' is not a valid http method, defaulting to null...")
|
||||
template_url = args.pop(0)
|
||||
|
||||
for arg in args:
|
||||
if '=' in arg:
|
||||
variable_name, variable_value = arg.split("=")
|
||||
template_variables[variable_name] = variable_value
|
||||
|
||||
template = {}
|
||||
template['method'] = template_method
|
||||
template['url'] = template_url
|
||||
template['data'] = template_variables
|
||||
|
||||
# Check if the templates.json file exists
|
||||
if not os.path.isfile(TEMPLATE_FILE):
|
||||
open(TEMPLATE_FILE, "w").close()
|
||||
|
||||
with open(TEMPLATE_FILE, "r+") as f:
|
||||
stored_templates = {}
|
||||
try:
|
||||
stored_templates = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
if template_name in stored_templates:
|
||||
stored_templates.pop(template_name)
|
||||
stored_templates[template_name] = template
|
||||
f.seek(0)
|
||||
json.dump(stored_templates, f)
|
||||
f.truncate()
|
||||
|
||||
|
||||
def edit_json_template(args):
|
||||
"""
|
||||
Edit a template in templates.json, the format for running this is:
|
||||
http editt <name> <item> <value>
|
||||
"""
|
||||
stored_templates = {}
|
||||
|
||||
# Check if the templates.json file exists
|
||||
if not os.path.isfile(TEMPLATE_FILE):
|
||||
open(TEMPLATE_FILE, "w").close()
|
||||
|
||||
with open(TEMPLATE_FILE, "r+") as f:
|
||||
try:
|
||||
stored_templates = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
template_name = args.pop(0)
|
||||
template_item = args.pop(0)
|
||||
template_value = args.pop(0)
|
||||
|
||||
# Check if the template exists
|
||||
if template_name not in stored_templates:
|
||||
print(f"Template '{template_name}' does not exist.")
|
||||
return
|
||||
|
||||
# Update the HTTP method
|
||||
if template_item == 'method':
|
||||
stored_templates[template_name]['method'] = template_value.upper()
|
||||
# Update the URL
|
||||
elif template_item == 'url':
|
||||
stored_templates[template_name]['url'] = template_value
|
||||
# Update a key-value pair in the data dictionary
|
||||
elif template_item in stored_templates[template_name]['data']:
|
||||
stored_templates[template_name]['data'][template_item] = template_value
|
||||
# Add a new key-value pair to the data dictionary
|
||||
else:
|
||||
stored_templates[template_name]['data'][template_item] = template_value
|
||||
|
||||
# Save the updated template to file
|
||||
f.seek(0)
|
||||
json.dump(stored_templates, f)
|
||||
f.truncate()
|
||||
|
||||
|
||||
def delete_template(arg):
|
||||
"""
|
||||
Tries to delete the template with name 'arg'
|
||||
Usage format: http delt <arg>
|
||||
"""
|
||||
stored_templates = {}
|
||||
|
||||
# Check if the templates.json file exists
|
||||
if not os.path.isfile(TEMPLATE_FILE):
|
||||
open(TEMPLATE_FILE, "w").close()
|
||||
|
||||
with open(TEMPLATE_FILE, "r+") as f:
|
||||
try:
|
||||
stored_templates = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
# Check if the template exists
|
||||
if arg not in stored_templates:
|
||||
print(f"Template '{arg}' does not exist.")
|
||||
return
|
||||
del stored_templates[arg]
|
||||
# Save the updated template to file
|
||||
f.seek(0)
|
||||
json.dump(stored_templates, f)
|
||||
f.truncate()
|
||||
|
||||
|
||||
def load_template(arg):
|
||||
"""
|
||||
Load a template from templates.json and return a list of arguments to be passed to the main function
|
||||
the format for running this is:
|
||||
http runt <template>
|
||||
"""
|
||||
|
||||
# Check if the templates.json file exists
|
||||
if not os.path.isfile(TEMPLATE_FILE):
|
||||
open(TEMPLATE_FILE, "w").close()
|
||||
|
||||
with open(TEMPLATE_FILE, "r+") as f:
|
||||
stored_templates = {}
|
||||
try:
|
||||
stored_templates = json.load(f)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
args = []
|
||||
args_dict = None
|
||||
try:
|
||||
args_dict = stored_templates[arg]
|
||||
except KeyError:
|
||||
print(f"Template '{arg}' does not exist.")
|
||||
if args_dict is not None:
|
||||
args.append(args_dict.pop('method'))
|
||||
args.append(args_dict.pop('url'))
|
||||
data_dict = args_dict.pop('data')
|
||||
for key, value in data_dict.items():
|
||||
args.append(key + "=" + value)
|
||||
return args
|
1
httpie/cli/templates.json
Normal file
1
httpie/cli/templates.json
Normal file
@ -0,0 +1 @@
|
||||
{"nytemplate2": {"method": "POST", "url": "google.com", "data": {"name": "adam"}}, "nytemplate": {"method": "GET", "url": "youtube.com", "data": {"name": "porsev"}}}
|
@ -48,6 +48,44 @@ def raw_main(
|
||||
if use_default_options and env.config.default_options:
|
||||
args = env.config.default_options + args
|
||||
|
||||
if len(args) > 0:
|
||||
# Unlike when running httpie commands without templates, when creating templates you need to specify an http method
|
||||
if (args[0] == "template"):
|
||||
# http template <name> <method> <url> |<REQUEST_ITEM>|
|
||||
if len(args) < 2:
|
||||
return ExitStatus.ERROR
|
||||
if len(args) < 4:
|
||||
return ExitStatus.ERROR
|
||||
from httpie.cli.argtemplate import store_json_template
|
||||
store_json_template(args[1:])
|
||||
return ExitStatus.SUCCESS
|
||||
elif (args[0] == "runt"):
|
||||
# http runt <name>
|
||||
if len(args) < 2:
|
||||
print("No template name was specified")
|
||||
return ExitStatus.ERROR
|
||||
from httpie.cli.argtemplate import store_json_template, load_template
|
||||
args = load_template(args[1])
|
||||
elif (args[0] == "editt"):
|
||||
# http editt <name> <request_item> <new_value>
|
||||
if len(args) < 2:
|
||||
print("No template name was specified")
|
||||
return ExitStatus.ERROR
|
||||
if len(args) < 4:
|
||||
print("Specify at least one parameter and its new value to edit a template")
|
||||
return ExitStatus.ERROR
|
||||
from httpie.cli.argtemplate import edit_json_template
|
||||
edit_json_template(args[1:])
|
||||
return ExitStatus.SUCCESS
|
||||
elif (args[0] == "delt"):
|
||||
# http delt <name>
|
||||
if len(args) < 2:
|
||||
print("No template name was specified")
|
||||
return ExitStatus.ERROR
|
||||
from httpie.cli.argtemplate import delete_template
|
||||
delete_template(args[1])
|
||||
return ExitStatus.SUCCESS
|
||||
|
||||
include_debug_info = '--debug' in args
|
||||
include_traceback = include_debug_info or '--traceback' in args
|
||||
|
||||
|
1273
test_log_after.txt
Normal file
1273
test_log_after.txt
Normal file
File diff suppressed because it is too large
Load Diff
1506
test_log_before.txt
Normal file
1506
test_log_before.txt
Normal file
File diff suppressed because it is too large
Load Diff
332
tests/test_argtemplate.py
Normal file
332
tests/test_argtemplate.py
Normal file
@ -0,0 +1,332 @@
|
||||
""" Tests for creating and updating command templates """
|
||||
import httpie.cli.argtemplate
|
||||
import tempfile
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
class TestStoreTemplate:
|
||||
|
||||
def test_store_normal_template_with_method(self):
|
||||
"""
|
||||
Tests that a valid template can be stored properly when the template contains a method parameter
|
||||
"""
|
||||
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
|
||||
stored_templates = json.load(temp_fp)
|
||||
assert stored_templates[args[2]] is not None
|
||||
|
||||
template = stored_templates[args[2]]
|
||||
assert template['method'] == args[3]
|
||||
assert template['url'] == args[4]
|
||||
assert template['data'] is not None
|
||||
|
||||
template_data = template['data']
|
||||
assert template_data['param1'] == 'value1'
|
||||
assert template_data['param2'] == 'value2'
|
||||
|
||||
def test_store_template_overwrites_old_template_same_name(self):
|
||||
"""
|
||||
Tests that storing a template with the same name as an already existing template will overwrite the existing template
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
|
||||
old_stored_templates = json.load(temp_fp)
|
||||
|
||||
command = 'http template test_template POST https://catfact.ninja/fact param1=value2 param2=value1'
|
||||
args = command.split()
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
|
||||
temp_fp.seek(0)
|
||||
new_stored_templates = json.load(temp_fp)
|
||||
|
||||
assert len(old_stored_templates) == len(new_stored_templates)
|
||||
assert new_stored_templates[args[2]]['method'] == 'POST'
|
||||
assert new_stored_templates[args[2]]['data']['param1'] == 'value2'
|
||||
assert new_stored_templates[args[2]]['data']['param2'] == 'value1'
|
||||
|
||||
def test_store_template_invalid_method(self):
|
||||
"""
|
||||
Tests that storing a template with an invalid method argument leads to the template being saved without a method parameter
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template NOT_VALID_METHOD https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
|
||||
stored_templates = json.load(temp_fp)
|
||||
template = stored_templates[args[2]]
|
||||
assert template is not None
|
||||
assert template['url'] == args[4]
|
||||
assert template['method'] is None
|
||||
assert template['data']['param1'] == 'value1'
|
||||
assert template['data']['param2'] == 'value2'
|
||||
|
||||
def test_store_template_no_templates_json(self):
|
||||
"""
|
||||
Tests that a new file will be created for storing templates if templates.json (TEMPLATE_FILE) doesn't exist
|
||||
"""
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = "NOT_A_REAL_FILE"
|
||||
assert not os.path.isfile(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(["fake_template", "GET", "https://catfact.ninja/fact"])
|
||||
assert os.path.isfile(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
fp = open(httpie.cli.argtemplate.TEMPLATE_FILE, "r")
|
||||
templates = json.load(fp)
|
||||
|
||||
assert templates["fake_template"]["method"] == "GET"
|
||||
assert templates["fake_template"]["url"] == "https://catfact.ninja/fact"
|
||||
|
||||
os.remove(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
|
||||
class TestEditTemplate:
|
||||
def test_edit_template_update_value(self):
|
||||
"""
|
||||
Tests that the edit_json_template function can correctly update a value
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
|
||||
httpie.cli.argtemplate.edit_json_template(['test_template', 'param1', 'newvalue1'])
|
||||
|
||||
stored_templates = json.load(temp_fp)
|
||||
assert stored_templates[args[2]] is not None
|
||||
|
||||
template = stored_templates[args[2]]
|
||||
assert template['method'] == 'GET'
|
||||
assert template['url'] == args[4]
|
||||
assert template['data'] is not None
|
||||
|
||||
template_data = template['data']
|
||||
assert template_data['param1'] == 'newvalue1'
|
||||
assert template_data['param2'] == 'value2'
|
||||
|
||||
def test_edit_template_change_url(self):
|
||||
"""
|
||||
Tests that you can change the URL of a template
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
|
||||
httpie.cli.argtemplate.edit_json_template(['test_template', 'url', 'https://fake-url/'])
|
||||
|
||||
stored_templates = json.load(temp_fp)
|
||||
assert len(stored_templates) == 1
|
||||
assert stored_templates[args[2]] is not None
|
||||
|
||||
template = stored_templates[args[2]]
|
||||
assert template['method'] == 'GET'
|
||||
assert template['url'] == 'https://fake-url/'
|
||||
assert template['data'] is not None
|
||||
|
||||
def test_edit_template_change_method(self):
|
||||
"""
|
||||
Tests that you can change the method of a template
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
|
||||
httpie.cli.argtemplate.edit_json_template(['test_template', 'method', 'POST'])
|
||||
|
||||
stored_templates = json.load(temp_fp)
|
||||
assert len(stored_templates) == 1
|
||||
assert stored_templates[args[2]] is not None
|
||||
|
||||
template = stored_templates[args[2]]
|
||||
assert template['method'] == 'POST'
|
||||
assert template['url'] == 'https://catfact.ninja/fact'
|
||||
assert template['data'] is not None
|
||||
|
||||
def test_edit_template_not_found(self, capsys):
|
||||
"""
|
||||
Tests that an exception is raised when trying to edit a template that doesn't exist
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
httpie.cli.argtemplate.edit_json_template(['test_template', 'param', 'value'])
|
||||
out, _ = capsys.readouterr()
|
||||
assert "Template 'test_template' does not exist." in out
|
||||
|
||||
def test_edit_template_key_not_found(self):
|
||||
"""
|
||||
Tests that you can add new key-value pairs to an existing template
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
old_stored_templates = json.load(temp_fp)
|
||||
assert 'param2' not in old_stored_templates[args[2]]['data']
|
||||
|
||||
temp_fp.seek(0)
|
||||
httpie.cli.argtemplate.edit_json_template(['test_template', 'param2', 'value2'])
|
||||
new_stored_templates = json.load(temp_fp)
|
||||
assert new_stored_templates[args[2]]['data']['param2'] == 'value2'
|
||||
|
||||
def test_edit_template_no_templates_json(self):
|
||||
"""
|
||||
Tests that a new file will be created for editing templates if templates.json (TEMPLATE_FILE) doesn't exist
|
||||
"""
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = "NOT_A_REAL_FILE"
|
||||
assert not os.path.isfile(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
httpie.cli.argtemplate.edit_json_template(["fake_template", "param2", "value2"])
|
||||
assert os.path.isfile(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
os.remove(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
|
||||
class TestLoadTemplate:
|
||||
def test_load_template(self):
|
||||
"""
|
||||
Tests that loading a template yields the same arguments that were passed (except for 'http template <name>')
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
loaded_args = httpie.cli.argtemplate.load_template(args[2])
|
||||
|
||||
for i in range(len(args) - 3):
|
||||
assert args[i + 3] == loaded_args[i]
|
||||
|
||||
def test_load_template_not_found(self, capsys):
|
||||
"""
|
||||
Tests loading a template when the name of the template to load cannot be found in the template file
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
|
||||
loaded_args = httpie.cli.argtemplate.load_template(args[2])
|
||||
out, _ = capsys.readouterr()
|
||||
assert loaded_args == []
|
||||
assert "Template 'test_template' does not exist." in out
|
||||
|
||||
def test_load_template_no_templates_json(self):
|
||||
"""
|
||||
Tests that a new file will be created for loading templates if templates.json (TEMPLATE_FILE) doesn't exist
|
||||
"""
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = "NOT_A_REAL_FILE"
|
||||
assert not os.path.isfile(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
httpie.cli.argtemplate.load_template("fake_template")
|
||||
assert os.path.isfile(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
os.remove(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
|
||||
class TestDeleteTemplate:
|
||||
def test_delete_template(self):
|
||||
"""
|
||||
Tests that a template being deleted doesn't delete other templates
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
command2 = 'http template other_template GET https://catfact.ninja/fact param1=value1'
|
||||
args2 = command2.split()
|
||||
|
||||
httpie.cli.argtemplate.delete_template(args2[2])
|
||||
|
||||
fp = open(httpie.cli.argtemplate.TEMPLATE_FILE, "r")
|
||||
stored_templates = json.load(fp)
|
||||
assert "test_template" in stored_templates
|
||||
assert "other_template" not in stored_templates
|
||||
assert len(stored_templates) == 1
|
||||
assert stored_templates[args[2]]["method"] == "GET"
|
||||
assert stored_templates[args[2]]["url"] == "https://catfact.ninja/fact"
|
||||
assert stored_templates[args[2]]["data"]["param1"] == "value1"
|
||||
assert "param2" not in stored_templates[args[2]]["data"]
|
||||
|
||||
def test_delete_only_one_template(self):
|
||||
"""
|
||||
Tests that a template can be deleted when it's the only template in templates.json
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1'
|
||||
args = command.split()
|
||||
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
|
||||
httpie.cli.argtemplate.delete_template(args[2])
|
||||
|
||||
fp = open(httpie.cli.argtemplate.TEMPLATE_FILE, "r")
|
||||
stored_templates = json.load(fp)
|
||||
assert stored_templates == {}
|
||||
|
||||
def test_delete_template_no_templates_json(self):
|
||||
"""
|
||||
Tests that a new file will be created for deleting templates if templates.json (TEMPLATE_FILE) doesn't exist
|
||||
"""
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = "NOT_A_REAL_FILE"
|
||||
assert not os.path.isfile(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
httpie.cli.argtemplate.delete_template("fake_template")
|
||||
assert os.path.isfile(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
os.remove(httpie.cli.argtemplate.TEMPLATE_FILE)
|
||||
|
||||
def test_delete_template_doesnt_exist(self, capsys):
|
||||
"""
|
||||
Tests that an error message is printed when trying to delete a template that doesn't exist
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
command = 'http template test_template GET https://catfact.ninja/fact param1=value1 param2=value2'
|
||||
args = command.split()
|
||||
httpie.cli.argtemplate.store_json_template(args[2:])
|
||||
httpie.cli.argtemplate.delete_template("asdf")
|
||||
out, _ = capsys.readouterr()
|
||||
assert "Template 'asdf' does not exist." in out
|
@ -12,6 +12,9 @@ from httpie.context import Environment
|
||||
from httpie.encoding import UTF8
|
||||
from httpie.status import ExitStatus
|
||||
from .utils import HTTP_OK, MockEnvironment, StdinBytesIO, http
|
||||
import httpie.cli.argtemplate
|
||||
import tempfile
|
||||
import json
|
||||
|
||||
|
||||
def test_main_entry_point():
|
||||
@ -370,3 +373,57 @@ def test_options_dropping_redundant_content_length(httpbin, method):
|
||||
httpbin + '/anything'
|
||||
)
|
||||
assert 'Content-Length' not in r
|
||||
|
||||
|
||||
def test_store_template():
|
||||
"""
|
||||
Tests that a template can be stored with the command 'http template <name> <method> <url> ...'
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
http('template', 'test_template', 'GET', 'https://catfact.ninja/fact', '--traceback', '--timeout=3')
|
||||
|
||||
template = json.load(temp_fp)
|
||||
assert 'test_template' in template
|
||||
|
||||
|
||||
def test_run_template(httpbin_both):
|
||||
"""
|
||||
Tests that a template can be loaded with the command 'http runt <template>'
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
|
||||
r = http('template', 'test_template', 'GET', httpbin_both + '/get', '--debug') # store template
|
||||
|
||||
r = http('runt', 'test_template', '--traceback', '--timeout=3', tolerate_error_exit_status=True) # run it
|
||||
|
||||
assert HTTP_OK in r
|
||||
|
||||
|
||||
def test_delete_template():
|
||||
"""
|
||||
Tests that a template can be deleted with the command 'http delt <template>'
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
http('template', 'test_template', 'GET', 'https://catfact.ninja/fact', '--traceback', '--timeout=3') # store template
|
||||
|
||||
http('delt', 'test_template', '--traceback', '--timeout=3', tolerate_error_exit_status=True) # delete it
|
||||
|
||||
stored_templates = json.load(temp_fp)
|
||||
assert 'test_template' not in stored_templates
|
||||
|
||||
|
||||
def test_edit_template():
|
||||
"""
|
||||
Tests that a template can be edited with the command 'http editt <template>'
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_fp:
|
||||
httpie.cli.argtemplate.TEMPLATE_FILE = temp_fp.name
|
||||
http('template', 'test_template', 'GET', 'https://catfact.ninja/fact', '--traceback', '--timeout=3') # store template
|
||||
|
||||
http('editt', 'test_template', 'param', 'value', '--debug') # edit it
|
||||
stored_templates = json.load(temp_fp)
|
||||
assert 'param' in stored_templates['test_template']['data']
|
||||
|
Loading…
Reference in New Issue
Block a user