2013-08-16 6 views
19

現在、私のアプリはhttp://flask.pocoo.org/docs/testing/の提案でテストしていますが、投稿リクエストにヘッダーを追加したいと思います。Flask and Werkzeug:カスタムヘッダーを使用した投稿リクエストのテスト

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

が、私は、要求へのコンテンツ-MD5を追加したいと思います:

私の要求は、現在あります。これは可能ですか?

マイ調査:は(フラスコ/ testing.py中)

フラスコクライアントは、ここでは文書化され、WERKZEUGのクライアントを拡張: http://werkzeug.pocoo.org/docs/test/

を見てわかるように、postopenを使用しています。しかし、openは、次のもののみを持っています:

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. 

それはサポートされていないようです。しかし、どうすればそのような機能を動作させることができますか?

答えて

37

openEnvironBuilder引数として使用*args**kwargsを取ります。したがって、最初の投稿要求にheaders引数を追加することができます:

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 to the rescue!

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 
関連する問題