Skip to content

Commit

Permalink
Single requests that don't start at the first character of a line can…
Browse files Browse the repository at this point in the history
… be parsed correctly
  • Loading branch information
kylebebak committed Oct 31, 2018
1 parent 57ac54c commit 734312e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions core/parsers.py
Expand Up @@ -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


Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/_content/body.md
Expand Up @@ -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('<url>')`. 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'
~~~


Expand Down
10 changes: 10 additions & 0 deletions 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'}
)
2 changes: 2 additions & 0 deletions tests/requester.py
Expand Up @@ -12,3 +12,5 @@
requests.get(
'127.0.0.1:8000/get'
)

# requests.get('127.0.0.1:8000/get')
13 changes: 11 additions & 2 deletions tests/test_integration.py
Expand Up @@ -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')
Expand Down Expand Up @@ -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.
"""
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 734312e

Please sign in to comment.