Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing for 400 errors with paste on a web.py app

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?

like image 479
Matt W Avatar asked Aug 15 '11 21:08

Matt W


1 Answers

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!

like image 71
jkoelker Avatar answered Oct 18 '22 07:10

jkoelker