-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathtest_apidoc.py
More file actions
149 lines (107 loc) · 4.94 KB
/
Copy pathtest_apidoc.py
File metadata and controls
149 lines (107 loc) · 4.94 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pytest
from flask import url_for, Blueprint
from werkzeug.routing import BuildError
import flask_restx as restx
class APIDocTest(object):
def test_default_apidoc_on_root(self, app, client):
restx.Api(app, version="1.0")
assert url_for("doc") == url_for("root")
response = client.get(url_for("doc"))
assert response.status_code == 200
assert response.content_type == "text/html; charset=utf-8"
def test_default_apidoc_on_root_lazy(self, app, client):
api = restx.Api(version="1.0")
api.init_app(app)
assert url_for("doc") == url_for("root")
response = client.get(url_for("doc"))
assert response.status_code == 200
assert response.content_type == "text/html; charset=utf-8"
def test_default_apidoc_on_root_with_blueprint(self, app, client):
blueprint = Blueprint("api", __name__, url_prefix="/api")
restx.Api(blueprint, version="1.0")
app.register_blueprint(blueprint)
assert url_for("api.doc") == url_for("api.root")
response = client.get(url_for("api.doc"))
assert response.status_code == 200
assert response.content_type == "text/html; charset=utf-8"
def test_apidoc_with_custom_validator(self, app, client):
app.config["SWAGGER_VALIDATOR_URL"] = "http://somewhere.com/validator"
restx.Api(app, version="1.0")
response = client.get(url_for("doc"))
assert response.status_code == 200
assert response.content_type == "text/html; charset=utf-8"
assert 'validatorUrl: "http://somewhere.com/validator" || null,' in str(
response.data
)
def test_apidoc_doc_expansion_parameter(self, app, client):
restx.Api(app)
response = client.get(url_for("doc"))
assert 'docExpansion: "none"' in str(response.data)
app.config["SWAGGER_UI_DOC_EXPANSION"] = "list"
response = client.get(url_for("doc"))
assert 'docExpansion: "list"' in str(response.data)
app.config["SWAGGER_UI_DOC_EXPANSION"] = "full"
response = client.get(url_for("doc"))
assert 'docExpansion: "full"' in str(response.data)
def test_apidoc_doc_display_operation_id(self, app, client):
restx.Api(app)
response = client.get(url_for("doc"))
assert "displayOperationId: false" in str(response.data)
app.config["SWAGGER_UI_OPERATION_ID"] = False
response = client.get(url_for("doc"))
assert "displayOperationId: false" in str(response.data)
app.config["SWAGGER_UI_OPERATION_ID"] = True
response = client.get(url_for("doc"))
assert "displayOperationId: true" in str(response.data)
def test_apidoc_doc_display_request_duration(self, app, client):
restx.Api(app)
response = client.get(url_for("doc"))
assert "displayRequestDuration: false" in str(response.data)
app.config["SWAGGER_UI_REQUEST_DURATION"] = False
response = client.get(url_for("doc"))
assert "displayRequestDuration: false" in str(response.data)
app.config["SWAGGER_UI_REQUEST_DURATION"] = True
response = client.get(url_for("doc"))
assert "displayRequestDuration: true" in str(response.data)
def test_custom_apidoc_url(/sitelet?url=https%3A%2F%2Fgithub.com%2Fpython-restx%2Fflask-restx%2Fblob%2Fmaster%2Ftests%2Fself%2C%2520app%2C%2520client):
restx.Api(app, version="1.0", doc="/doc/")
doc_url = url_for("doc")
root_url = url_for("root")
assert doc_url != root_url
response = client.get(root_url)
assert response.status_code == 404
assert doc_url == "/doc/"
response = client.get(doc_url)
assert response.status_code == 200
assert response.content_type == "text/html; charset=utf-8"
def test_custom_api_prefix(self, app, client):
prefix = "/api"
api = restx.Api(app, prefix=prefix)
api.namespace("resource")
assert url_for("root") == prefix
def test_custom_apidoc_page(self, app, client):
api = restx.Api(app, version="1.0")
content = "My Custom API Doc"
@api.documentation
def api_doc():
return content
response = client.get(url_for("doc"))
assert response.status_code == 200
assert response.data.decode("utf8") == content
def test_custom_apidoc_page_lazy(self, app, client):
blueprint = Blueprint("api", __name__, url_prefix="/api")
api = restx.Api(blueprint, version="1.0")
content = "My Custom API Doc"
@api.documentation
def api_doc():
return content
app.register_blueprint(blueprint)
response = client.get(url_for("api.doc"))
assert response.status_code == 200
assert response.data.decode("utf8") == content
def test_disabled_apidoc(self, app, client):
restx.Api(app, version="1.0", doc=False)
with pytest.raises(BuildError):
url_for("doc")
response = client.get(url_for("root"))
assert response.status_code == 404