LoginSignup
1
1

More than 5 years have passed since last update.

FlaskのテストクライアントのHTTPヘッダを変更したいとき

Last updated at Posted at 2013-11-07

これでいけました
たとえばUserAgentをいじる

というかこれのままです
http://stackoverflow.com/questions/15278285/setting-mocking-request-headers-for-flask-app-unit-test

tests.py
# -*- coding: utf-8 -*-
from nose.tools import ok_, eq_
import flask

class ClientProxy(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        environ['HTTP_USER_AGENT'] = environ.get('HTTP_USER_AGENT', 'hogehoge')
        return self.app(environ, start_response)


def sample_test():
    app = flask.Flask(__name__)
    app.wsgi_app = ClientProxy(app.wsgi_app)
    with app.test_client() as c:
        c.get('/')
        eq_(flask.request.user_agent.string, 'hogehoge')
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1