[PYTHON] When you want to change the HTTP headers of Flask's test client

I was able to do this For example, play with UserAgent

Or rather, it's just like this 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')

Recommended Posts

When you want to change the HTTP headers of Flask's test client
When you want to save the result of the callback function somewhere
When you want to update the chrome driver.
Python Note: When you want to know the attributes of an object
[Python3] Code that can be used when you want to change the extension of an image at once
When you want to keep the Sphinx documentation theme as usual
When you want to adjust the axis scale interval with APLpy
When you want to use multiple versions of the same Python library (virtual environment using venv)
Script to change the description of fasta
Memorandum of means when you want to make machine learning with 50 images
Defense Techniques When You Have to Fight the Performance of Unfamiliar Applications (Part 2)
Change the decimal point of logging from, to.
What you want to memorize with the basic "string manipulation" grammar of python
Linux: Netplan configuration guide to see when you want to fix the IP address
I want to customize the appearance of zabbix
What to do when you want to receive files from a Windows client remotely
When the variable you want to superscript with matplotlib is two or more characters
I want to be notified of the connection environment when the Raspberry Pi connects to the network
I want to grep the execution result of strace
Settings when you want to run python-mecab with travis
When you want to filter with Django REST framework
I want to fully understand the basics of Bokeh
When you want to play a game via Proxy
When you want to plt.save in a for statement
When you think the update of ManjaroLinux is strange
The programming language you want to be able to use
I want to increase the security of ssh connections
I want to change the symbolic link destination of / lib64 from / usr / lib64 to / my-lib64 on CentOS
Make a note of what you want to do in the future with Raspberry Pi
[Python] When you want to import and use your own package in the upper directory
Solution when you want to use cv_bridge with python3 (virtualenv)
[Django] A memorandum when you want to communicate asynchronously [Python3]
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
[AWS] What to do when you want to pip with Lambda
How to test the attributes added by add_request_method of pyramid
An introduction to object orientation-let's change the internal state of an object
I want to use only the normalization process of SudachiPy
Use aggdraw when you want to draw beautifully with pillow
I want to change the Japanese flag to the Palau flag with Numpy
I want to judge the authenticity of the elements of numpy array
When you want to register Django's initial data with relationships
Change the volume of Pepper according to the surrounding environment (sound)
I want to know the features of Python and pip
When you want to use python2.x on modern Gentoo Linux
If you want your colleagues to use the same language
Change the message displayed when logging in to Raspberry Pi
Change the installation destination when --user is added to pip
Keras I want to get the output of any layer !!
I want to know the legend of the IT technology world
When you want to hit a UNIX command on Python
[Django] What to do if the model you want to create has a large number of fields
[Django] Carefully explain the escape route if you really want to use the table of another application
You there who want to launch Rails on both the host and the client with Docker Compose!
Change the theme of Jupyter
Change the style of matplotlib
[Subprocess] When you want to execute another Python program in Python code
I want to get the name of the function / method being executed
I want to manually assign the training parameters of the [Pytorch] model
How to change the log level of Azure SDK for Python
Become familiar with (want to be) around the pipeline of spaCy
[Linux] I want to know the date when the user logged in