Request-like wrapper for test_client vial

I am trying to make usable tests for my package, but using Flask.test_client so different from the requests API that it was difficult for me to use it.

I tried to make requests.adapters.HTTPAdapter complete the response, but it seems like werkzeug not using httplib (or urllib ) to create its own Response object.

Any idea how this can be done? Link to existing code will be better (werkzeug + searches do not yield useful results)

Thank you very much!

+5
source share
2 answers

As I understand it, you need mocking (see What is Mocking? And Mock Object ). Well, a couple of your options are listed below:

0
source

You can use the requests method defined in the code section below. To use it, you also need to define the get_auth_headers method shown in the code below.

Features:

  • A wrapper around the client checking the flask.
  • Automatically set request headers.

    • Content-Type: Application/json when passing json
    • Basic Authentication built with auth passing.
  • Flushes your json data as a string to the Flask test client.
  • (None) Wrap the response in the requests.Response object

Code:

 class MyTestCase(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.client = self.app.test_client() self.app_context = self.app.app_context() self.app_context.push() db.create_all() def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop() def get_auth_headers(self, username, password): return { 'Authorization': 'Basic ' + base64.b64encode( (username + ':' + password).encode('utf-8')).decode('utf-8'), 'Accept': 'application/json', 'Content-Type': 'application/json' } def requests(self, method, url, json={}, auth=(), **kwargs): """Wrapper around Flask test client to automatically set headers if JSON data is passed + dump JSON data as string.""" if not hasattr(self.client, method): print("Method %s not supported" % method) return fun = getattr(self.client, method) # Set headers headers = {} if auth: username, password = auth headers = self.get_auth_headers(username, password) # Prepare JSON if needed if json: import json as _json headers.update({'Content-Type': 'application/json'}) response = fun(url, data=_json.dumps(json), headers=headers, **kwargs) else: response = fun(url, **kwargs) self.assertTrue(response.headers['Content-Type'] == 'application/json') return response 

Usage (in test case):

 def test_requests(self): url = 'http://localhost:5001' response = self.requests('get', url) response = self.requests('post', url, json={'a': 1, 'b': 2}, auth=('username', 'password')) ... 

The only difference from requests is that instead of entering requests.get(...) you will need to enter self.request('get', ...) .

If you really need requests behavior, you will need to define your own get , put , post , delete wrappers around requests

Note:

The best way to do this could be to subclass FlaskClient , as described in the Flags API documentation

0
source

Source: https://habr.com/ru/post/1261158/


All Articles