13

I'm using paste to do some functional testing on my 'controllers' in my web.py app. In one case I'm trying to test for a 400 response when a malformed post is made to an API endpoint. Here is what my test looks like:

def test_api_users_index_post_malformed(self):
    r = self.testApp.post('/api/users', params={})
    assert r.header('Content-Type') == 'application/json'
    assert r.status == 400

But I'm getting the following exception:

AppError: Bad response: 400 Bad Request (not 200 OK or 3xx redirect for /api/users)

I see paste has HttpException middleware, but I can't find any examples on how to use it or if its even the right way to go. Any suggestions? Or am I just going about this wrong?

Matt W
  • 6,078
  • 3
  • 32
  • 40
  • It looks like you need to catch the error. Try making the test a `unittest.TestCase`, and using its `assertRaises` method: http://docs.python.org/library/unittest.html#unittest.TestCase.assertRaises – Thomas K Aug 15 '11 at 22:59
  • I don't think that will help me test my response. The exception is raised during the self.testApp.post(...) call, so I can't check my status code – Matt W Aug 15 '11 at 23:25

1 Answers1

33

I know I'm tardy to the party, but I ran across this searching for the answer to the same issue. To allow the TestApp to pass non 2xx/3xx responses back you need to tell the request to allow "errors".

def test_api_users_index_post_malformed(self):
    r = self.testApp.post('/api/users', params={}, expect_errors=True)
    assert r.header('Content-Type') == 'application/json'
    assert r.status == 400

Happy Hacking!

jkoelker
  • 940
  • 1
  • 14
  • 14