Skip to content

Commit

Permalink
fix: use relative path in asgi redirect (#3433)
Browse files Browse the repository at this point in the history
Fixes #2938
  • Loading branch information
mscolnick authored Jan 14, 2025
1 parent b8a0442 commit 744f1f9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
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

0 comments on commit 744f1f9

Please sign in to comment.