2013-08-16 18 views
19

Attualmente sto testando la mia app con suggerimenti da http://flask.pocoo.org/docs/testing/, ma vorrei aggiungere un'intestazione a una richiesta di post.Flask e Werkzeug: test di una richiesta di post con intestazioni personalizzate

La mia richiesta è attualmente:

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

ma vorrei aggiungere un contenuto-MD5 alla richiesta. È possibile?

mie indagini:

Flask client (in boccetta/testing.py) si estende Client Werkzeug, qui documentata: http://werkzeug.pocoo.org/docs/test/

Come si può vedere, post utilizza open. Ma open ha solo:

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. 

Quindi sembra che non sia supportato. Come potrei ottenere una funzionalità simile, comunque?

risposta

37

open anche prendere *args e **kwargs usato come EnvironBuilder argomenti. Quindi puoi aggiungere solo l'argomento headers alla tua prima richiesta:

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

Werkzeug per il salvataggio!

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 
Problemi correlati