开发者

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

开发者 https://www.devze.com 2023-03-28 20:00 出处:网络
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

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('/ap开发者_如何学JAVAi/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?


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号