diff --git a/dotextensions/server/handlers/basic_handlers.py b/dotextensions/server/handlers/basic_handlers.py index 36472e6..b69b07e 100644 --- a/dotextensions/server/handlers/basic_handlers.py +++ b/dotextensions/server/handlers/basic_handlers.py @@ -19,7 +19,8 @@ RequestCompiler, dothttp_model, ) -from ..models import BaseHandler, Command, Result +from ..models import BaseHandler, Command, DothttpTypes, Result +from .gohandler import TypeFromPos from . import logger @@ -299,6 +300,60 @@ def run(self, command: Command) -> Result: result = Result(id=command.id, result=command.params) return result +class ResolveBase(): + + def get_resolved(self, command: Command) -> Result: + pos = command.params.get('position') + config = self.get_config(command) + comp: RequestCompiler = self.get_request_comp(config) + comp.load_def() + type_dict = TypeFromPos.figure_n_get(comp.model, pos) + type_type = type_dict['type'] + if type_type == DothttpTypes.URL.value: + type_dict["resolved"] = comp.httpdef.url + elif type_type == DothttpTypes.NAME.value: + type_dict["resolved"] = comp.httpdef.name + # header + # query is pending + elif type_type == DothttpTypes.VARIABLE.value: + variable_name = type_dict["name"] + value = comp.property_util.resolve_property_string(variable_name) + type_dict["resolved"] = value + # elif type_type == DothttpTypes.HEADER: + # return Result(id=command.id, result={"resolved": comp.httpdef.headerwrap.header}) + elif type_type == DothttpTypes.PAYLOAD_DATA.value: + type_dict["resolved"] = comp.httpdef.payload.data + elif type_type == DothttpTypes.PAYLOAD_ENCODED.value: + type_dict["resolved"] = comp.httpdef.payload.data + elif type_type == DothttpTypes.PAYLOAD_JSON.value: + type_dict["resolved"] = comp.httpdef.payload.json + elif type_type == DothttpTypes.PAYLOAD_MULTIPART.value: + type_dict["resolved"] = comp.httpdef.payload.files + elif type_type == DothttpTypes.PAYLOAD_FILE.value: + type_dict["resolved"] = comp.httpdef.payload.filename + else: + type_dict["resolved"] = "" + return Result(id=command.id, result=type_dict) + +# return resolved string instead of model object +class GetHoveredResolvedParamFileHandler(RunHttpFileHandler, ResolveBase): + method = "/file/resolve" + + def get_method(self): + return GetHoveredResolvedParamFileHandler.method + + def run(self, command): + return self.get_resolved(command) + + +class GetHoveredResolvedParamContentHandler(ContentExecuteHandler, ResolveBase): + method = "/content/resolve" + + def get_method(self): + return GetHoveredResolvedParamContentHandler.method + + def run(self, command): + return self.get_resolved(command) class GetNameReferencesHandler(BaseHandler): name = "/file/names" diff --git a/dotextensions/server/handlers/gohandler.py b/dotextensions/server/handlers/gohandler.py index c295558..8a614ba 100644 --- a/dotextensions/server/handlers/gohandler.py +++ b/dotextensions/server/handlers/gohandler.py @@ -61,7 +61,9 @@ def run(self, command: Command) -> Result: result={"error_message": f"unknown Exception {e}", "error": True}, ) - def figure_n_get(self, model: MultidefHttp, position: int) -> dict: + @staticmethod + def figure_n_get(model: MultidefHttp, position: int) -> dict: + self = TypeFromPos if self.is_in_between(model, position): index = 0 if model.import_list: diff --git a/pyproject.toml b/pyproject.toml index fcb1fb8..d465c3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "dothttp-req" -version = "0.0.44a3" +version = "0.0.44a4" description = "Dothttp is Simple http client for testing and development" authors = ["Prasanth "] license = "MIT" diff --git a/test/conftest.py b/test/conftest.py index 0ede717..6a38241 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -9,13 +9,13 @@ def start_httpbin_container(): container_id = subprocess.check_output( ["docker", "run", "-d", "-p", "8000:80", "kennethreitz/httpbin"] ).decode().strip() - except: - pass - # Wait for the container to be ready - time.sleep(5) # Adjust the sleep time as needed + # Wait for the container to be ready + time.sleep(5) # Adjust the sleep time as needed - yield + yield - # Teardown: Stop and remove the Docker container - subprocess.run(["docker", "stop", container_id]) - subprocess.run(["docker", "rm", container_id]) \ No newline at end of file + # Teardown: Stop and remove the Docker container + subprocess.run(["docker", "stop", container_id]) + subprocess.run(["docker", "rm", container_id]) + except: + pass \ No newline at end of file diff --git a/test/extensions/test_commands.py b/test/extensions/test_commands.py index d42bab6..2f07f61 100644 --- a/test/extensions/test_commands.py +++ b/test/extensions/test_commands.py @@ -7,6 +7,8 @@ from dotextensions.server.handlers.basic_handlers import ( ContentNameReferencesHandler, + GetHoveredResolvedParamContentHandler, + GetHoveredResolvedParamFileHandler, GetNameReferencesHandler, RunHttpFileHandler, ) @@ -39,7 +41,8 @@ def execute_and_compare_names(self, result): {"end": 77, "method": "GET", "name": "test", "start": 0}, {"end": 207, "method": "POST", "name": "test2", "start": 79}, {"end": 337, "method": "POST", "name": "test3", "start": 209}, - {"end": 398, "method": "POST", "name": "test4.test", "start": 339}, + {"end": 398, "method": "POST", + "name": "test4.test", "start": 339}, ], "urls": [ { @@ -105,6 +108,114 @@ def test_syntax_error_name(self): self.assertTrue(result.result["error"]) +class ResovleTest(TestBase): + def test_hover_url(self): + resolve_handler = GetHoveredResolvedParamFileHandler() + result = resolve_handler.run( + Command( + method=GetHoveredResolvedParamFileHandler.name, + params={"file": f"{command_dir}/simple.http", "position": 7}, + id=1, + ) + ) + self.assertEquals( + result.result["resolved"], "http://localhost:8000/get") + + def test_hover_url_content(self): + resolve_handler = GetHoveredResolvedParamContentHandler() + with open(f"{command_dir}/simple.http") as f: + result = resolve_handler.run( + Command( + method=resolve_handler.name, + params={"content": f.read(), "position": 7}, + id=1, + ) + ) + self.assertEquals( + result.result["resolved"], "http://localhost:8000/get") + + def test_hover_json_content(self): + resolve_handler = GetHoveredResolvedParamContentHandler() + content = """ +var numOfHours = 3; +var numOfSeconds = (numOfHours * 60 * 60); +POST "http://localhost:8000/post" +json({ + "totalSeconds" : {{numOfSeconds}} +}) +""" + index = content.find("totalSeconds") + result = resolve_handler.run( + Command( + method=resolve_handler.name, + params={"content": content, "position": index}, + id=1, + ) + ) + expected = {'totalSeconds': 10800} + self.assertEquals(expected, result.result["resolved"]) + + index2 = content.find("numOfHours * 60 * 60") + result2 = resolve_handler.run( + Command( + method=resolve_handler.name, + params={"content": content, "position": index2}, + id=2, + ) + ) + self.assertEquals(10800, result2.result["resolved"]) + + + def test_hover_import_content(self): + resolve_handler = GetHoveredResolvedParamContentHandler() + names_http_file = f"{command_dir}/names.http" + content = f'import "{names_http_file}";' + """ + var numOfHours = 3; + var numOfSeconds = (numOfHours * 60 * 60); + @name("my-test"): "test3" + POST "/ram" + json({ + "totalSeconds" : {{numOfSeconds}} + }) + """ + index = content.find("ram") + result = resolve_handler.run( + Command( + method=resolve_handler.name, + params={"content": content, "position": index}, + id=1, + ) + ) + expected = "https://req.dothttp.dev/ram" + self.assertEquals(expected, result.result["resolved"]) + + + def test_hover_context_content(self): + resolve_handler = GetHoveredResolvedParamContentHandler() + contexts = ["""@name('test3') +GET "https://httpbin.org/get" +"""] + content = """ + var numOfHours = 3; + var numOfSeconds = (numOfHours * 60 * 60); + @name("my-test"): "test3" + POST "/ram" + json({ + "totalSeconds" : {{numOfSeconds}} + }) + """ + index = content.find("ram") + result = resolve_handler.run( + Command( + method=resolve_handler.name, + params={"content": content, "position": index, "contexts": contexts}, + id=1, + ) + ) + expected = "https://httpbin.org/get/ram" + self.assertEquals(expected, result.result["resolved"]) + + class FileExecute(TestBase): def setUp(self) -> None: self.execute_handler = RunHttpFileHandler() @@ -135,7 +246,8 @@ def test_complex_file(self): self.assertTrue("status" in result.result) self.assertEqual(200, result.result["status"]) self.assertTrue("headers" in result.result) - self.assertEqual("http://localhost:8000/post?startusing=dothttp", body["url"]) + self.assertEqual( + "http://localhost:8000/post?startusing=dothttp", body["url"]) self.assertEqual( """var host = 'localhost:8000' ; @name("2") @@ -162,7 +274,8 @@ def test_complex_file(self): self.assertTrue("status" in result2.result) self.assertEqual(200, result2.result["status"]) self.assertTrue("headers" in result2.result) - self.assertEqual("http://localhost:8000/post?startusing=dothttp", body["url"]) + self.assertEqual( + "http://localhost:8000/post?startusing=dothttp", body["url"]) self.assertEqual( """var host = 'req.dothttp.dev' ; @name("2") @@ -256,11 +369,13 @@ def test_property_not_found(self): self.assertEqual(True, result.result["error"]) def test_env(self): - result = self.execute_file(f"{command_dir}/isolated/env.http", env=["simple"]) + result = self.execute_file( + f"{command_dir}/isolated/env.http", env=["simple"]) self.assertEqual(200, result.result["status"]) def test_execute(self): - result = self.execute_file(f"{command_dir}/cookie.http", target="set-cookie") + result = self.execute_file( + f"{command_dir}/cookie.http", target="set-cookie") self.assertEqual( """@name("set-cookie") GET "http://localhost:8000/cookies/set/dev/ram" @@ -392,7 +507,6 @@ def test_property_file(self): self.assertEqual({"prop": "value"}, body["json"]) print(result) - def test_property_sub(self): http_file = f"{command_dir}/custom-property/property-refer.http" result = self.execute_file( @@ -429,6 +543,5 @@ def test_property_sub_from_command_line_props(self): self.assertEqual({'fullName': 'Mr. John Doe'}, body["json"]) - if __name__ == "__main__": unittest.main()