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

fix: use relative path in asgi redirect #3433

Merged
merged 1 commit into from
Jan 14, 2025
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
2 changes: 2 additions & 0 deletions marimo/_server/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ def build(self) -> "ASGIApp":
def create_redirect_to_slash(
base_url: str,
) -> Callable[[Request], Response]:
# Strip leading slash
base_url = base_url.lstrip("/")
redirect_path = f"{base_url}/"
return lambda _: RedirectResponse(
url=redirect_path, status_code=301
Expand Down
56 changes: 52 additions & 4 deletions tests/_server/test_asgi.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from __future__ import annotations

import os
import tempfile
import unittest
from pathlib import Path
from typing import Any
from typing import TYPE_CHECKING, Any

from starlette.applications import Starlette
from starlette.requests import Request
from starlette.responses import PlainTextResponse, Response
from starlette.testclient import TestClient
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from starlette.websockets import WebSocket

from marimo._server.asgi import (
ASGIAppBuilder,
DynamicDirectoryMiddleware,
create_asgi_app,
)

if TYPE_CHECKING:
from starlette.requests import Request
from starlette.types import ASGIApp, Message, Receive, Scope, Send
from starlette.websockets import WebSocket

contents = """
import marimo

Expand Down Expand Up @@ -142,6 +146,50 @@ def test_can_hit_health(self) -> None:
response = client.get("/app1/health")
assert response.status_code == 200, response.text

def test_mount_at_root(self) -> None:
"""Test that assets are correctly served when app is mounted at the root path."""
builder = create_asgi_app(quiet=True, include_code=True)
builder = builder.with_app(path="/app1", root=self.app1)
app = builder.build()

base_app = Starlette()
base_app.mount("/", app)
client = TestClient(base_app)

# Index page
response = client.get("/app1")
assert response.status_code == 200, response.text
# Index page with trailing slash
response = client.get("/app1/")
assert response.status_code == 200, response.text
# Health check
response = client.get("/app1/health")
assert response.status_code == 200, response.text

def test_mount_at_non_root(self) -> None:
"""Test that assets are correctly served when app is mounted at a non-root path."""
builder = create_asgi_app(quiet=True, include_code=True)
builder = builder.with_app(path="/app1", root=self.app1)
app = builder.build()

base_app = Starlette()
base_app.mount("/marimo", app)
client = TestClient(base_app)

# Not at the root
response = client.get("/app1")
assert response.status_code == 404, response.text

# Index page
response = client.get("/marimo/app1")
assert response.status_code == 200, response.text
# Index page with trailing slash
response = client.get("/marimo/app1/")
assert response.status_code == 200, response.text
# Health check
response = client.get("/marimo/app1/health")
assert response.status_code == 200, response.text

def test_app_with_middleware(self):
# Create a simple middleware
class TestMiddleware:
Expand Down
Loading