Skip to content

Commit

Permalink
Added unit test for issue 810.
Browse files Browse the repository at this point in the history
The problem seems to be that code called by post_internal() relies on the
Flask request object, even though the current request may not be a POST
to the given resource.
  • Loading branch information
sybrenstuvel authored and nicolaiarocci committed May 24, 2017
1 parent bc753b0 commit 12ddf78
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions eve/methods/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,8 @@ def resource_link():
"""
path = request.path.strip('/')

assert request.routing_exception is None, 'Routing error for %s: %s' % (request.url, request.routing_exception)

if '|item' in request.endpoint:
path = path[:path.rfind('/')]

Expand Down
21 changes: 21 additions & 0 deletions eve/tests/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,27 @@ def test_api_prefix(self):
r = self.test_prefix.get('/prefix/contacts/')
self.assert200(r.status_code)

r = self.test_prefix.post('/prefix/contacts/', data='{}',
content_type='application/json')
self.assert201(r.status_code)

def test_api_prefix_post_internal(self):
from eve.methods.post import post_internal

settings_file = os.path.join(self.this_directory, 'test_prefix.py')
self.app = Eve(settings=settings_file)
self.test_prefix = self.app.test_client()

# This works fine
with self.app.test_request_context(method='POST', path='/prefix/contacts'):
r, _, _, status_code = post_internal('contacts', {})
self.assert201(status_code)

# This fails
with self.app.test_request_context():
r, _, _, status_code = post_internal('contacts', {})
self.assert201(status_code)

def test_api_prefix_version(self):
settings_file = os.path.join(self.this_directory,
'test_prefix_version.py')
Expand Down
1 change: 1 addition & 0 deletions eve/tests/test_prefix.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-

RESOURCE_METHODS = ['GET', 'POST']
URL_PREFIX = 'prefix'
DOMAIN = {'contacts': {}}

0 comments on commit 12ddf78

Please sign in to comment.