Skip to content

Commit dc850a9

Browse files
committed
handle quoted extract argument, ref #4
1 parent 7e19e1d commit dc850a9

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

restmagic/magic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
ResponseParser,
2626
expand_variables,
2727
parse_rest_request,
28+
remove_argument_quotes,
2829
)
2930
from restmagic.request import RESTRequest
3031
from restmagic.sender import RequestSender
@@ -220,7 +221,7 @@ def rest(self, line, cell=''):
220221
if args.parser_expression:
221222
display_dict(
222223
ResponseParser(response=response,
223-
expression=args.parser_expression,
224+
expression=remove_argument_quotes(args.parser_expression),
224225
content_subtype=args.parser).parse()
225226
)
226227
else:

restmagic/parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class ParseError(Exception):
1515
"""Query parsing error occured."""
1616

1717

18+
def remove_argument_quotes(text: str) -> str:
19+
"""Remove single and double outer quotes from text.
20+
"""
21+
if re.match(r'^(\'.*\')|(".*")$', text or ''):
22+
return text[1:-1]
23+
return text
24+
25+
1826
def expand_variables(text, kwargs):
1927
"""Expand python variables in a string.
2028

tests/test_magic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,29 @@ def test_timeout_option_handled(send):
279279
assert send.call_args[1]['timeout'] == 1.01
280280

281281

282+
@pytest.mark.parametrize('option', ('-[', '--extract'))
283+
def test_extract_option_handled(send, display_dict, display_response,
284+
response_parser, option):
285+
RESTMagic().rest(line=f"{option} test GET http://localhost")
286+
response_parser.assert_called_once_with(response='test sended', expression='test',
287+
content_subtype=None)
288+
display_dict.assert_called_once()
289+
290+
291+
@pytest.mark.parametrize(
292+
'line, expected_expression', (
293+
('-[ "a b"', 'a b'),
294+
('--extract \'/[name() = "svg"]\'', '/[name() = "svg"]'),
295+
)
296+
)
297+
def test_extract_option_quoted_expression_handled(send, display_dict, display_response,
298+
response_parser, line, expected_expression):
299+
RESTMagic().rest(line=f"{line} test GET http://localhost")
300+
response_parser.assert_called_once_with(response='test sended', expression=expected_expression,
301+
content_subtype=None)
302+
display_dict.assert_called_once()
303+
304+
282305
@pytest.mark.parametrize('parser', ('json', 'xml', 'html'))
283306
def test_parser_option_handled(send, display_dict, display_response,
284307
response_parser, parser):

tests/test_parser.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
parse_rest_request,
1313
guess_response_content_subtype,
1414
parse_json_response,
15+
remove_argument_quotes,
1516
)
1617

1718

@@ -223,3 +224,17 @@ def test_guess_response_content_subtype(response, expected):
223224

224225
def test_response_parser(json_response):
225226
ResponseParser(response=json_response, expression=None)
227+
228+
229+
@pytest.mark.parametrize(
230+
'text, expected', (
231+
('', ''),
232+
('test', 'test'),
233+
('"test test"', 'test test'),
234+
("'test test'", 'test test'),
235+
("'test' 'test' \"test\"", "test 'test' \"test\""),
236+
("test 'test'", "test 'test'"),
237+
)
238+
)
239+
def test_quotes_removed_from_argument(text, expected):
240+
remove_argument_quotes(text) == expected

0 commit comments

Comments
 (0)