File: test_openapi_extensions.py

package info (click to toggle)
flask-openapi3 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,224 kB
  • sloc: python: 4,807; makefile: 14; javascript: 5
file content (64 lines) | stat: -rw-r--r-- 1,544 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
# @Author  : llc
# @Time    : 2023/5/31 14:32
import pytest

from flask_openapi3 import APIBlueprint, APIView, OpenAPI

app = OpenAPI(
    __name__,
    openapi_extensions={
        "x-google-endpoints": [{"name": "my-cool-api.endpoints.my-project-id.cloud.goog", "allowCors": True}]
    },
)

openapi_extensions = {"x-google-backend": {"address": "https://<NODE_SERVICE_ID>-<HASH>.a.run.app", "protocol": "h2"}}

app.config["TESTING"] = True


@pytest.fixture
def client():
    client = app.test_client()

    return client


@app.get("/", openapi_extensions=openapi_extensions)
def hello():
    return "ok"  # pragma: no cover


# APIBlueprint
api = APIBlueprint("book", __name__, url_prefix="/api")


@api.get("/book", openapi_extensions=openapi_extensions)
def get_book():
    return {"code": 0, "message": "ok"}  # pragma: no cover


app.register_api(api)

# APIView
api_view = APIView()


@api_view.route("/view/book")
class BookListAPIView:
    @api_view.doc(openapi_extensions=openapi_extensions)
    def post(self):
        return "ok"  # pragma: no cover


app.register_api_view(api_view)


def test_openapi(client):
    resp = client.get("/openapi/openapi.json")
    _json: dict = resp.json
    assert resp.status_code == 200
    assert _json.get("x-google-endpoints") is not None
    assert _json["paths"]["/"]["get"]["x-google-backend"] is not None
    assert _json["paths"]["/api/book"]["get"]["x-google-backend"] is not None
    assert _json["paths"]["/view/book"]["post"]["x-google-backend"] is not None