python - 在 web.py 应用程序上使用粘贴测试 400 错误

标签 python functional-testing paste

我正在使用 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/

相关文章:

python - 使用 RDFlib 向 GraphDB 插入数据时查询出错

python - 管道长时间运行的进程

unit-testing - tdd - 为第 3 方代码创建测试

javascript - 如何在 jQuery 中粘贴时保持文本格式?

python - 创建一个字典列表,其中每个字典都包含另一个字典作为值

python - 如何在 for 循环中注释类型?

ruby-on-rails - rails : How to test a controller with routes defined via a scope?

ruby-on-rails - 使用 Recaptcha 进行 Rails 功能测试

r - 如何在公式中使用字符串引用变量?

r - 取消列出列名称并在facet_grid中使用它