我正在使用 paste 对我的 web.py 应用程序中的“ Controller ”进行一些功能测试。在一种情况下,当向 API 端点发出格式错误的帖子时,我试图测试 400 响应。这是我的测试结果:
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
但是我得到以下异常:
AppError: Bad response: 400 Bad Request (not 200 OK or 3xx redirect for /api/users)
我看到 paste 有 HttpException 中间件,但我找不到任何关于如何使用它的例子,或者它是否是正确的方法。有什么建议么?还是我只是做错了?
最佳答案
我知道我迟到了,但我在寻找同一问题的答案时遇到了这个问题。要允许 TestApp 传回非 2xx/3xx 响应,您需要告诉请求允许“错误”。
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
黑客攻击快乐!
关于python - 在 web.py 应用程序上使用粘贴测试 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7071210/