简体   繁体   English

Flask test_client 无法处理 HTTP 204 No data

[英]Flask test_client can't handle HTTP 204 No data

I have delete endpoint, returning HTTP 204我删除了端点,返回 HTTP 204

@blueprint.route('/foo', methods=['DELETE'])
def delete_tag(id):
    # ....
    return '', 204

and I want to test it我想测试一下

def test_delete_tag(self):
    resp = self.client.delete(url_for('tags.delete_tag', id=1))
    self.assertEqual(resp.status_code, 204)

but I got exception但我有例外

Traceback (most recent call last):
  File "tests/tags_test.py", line 31, in test_delete_tag
    resp = self.client.delete(url_for('tags.delete_tag', id=1)})
  File ".virtualenvs/...site-packages/werkzeug/test.py", line 799, in delete
    return self.open(*args, **kw)
  File ".virtualenvs/...site-packages/flask/testing.py", line 108, in open
    follow_redirects=follow_redirects)
  File ".virtualenvs/...site-packages/werkzeug/test.py", line 742, in open
    response = self.run_wsgi_app(environ, buffered=buffered)
  File ".virtualenvs/...site-packages/werkzeug/test.py", line 659, in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
  File ".virtualenvs/.../site-packages/werkzeug/test.py", line 885, in run_wsgi_app
    buffer.append(next(app_iter))
StopIteration

with response status 200 it works all fine.响应状态为 200 时一切正常。 Is there way how to fix the test?有没有办法修复测试?

204 的挖掘根本是“无内容”,假设您不会在该响应中添加任何主体。

small flask app:小烧瓶应用程序:

from flask import Flask, request
app = Flask(__name__)

@app.route('/foo', methods=['DELETE'])
def delete_tag():
    print("i got", request.form['id'])
    return '', 204

@app.route('/foo2/<id>', methods=['DELETE'])
def delete_tag2(id):
    print("i got.. .", id)
    return '', 204

if __name__ == '__main__':
    app.run(debug=True)

and in ipython qtconsole;在 ipython qtconsole 中; i did this:我这样做了:

In [3]: from app import app

In [4]: from flask import url_for

In [5]: c = app.test_client()

In [6]: with app.test_request_context():
   ...:     rv = c.delete(url_for('delete_tag2', id=55))
   ...:     print(rv.status_code)
   ...:     
i got.. . 55
204

In [7]: rv = c.delete("/foo", data={"id": 555})
i got 555

In [8]: rv.status_code
Out[8]: 204

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM