web-dev-qa-db-fra.com

Flask et Werkzeug: Test d'une demande de publication avec des en-têtes personnalisés

Je teste actuellement mon application avec des suggestions de http://flask.pocoo.org/docs/testing/ , mais je voudrais ajouter un en-tête à une demande de publication.

Ma demande est actuellement:

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

mais je voudrais ajouter un content-md5 à la demande. Est-ce possible?

Mes investigations:

Flask Client (dans flask/testing.py) étend le client de Werkzeug, documenté ici: http://werkzeug.pocoo.org/docs/test/

Comme vous pouvez le voir, post utilise open. Mais open n'a que:

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.

Il semble donc que ce ne soit pas pris en charge. Comment puis-je faire fonctionner une telle fonctionnalité?

33
theicfire

open prend également *args et **kwargs, utilisé comme arguments EnvironBuilder. Vous pouvez donc ajouter juste headers argument à votre première demande de publication:

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'});
61
tbicr

Werkzeug à la rescousse!

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
6
theicfire