traduccion python flask werkzeug

python - werkzeug traduccion



Flask y Werkzeug: prueba de una solicitud de publicaciĆ³n con encabezados personalizados (2)

Actualmente estoy probando mi aplicación con sugerencias de http://flask.pocoo.org/docs/testing/ , pero me gustaría agregar un encabezado a una solicitud de publicación.

Mi solicitud es actualmente:

self.app.post(''/v0/scenes/test/foo'', data=dict(image=(StringIO(''fake image''), ''image.png'')))

pero me gustaría agregar un contenido-md5 a la solicitud. es posible?

Mis investigaciones:

Flask Client (en flask / testing.py) extiende el Cliente de Werkzeug, documentado aquí: http://werkzeug.pocoo.org/docs/test/

Como se puede ver, el post usa open . Pero open solo tiene:

Parameters: as_tuple – Returns a tuple in the form (environ, result) buffered – Set this to True to buffer the application run. This will automatically close the application for you as well. follow_redirects – Set this to True if the Client should follow HTTP redirects.

Así que parece que no es compatible. Sin embargo, ¿cómo puedo hacer que funcione esa característica?


Werkzeug al rescate!

from werkzeug.test import EnvironBuilder, run_wsgi_app from werkzeug.wrappers import Request builder = EnvironBuilder(path=''/v0/scenes/bucket/foo'', method=''POST'', data={''image'': (StringIO(''fake image''), ''image.png'')}, / headers={''content-md5'': ''some hash''}) env = builder.get_environ() (app_iter, status, headers) = run_wsgi_app(http.app.wsgi_app, env) status = int(status[:3]) # output will be something like 500 INTERNAL SERVER ERROR


open también toma *args y **kwargs que se usaron como argumentos de EnvironBuilder . Por lo tanto, puede agregar solo argumentos de headers a su primera solicitud de publicación:

with self.app.test_client() as client: client.post(''/v0/scenes/test/foo'', data=dict(image=(StringIO(''fake image''), ''image.png'')), headers={''content-md5'': ''some hash''});