Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol and port control #207

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pyramid_openapi3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def openapi_view(view: View, info: ViewDeriverInfo) -> View:
if info.options.get("openapi"):

def wrapper_view(context: Context, request: Request) -> Response:

# We need this to be able to raise AttributeError if view code
# accesses request.openapi_validated on a view that is marked
# with openapi=False
Expand Down Expand Up @@ -154,6 +153,7 @@ def add_explorer_view(
ui_version: str = "4.15.5",
permission: str = NO_PERMISSION_REQUIRED,
apiname: str = "pyramid_openapi3",
proto_port: t.Optional[t.Tuple[str, int]] = None,
) -> None:
"""Serve Swagger UI at `route` url path.

Expand All @@ -162,6 +162,7 @@ def add_explorer_view(
:param template: Dotted path to the html template that renders Swagger UI response
:param ui_version: Swagger UI version string
:param permission: Permission for the explorer view
:proto_port: Internet protocol and port for the specification URL
"""

def register() -> None:
Expand All @@ -175,10 +176,19 @@ def explorer_view(request: Request) -> Response:
"to work."
)
with open(resolved_template.abspath()) as f:
if proto_port:
spec_url = request.route_url(
settings[apiname]["spec_route_name"],
_scheme=proto_port[0],
_port=proto_port[1],
)
else:
spec_url = request.route_url(settings[apiname]["spec_route_name"])

template = Template(f.read())
html = template.safe_substitute(
ui_version=ui_version,
spec_url=request.route_url(settings[apiname]["spec_route_name"]),
spec_url=spec_url,
)
return Response(html)

Expand Down
27 changes: 27 additions & 0 deletions pyramid_openapi3/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,33 @@ def test_add_explorer_view() -> None:
assert b"<title>Swagger UI</title>" in response.body


def test_add_explorer_view_custom_proto_port() -> None:
"""Test serving Swagger UI on a custom protocol and port."""
with testConfig() as config:
config.include("pyramid_openapi3")

with tempfile.NamedTemporaryFile() as document:
document.write(MINIMAL_DOCUMENT)
document.seek(0)

config.pyramid_openapi3_spec(
document.name, route="/foo.yaml", route_name="foo_api_spec"
)

config.pyramid_openapi3_add_explorer(proto_port=("https", 6543))
request = config.registry.queryUtility(
IRouteRequest, name="pyramid_openapi3.explorer"
)
view = config.registry.adapters.registered(
(IViewClassifier, request, Interface), IView, name=""
)
request = DummyRequest(config=config)
request.environ["SERVER_NAME"] = "example.com"
response = view(request=request, context=None)
assert b"<title>Swagger UI</title>" in response.body
assert b"https://example.com:6543/foo.yaml" in response.body


def test_add_multiple_explorer_views() -> None:
"""Test registration of multiple views serving different Swagger UI."""
with testConfig() as config:
Expand Down