2013-08-16 11 views
19

Aktualnie testuję swoją aplikację z sugestiami od http://flask.pocoo.org/docs/testing/, ale chciałbym dodać nagłówek do żądania wpisu.Flask and Werkzeug: Testowanie żądania wpisu z niestandardowymi nagłówkami

Moja prośba jest obecnie:

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

ale chciałbym dodać zawartość MD5 do wniosku. czy to możliwe?

Moje badania:

Kolba klienta (w kolbie/testing.py) rozciąga Client Werkzeug'S, udokumentowany tutaj: http://werkzeug.pocoo.org/docs/test/

Jak widać, post używa open. Ale open ma tylko:

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. 

Wygląda na to, że nie jest obsługiwany. Jak mogę jednak uzyskać taką funkcję?

Odpowiedz

37

open również wziąć *args i **kwargs który stosowany jako EnvironBuilder argumentów. Możesz więc dodać tylko argument headers do swojej pierwszej prośby o wpis:

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 na ratunek!

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 
Powiązane problemy