File: test_response_overwriting.py

package info (click to toggle)
pytest-flask 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: python: 553; makefile: 161; sh: 5
file content (35 lines) | stat: -rw-r--r-- 908 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pytest
from flask import Flask
from flask import jsonify
from flask import url_for


class TestResponseOverwriting:
    """
    we overwrite the app fixture here so we can test
    _monkeypatch_response_class (an autouse fixture)
    will return the original response_class since a
    json @property is already present in response_class
    """

    @pytest.fixture
    def app(self):
        app = Flask(__name__)
        app.config["SECRET_KEY"] = "42"

        class MyResponse(app.response_class):
            @property
            def json(self):
                return 49

        @app.route("/ping")
        def ping():
            return jsonify(ping="pong")

        app.response_class = MyResponse

        return app

    def test_dont_rewrite_existing_implementation(self, accept_json, client):
        res = client.get(url_for("ping"), headers=accept_json)
        assert res.json == 49