Skip to content

Commit

Permalink
Support resolve url or json or variable
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric05 committed Dec 31, 2024
1 parent 764d1cf commit 5caf6e9
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 18 deletions.
57 changes: 56 additions & 1 deletion dotextensions/server/handlers/basic_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion dotextensions/server/handlers/gohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down
16 changes: 8 additions & 8 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
# Teardown: Stop and remove the Docker container
subprocess.run(["docker", "stop", container_id])
subprocess.run(["docker", "rm", container_id])
except:
pass
127 changes: 120 additions & 7 deletions test/extensions/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from dotextensions.server.handlers.basic_handlers import (
ContentNameReferencesHandler,
GetHoveredResolvedParamContentHandler,
GetHoveredResolvedParamFileHandler,
GetNameReferencesHandler,
RunHttpFileHandler,
)
Expand Down Expand Up @@ -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": [
{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()

0 comments on commit 5caf6e9

Please sign in to comment.