forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_openapi_swagger.py
114 lines (81 loc) · 3.89 KB
/
test_openapi_swagger.py
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
import json
from typing import Dict
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from tests.functional.utils import load_event
LOAD_GW_EVENT = load_event("apiGatewayProxyEvent.json")
def test_openapi_swagger():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger()
LOAD_GW_EVENT["path"] = "/swagger"
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["multiValueHeaders"]["Content-Type"] == ["text/html"]
def test_openapi_swagger_compressed():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(compress=True)
LOAD_GW_EVENT["headers"] = {"Accept-Encoding": "gzip, deflate, br"}
LOAD_GW_EVENT["path"] = "/swagger"
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["isBase64Encoded"]
assert result["multiValueHeaders"]["Content-Type"] == ["text/html"]
assert result["multiValueHeaders"]["Content-Encoding"] == ["gzip"]
def test_openapi_swagger_with_custom_base_url():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(swagger_base_url="https://aws.amazon.com")
LOAD_GW_EVENT["path"] = "/swagger"
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["multiValueHeaders"]["Content-Type"] == ["text/html"]
assert "aws.amazon.com/swagger-ui.min.css" in result["body"]
assert "aws.amazon.com/swagger-ui-bundle.min.js" in result["body"]
def test_openapi_swagger_with_custom_base_url_no_embedded_assets():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(swagger_base_url="https://aws.amazon.com")
# Try to load the CSS file
LOAD_GW_EVENT["path"] = "/swagger.css"
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 404
# Try to load the JS file
LOAD_GW_EVENT["path"] = "/swagger.js"
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 404
def test_openapi_swagger_json_view_with_default_path():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(title="OpenAPI JSON View")
LOAD_GW_EVENT["path"] = "/swagger"
LOAD_GW_EVENT["queryStringParameters"] = {"format": "json"}
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["multiValueHeaders"]["Content-Type"] == ["application/json"]
assert isinstance(json.loads(result["body"]), Dict)
assert "OpenAPI JSON View" in result["body"]
def test_openapi_swagger_json_view_with_custom_path():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(path="/fizzbuzz/foobar", title="OpenAPI JSON View")
LOAD_GW_EVENT["path"] = "/fizzbuzz/foobar"
LOAD_GW_EVENT["queryStringParameters"] = {"format": "json"}
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 200
assert result["multiValueHeaders"]["Content-Type"] == ["application/json"]
assert isinstance(json.loads(result["body"]), Dict)
assert "OpenAPI JSON View" in result["body"]
def test_openapi_swagger_with_rest_api_default_stage():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger()
event = load_event("apiGatewayProxyEvent.json")
event["path"] = "/swagger"
event["requestContext"]["stage"] = "$default"
result = app(event, {})
assert result["statusCode"] == 200
assert "ui.specActions.updateUrl('/swagger?format=json')" in result["body"]
def test_openapi_swagger_with_rest_api_stage():
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger()
event = load_event("apiGatewayProxyEvent.json")
event["path"] = "/swagger"
event["requestContext"]["stage"] = "prod"
event["requestContext"]["path"] = "/prod/swagger"
result = app(event, {})
assert result["statusCode"] == 200
assert "ui.specActions.updateUrl('/prod/swagger?format=json')" in result["body"]