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
150
151
152
153
154
155
156
157
158
159
160
161import json
import os
from click.testing import CliRunner
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.testclient import TestClient
from apistar.cli import cli
def test_valid_document(tmpdir):
schema = os.path.join(tmpdir, "schema.json")
with open(schema, "w") as schema_file:
schema_file.write(
json.dumps(
{"openapi": "3.0.0", "info": {"title": "", "version": ""}, "paths": {}}
)
)
runner = CliRunner()
result = runner.invoke(cli, ["validate", "--path", schema, "--format", "openapi"])
assert result.exit_code == 0
assert result.output == "โ Valid OpenAPI schema.\n"
def test_invalid_document(tmpdir):
schema = os.path.join(tmpdir, "schema.json")
with open(schema, "w") as schema_file:
schema_file.write(json.dumps({"openapi": "3.0.0", "info": {"version": ""}}))
runner = CliRunner()
result = runner.invoke(cli, ["validate", "--path", schema, "--format", "openapi"])
assert result.exit_code != 0
assert result.output == (
"* The field 'paths' is required. (At line 1, column 1.)\n"
"* The field 'title' is required. (At ['info'], line 1, column 30.)\n"
"โ Invalid OpenAPI schema.\n"
)
def test_invalid_document_verbose(tmpdir):
schema = os.path.join(tmpdir, "schema.json")
with open(schema, "w") as schema_file:
schema_file.write(json.dumps({"openapi": "3.0.0", "info": {"version": ""}}))
runner = CliRunner()
result = runner.invoke(
cli, ["validate", "--path", schema, "--format", "openapi", "--verbose"]
)
assert result.exit_code != 0
assert result.output == (
'{"openapi": "3.0.0", "info": {"version": ""}}\n'
"^ The field 'paths' is required.\n"
" ^ The field 'title' is required.\n"
"\n"
"โ Invalid OpenAPI schema.\n"
)
def test_docs(tmpdir):
schema = os.path.join(tmpdir, "schema.json")
output_dir = os.path.join(tmpdir, "build")
output_index = os.path.join(output_dir, "index.html")
with open(schema, "w") as schema_file:
schema_file.write(
json.dumps(
{"openapi": "3.0.0", "info": {"title": "", "version": ""}, "paths": {}}
)
)
runner = CliRunner()
result = runner.invoke(
cli,
["docs", "--path", schema, "--format", "openapi", "--output-dir", output_dir],
)
assert result.exit_code == 0
assert result.output == 'โ Documentation built at "%s".\n' % output_index
app = Starlette()
@app.route("/homepage")
def homepage(request):
return JSONResponse({"hello": "world"})
@app.route("/error")
def error(request):
return JSONResponse({"error": "something failed"}, status_code=400)
def test_request(tmpdir):
schema = os.path.join(tmpdir, "schema.json")
with open(schema, "w") as schema_file:
schema_file.write(
json.dumps(
{
"openapi": "3.0.0",
"info": {"title": "", "version": ""},
"servers": [{"url": "https://testserver"}],
"paths": {"/homepage": {"get": {"operationId": "example"}}},
}
)
)
session = TestClient(app)
runner = CliRunner()
cmd = ["request", "--path", schema, "example"]
result = runner.invoke(cli, cmd, obj=session)
assert result.exit_code == 0
assert result.output == '{\n "hello": "world"\n}\n'
def test_request_verbose(tmpdir):
schema = os.path.join(tmpdir, "schema.json")
with open(schema, "w") as schema_file:
schema_file.write(
json.dumps(
{
"openapi": "3.0.0",
"info": {"title": "", "version": ""},
"servers": [{"url": "https://testserver"}],
"paths": {"/homepage": {"get": {"operationId": "example"}}},
}
)
)
session = TestClient(app)
runner = CliRunner()
cmd = ["request", "--path", schema, "--verbose", "example"]
result = runner.invoke(cli, cmd, obj=session)
assert result.exit_code == 0
assert "> GET /homepage HTTP/1.1" in result.output
assert "< 200 OK" in result.output
assert '{\n "hello": "world"\n}\n' in result.output
def test_request_error(tmpdir):
schema = os.path.join(tmpdir, "schema.json")
with open(schema, "w") as schema_file:
schema_file.write(
json.dumps(
{
"openapi": "3.0.0",
"info": {"title": "", "version": ""},
"servers": [{"url": "https://testserver"}],
"paths": {"/error": {"get": {"operationId": "example"}}},
}
)
)
session = TestClient(app)
runner = CliRunner()
cmd = ["request", "--path", schema, "example"]
result = runner.invoke(cli, cmd, obj=session)
assert result.exit_code != 0
assert result.output == '{\n "error": "something failed"\n}\nโ 400 Bad Request\n'