Getting always a 404 with simulate request with TestCase class

147 views
Skip to first unread message

Sven Nebel

unread,
Oct 14, 2016, 1:51:55 PM10/14/16
to Falcon Framework
Hi,
Found this simple testing example

Then I've tried to adapt it to the new TestCase class provided for 1.0.0

but always getting a 404, what I'm doing wrong? was unable to find examples for the new class, I'd really like to use the new class as this provides better functionality

import falcon
import falcon.testing as testing


QUOTE
= (u"\nI've always been more interested in\n"
         u
'the future than in the past.\n'
         u
'\n'
         u
'    ~ Grace Hopper\n\n')


class ThingsResource(object):
   
def on_get(self, req, resp):
       
"""Handles GET requests"""
        resp
.body = QUOTE


class TestThings(testing.TestCase):
   
def before(self):
        things
= ThingsResource()
       
# TestBase provides an instance of falcon.API to use along
       
# with simulate_request (see below).
       
self.api.add_route('/things', things)


   
def test_grace(self):
       
# TestBase provides a method to simulate a WSGI request without
       
# having to stand up an actual server. The decode option tells
       
# simulate_request to convert the raw WSGI response into a
       
# Unicode string.
        body
= self.simulate_request('GET', '/things')


       
# TestBase provides an instance of StartResponseMock that captures
       
# the data passed to WSGI's start_response callback. This includes
       
# the status code and headers returned by the Falcon app.
       
self.assertEqual(body.status, falcon.HTTP_200)
       
self.assertEqual(body, QUOTE)


Thanks!

Sven Nebel

unread,
Oct 16, 2016, 3:39:48 PM10/16/16
to Falcon Framework
Playing with it found that before method is not being executed as with the deprecated Test.Base class, tried to use setUp() but seems that api is not available to add routes, not sure how should I add the route...

Traceback (most recent call last):
 
File "/home/snebel29/ask.com/openshift_deployment_gate/test_not_working.py", line 20, in setUp
   
self.api.add_route('/things', things)
AttributeError: 'TestThings' object has no attribute 'api'

And the code

import falcon
import falcon.testing as testing


QUOTE = (u"\nI've always been more interested in\n"
        u'the future than in the past.\n'
        u'\n'
        u'    ~ Grace Hopper\n\n')


class ThingsResource(object):
   def on_get(self, req, resp):
       """Handles GET requests"""
       resp.body = QUOTE


class TestThings(testing.TestCase):
   def setUp(self):
       super().__init__()
       things = ThingsResource()
       # TestBase provides an instance of falcon.API to use along
       # with simulate_request (see below).
       self.api.add_route('/things', things)
       print('A:' + dir(self.api))


    def test_grace(self):
       # TestBase provides a method to simulate a WSGI request without
       # having to stand up an actual server. The decode option tells
       # simulate_request to convert the raw WSGI response into a
       # Unicode string.
       body = self.simulate_request('GET', '/things')


        # TestBase provides an instance of StartResponseMock that captures
       # the data passed to WSGI's start_response callback. This includes
       # the status code and headers returned by the Falcon app.
       self.assertEqual(body.status, falcon.HTTP_200)
       self.assertEqual(body, QUOTE)

Sven Nebel

unread,
Oct 16, 2016, 3:47:14 PM10/16/16
to Falcon Framework
Finally found the solution on my own, posting here :-)

import falcon
import falcon.testing as testing


QUOTE
= (u"\nI've always been more interested in\n"
         u
'the future than in the past.\n'
         u
'\n'
         u
'    ~ Grace Hopper\n\n')


class ThingsResource(object):
   
def on_get(self, req, resp):
       
"""Handles GET requests"""
        resp
.body = QUOTE


class TestThings(testing.TestCase):

   
def setUp(self):
       
super().__init__()
        things
= ThingsResource()

       
self.api = falcon.API(media_type='application/json; charset=utf-8')

       
self.api.add_route('/things', things)


   
def test_grace(self):

        resp
= self.simulate_request('GET', '/things')
       
self.assertEqual(resp.status, falcon.HTTP_200)
       
self.assertEqual(resp.text, QUOTE)



Kurt G. | @kgriffs

unread,
Oct 16, 2016, 9:16:13 PM10/16/16
to Falcon Framework
Hi Sven, thanks for sharing your solution. The latest documentation (will be released as stable in a week or two) now includes an example along these lines that may also be helpful. That being said, we have an open issue for continuing to improve the testing docs. If anyone is interested in contributing in this area, please comment on the issue. Thanks!

@kgriffs

Paul G

unread,
Jan 23, 2017, 1:27:36 PM1/23/17
to Falcon Framework
I have the same problem, I get 404 from simulate_get.

My test code looks like:

from dp_internal_api.users.falcon_demo import app as falcon_demo_app


class FalconUserViewSetTestCase(falcon_testing.TestCase):


   
@classmethod
   
def setUpClass(cls):
        cls
.users = UserFactory.create_batch(5)
        cls
.app = falcon_demo_app


   
def test_get_detail_public_unauthenticated(self):
        response
= self.simulate_get('/internal/v1/users/%s/' % self.users[2].id)

       
self.assertEqual(response.status_code, 200)


I am using Werkzeug to route to Falcon alongside our main Django app

...but I think this shouldn't matter in the test since I'm attaching the Falcon wsgi app directly to the test class.

My question is really how to debug the Falcon app routing. I couldn't see obviously how to inspect the generated routes for example. I guess that could lead me in the right direction.

Paul G

unread,
Jan 23, 2017, 1:36:19 PM1/23/17
to Falcon Framework
Well I found the answer already...

Need to attach the app in setUp method NOT setUpClass  (I guess falcon TestCase overwrites it with an empty app in setUp if you do that)
Reply all
Reply to author
Forward
0 new messages