From 734312ed70514e27ce7a69f248045ebb32c1b1c0 Mon Sep 17 00:00:00 2001 From: Kyle Bebak Date: Wed, 31 Oct 2018 16:05:37 -0600 Subject: [PATCH] Single requests that don't start at the first character of a line can be parsed correctly --- CONTRIBUTING.md | 2 +- core/parsers.py | 13 +++++++++---- docs/_content/body.md | 4 ++-- messages/2.28.0.txt | 10 ++++++++++ tests/requester.py | 2 ++ tests/test_integration.py | 13 +++++++++++-- 6 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 messages/2.28.0.txt diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e9f079c..b135553 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -47,7 +47,7 @@ To run `core` tests, execute `python3 -m unittest tests.core -v` from the root o To run the integration tests, install __UnitTesting__ via Package Control. Read more about [UnitTesting](https://github.com/randy3k/UnitTesting-example). Also, make sure you've cloned the Requester repo into your __Packages__ directory. -Run integration tests before committing changes. Look for __UnitTesting__ in the command palette, hit Enter, and type `Requester`. Or, if you've created a project for Requester, run __UnitTesting: Test Current Project__. +Run integration tests before committing changes. Look for __UnitTesting__ in the command palette, hit Enter, and type `Requester`. Or, if you've created a project for Requester, run __UnitTesting: Test Current Package__. ### Docs diff --git a/core/parsers.py b/core/parsers.py index 5d21e43..7af841a 100644 --- a/core/parsers.py +++ b/core/parsers.py @@ -39,9 +39,7 @@ def parse_tests(s): break else: if s.type == 'request' and next_s.type == 'assertion': - tests.append(RequestAssertion( - s.selection.selection, next_s.selection.selection - )) + tests.append(RequestAssertion(s.selection.selection, next_s.selection.selection)) return tests @@ -63,13 +61,20 @@ def parse(s, open_bracket, close_bracket, match_patterns, n=None, es=None): if n and len(start_indices) >= n: break for pattern in match_patterns: + # for multiline requests, match must occur at start of line, + # to diminish risk of matching something like 'get(' in the middle of a string if re.match(pattern, line): start_indices.append(index) break index += len(line) if not start_indices and len(lines) == 1: # shorthand syntax for basic, one-line GET requests - return [Selection("get('{}')".format(prepend_scheme(s.strip().strip('"').strip("'"))), 0)] + # if selection has only one line, we can safely search for start index of first match, + # even if it's not at start of line + match = re.search(pattern, line) + if not match: + return [Selection("get('{}')".format(prepend_scheme(s.strip().strip('"').strip("'"))), 0)] + start_indices.append(match.start()) if es: # replace selection with extended selection AFTER `start_indices` have been found s = es diff --git a/docs/_content/body.md b/docs/_content/body.md index c248ec7..dbc9e43 100644 --- a/docs/_content/body.md +++ b/docs/_content/body.md @@ -67,8 +67,8 @@ get('http://headers.jsontest.com/', fmt='raw') Try sending the following requests. This is obviously not valid Python syntax, but Requester has a shortuct for basic GET requests. If you run Requester on a URL like the one below, it automatically wraps it like so: `requests.get('')`. And it doesn't wrap the URL in quotes if it's already got them. ~~~py -httpbin.com/get -'httpbin.com/headers' +httpbin.org/get +'httpbin.org/headers' ~~~ diff --git a/messages/2.28.0.txt b/messages/2.28.0.txt new file mode 100644 index 0000000..9678709 --- /dev/null +++ b/messages/2.28.0.txt @@ -0,0 +1,10 @@ +# New Features + +Single requests that don't start at the first character of a line can be parsed correctly. This allows you, for example, to execute requests that have been commented out, or requests that are in doc strings, or requests in a markdown list. + +Place your cursor on the first line of either of these requests and attempt to execute them. + +* get('google.com') +- get('httpbin.org/get', + params={'a': 'b'} +) diff --git a/tests/requester.py b/tests/requester.py index dead0ec..993a2a1 100644 --- a/tests/requester.py +++ b/tests/requester.py @@ -12,3 +12,5 @@ requests.get( '127.0.0.1:8000/get' ) + +# requests.get('127.0.0.1:8000/get') diff --git a/tests/test_integration.py b/tests/test_integration.py index d9fc8b2..a2dd650 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -59,7 +59,7 @@ def get_line(view, line): class TestRequesterMixin: - WAIT_MS = 500 # wait in ms for responses to return + WAIT_MS = 750 # wait in ms for responses to return def setUp(self): self.config = sublime.load_settings('Requester.sublime-settings') @@ -131,6 +131,15 @@ def test_single_request_on_multiple_lines(self): self._test_url_in_view(self.window.active_view(), 'http://127.0.0.1:8000/get') self._test_name_in_view(self.window.active_view(), 'GET: /get') + def test_single_request_commented_out(self): + """Commented out request on one line. + """ + select_line_beginnings(self.view, 16) + self.view.run_command('requester') + yield self.WAIT_MS + self._test_url_in_view(self.window.active_view(), 'http://127.0.0.1:8000/get') + self._test_name_in_view(self.window.active_view(), 'GET: /get') + def test_single_request_with_env_block(self): """From env block. """ @@ -175,7 +184,7 @@ def test_single_request_focus_change(self): class TestRequesterMultiple(TestRequesterMixin, DeferrableTestCase): REQUESTER_FILE = 'Packages/Requester/tests/requester.py' - WAIT_MS = 500 + WAIT_MS = 750 def test_multiple_requests(self): """Tests the following: