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

Issue 23 #33

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion httpie/cli/argtemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ def edit_json_template(args):
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
Expand All @@ -114,6 +119,7 @@ def delete_template(arg):
json.dump(stored_templates, f)
f.truncate()


def load_template(arg):
# Check if the templates.json file exists
if not os.path.isfile(TEMPLATE_FILE):
Expand All @@ -136,5 +142,5 @@ def load_template(arg):
args.append(args_dict.pop('url'))
data_dict = args_dict.pop('data')
for key, value in data_dict.items():
args.append(key+"="+value)
args.append(key + "=" + value)
return args
11 changes: 4 additions & 7 deletions httpie/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ def raw_main(
if (args[0] == "template"):
# http template <name> <method> <url> |<REQUEST_ITEM>|
if len(args) < 2:
print("No template name was specified")
return ExitStatus.ERROR
if len(args) < 4:
print("Template needs at least a METHOD and a URL")
return ExitStatus.ERROR
from httpie.cli.argtemplate import store_json_template
store_json_template(args[1:])
Expand All @@ -67,7 +65,7 @@ def raw_main(
print("No template name was specified")
return ExitStatus.ERROR
if len(args) > 2:
print("Too many arguments, http <template name> expected.")
print("Too many arguments, http runt <template name> expected.")
return ExitStatus.ERROR
from httpie.cli.argtemplate import store_json_template, load_template
args = load_template(args[1])
Expand All @@ -82,14 +80,13 @@ def raw_main(
from httpie.cli.argtemplate import edit_json_template
edit_json_template(args[1:])
return ExitStatus.SUCCESS

# http delt <name>
if (args[0] == "delt"):
elif (args[0] == "delt"):
# http delt <name>
if len(args) < 2:
print("No template name was specified")
return ExitStatus.ERROR
if len(args) > 2:
print("Too many arguments, http <template name> expected.")
print("Too many arguments, http delt <template name> expected.")
return ExitStatus.ERROR
from httpie.cli.argtemplate import delete_template
args = delete_template(args[1])
Expand Down
79 changes: 77 additions & 2 deletions tests/test_argtemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import os


class TestStoreTemplate:

def test_store_normal_template_with_method(self):
Expand Down Expand Up @@ -97,6 +98,7 @@ def test_store_template_no_templates_json(self):

os.remove(httpie.cli.argtemplate.TEMPLATE_FILE)


class TestEditTemplate:
def test_edit_template_update_value(self):
"""
Expand Down Expand Up @@ -194,7 +196,7 @@ def test_edit_template_key_not_found(self):
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)
Expand All @@ -212,6 +214,7 @@ def test_edit_template_no_templates_json(self):

os.remove(httpie.cli.argtemplate.TEMPLATE_FILE)


class TestLoadTemplate:
def test_load_template(self):
"""
Expand Down Expand Up @@ -243,7 +246,7 @@ def test_load_template_not_found(self, capsys):
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
Expand All @@ -255,3 +258,75 @@ def test_load_template_no_templates_json(self):
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