Skip to content

Commit

Permalink
no escape quoted string for data payload in httpdef #54
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric05 committed Apr 12, 2021
1 parent d42df65 commit 7c2a23c
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 9 deletions.
7 changes: 2 additions & 5 deletions dothttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,8 @@ def _load_payload(self):
# can be short circuted
if not self.http.payload:
return Payload()
elif data := self.http.payload.data:
if getattr(data, "act"):
# quoted string allow
# check MULTILINE_STRING in http.tx
data = data.act
elif self.http.payload.data or self.http.payload.multi:
data = self.http.payload.data or self.http.payload.multi[3:-3]
content = self.get_updated_content(data)
mimetype = self.get_mimetype_from_buffer(content,
self.get_updated_content(self.http.payload.type))
Expand Down
6 changes: 4 additions & 2 deletions dothttp/http.tx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ QUERY:
;

PAYLOAD:
( 'data' '(' data=MULTILINE_STRING (',' type=STRING)? ','? ')'
( 'data' '(' data=STRING (',' type=STRING)? ','? ')'
| 'data' '(' multi=MULTILINE_STRING (',' type=STRING)? ','? ')'
| 'data' '(' datajson=JSON (',' type=STRING)? ','? ')'
| 'urlencoded' '(' datajson=JSON (',' type=STRING)? ','? ')'
| 'fileinput' '(' file=STRING (',' type=STRING)? ','? ')'
Expand Down Expand Up @@ -124,7 +125,8 @@ DotString:
// allow only alphanumeric or string in double/single quotes

MULTILINE_STRING:
"'''" act=/.*/ "'''" | '"""' act=/.*/ '"""'
/"""[\S\s]*"""/ | /'''[\S\s]*'''/

;

URLString:
Expand Down
1 change: 1 addition & 0 deletions dothttp/parse_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Payload:
json: Optional
fileswrap: Optional[FilesWrap]
type: Optional[str]
multi: Optional[str] = None


@dataclass
Expand Down
6 changes: 4 additions & 2 deletions dothttp/request_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from requests.status_codes import _codes as status_code
from textx import metamodel_from_file

from dothttp.parse_models import Allhttp
from . import eprint, Config, HttpDefBase
from .curl_utils import to_curl
from .dsl_jsonparser import json_or_array_to_json
Expand Down Expand Up @@ -158,7 +159,7 @@ def load(self):
self.prop_cache = {}

@staticmethod
def format(model):
def format(model: Allhttp):
output_str = ""
for http in model.allhttps:
new_line = "\n"
Expand All @@ -182,7 +183,8 @@ def format(model):
if payload := http.payload:
p = ""
mime_type = payload.type
if data := payload.data:
if payload.data or payload.multi:
data = payload.data or payload.multi[3:-3]
if '"' in data and "'" not in data:
data = f"'{data}'"
elif '"' not in data and "'" in data:
Expand Down
7 changes: 7 additions & 0 deletions test/core/payload/quoted.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
POST https://httpbin.org/post
data(
'''
"'this can have quotes with escape sequence'"

'''
)
7 changes: 7 additions & 0 deletions test/core/payload/quoted2.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
POST https://httpbin.org/post
data(
"""
"'this can have quotes with escape sequence'"

"""
)
8 changes: 8 additions & 0 deletions test/core/substitution/quoted.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POST "https://{{host1}}/ram"
? '{{queryname1}}' , 'value1'
? 'key2' , '{{valuename1}}'
data("""
"hi this is good"
'this barbarian'
""")
output('test')
14 changes: 14 additions & 0 deletions test/core/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,17 @@ def test_multipart_payload(self):

loadfile.close()
os.unlink(loadfile.name)

def test_data_json_payload(self):
req = self.get_request(f"{base_dir}/quoted.http")
self.assertEqual("""
"'this can have quotes with escape sequence'"
""", req.body)

def test_data_json_payload(self):
req = self.get_request(f"{base_dir}/quoted2.http")
self.assertEqual("""
"'this can have quotes with escape sequence'"
""", req.body)
16 changes: 16 additions & 0 deletions test/core/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ def test_format2_print(self):
output(test)
""", output)

def test_quoted3_format_print(self):
req = self.get_req_comp(f"{sub_dir}/quoted.http", format=True, stdout=True, target=2)
req.load()
output = req.format(req.model)
self.assertEqual("""POST "https://{{host1}}/ram"
? "{{queryname1}}"= "value1"
? "key2"= "{{valuename1}}"
data('
"hi this is good"
\\'this barbarian\\'
')
output(test)
""", output)

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
Expand Down

0 comments on commit 7c2a23c

Please sign in to comment.