A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: Invalid base url in api resolution (#5509)
Dylan Madisetti50c0ba2a87a7
diff --git a/frontend/src/core/network/__tests__/api.test.ts b/frontend/src/core/network/__tests__/api.test.tsnew file mode 100644index 0000000000..11c78f861d--- /dev/null+++ b/frontend/src/core/network/__tests__/api.test.ts@@ -0,0 +1,80 @@+/* Copyright 2024 Marimo. All rights reserved. */++import { beforeEach, describe, expect, it, vi } from "vitest";+import { API } from "../api";++// Mock fetch globally+const mockFetch = vi.fn();+global.fetch = mockFetch;++// Default mock for getRuntimeManager+let baseUrl = "http://localhost:8000";+vi.mock("@/core/runtime/config", () => ({+ getRuntimeManager: () => ({+ get httpURL() {+ return new URL(baseUrl);+ },+ headers: () => ({}),+ }),+}));++describe("API", () => {+ beforeEach(() => {+ mockFetch.mockReset();+ baseUrl = "http://localhost:8000";+ });++ it("API.post calls fetch with POST and correct URL", async () => {+ mockFetch.mockResolvedValue({+ ok: true,+ headers: { get: () => "application/json" },+ json: () => Promise.resolve({ ok: true }),+ });+ await API.post("/foo", { bar: 1 });+ expect(mockFetch).toHaveBeenCalledWith(+ "http://localhost:8000/api/foo",+ expect.objectContaining({ method: "POST" }),+ );+ });++ it("API.get calls fetch with GET and correct URL", async () => {+ mockFetch.mockResolvedValue({+ ok: true,+ headers: { get: () => "application/json" },+ json: () => Promise.resolve({ ok: true }),+ });+ await API.get("/bar");+ expect(mockFetch).toHaveBeenCalledWith(+ "http://localhost:8000/api/bar",+ expect.objectContaining({ method: "GET" }),+ );+ });++ it("API.post handles base URL with path correctly", async () => {+ baseUrl = "http://example.com/e";+ mockFetch.mockResolvedValue({+ ok: true,+ headers: { get: () => "application/json" },+ json: () => Promise.resolve({ ok: true }),+ });+ await API.post("/foo", { bar: 1 });+ expect(mockFetch).toHaveBeenCalledWith(+ "http://example.com/e/api/foo",+ expect.objectContaining({ method: "POST" }),+ );+ });++ it("API.post handles base URL with trailing slash correctly", async () => {+ baseUrl = "http://example.com/e/";+ mockFetch.mockResolvedValue({+ ok: true,+ headers: { get: () => "application/json" },+ json: () => Promise.resolve({ ok: true }),+ });+ await API.post("/foo", { bar: 1 });+ expect(mockFetch).toHaveBeenCalledWith(+ "http://example.com/e/api/foo",+ expect.objectContaining({ method: "POST" }),+ );+ });+});diff --git a/frontend/src/core/network/api.ts b/frontend/src/core/network/api.tsindex de1e0bfea7..db1809917f 100644--- a/frontend/src/core/network/api.ts+++ b/frontend/src/core/network/api.ts@@ -2,6 +2,7 @@import { createMarimoClient } from "@marimo-team/marimo-api";import { Logger } from "../../utils/Logger";+import { Strings } from "../../utils/strings";import { getRuntimeManager } from "../runtime/config";import type { RuntimeManager } from "../runtime/runtime";@@ -26,7 +27,9 @@ export const API = {baseUrl?: string;} = {},): Promise<RESP> {- const baseUrl = opts.baseUrl ?? getBaseUriWithoutQueryParams();+ const baseUrl = Strings.withTrailingSlash(+ opts.baseUrl ?? getBaseUriWithoutQueryParams(),+ );const fullUrl = `${baseUrl}api${url}`;return fetch(fullUrl, {method: "POST",@@ -65,7 +68,9 @@ export const API = {baseUrl?: string;} = {},): Promise<RESP> {- const baseUrl = opts.baseUrl ?? getBaseUriWithoutQueryParams();+ const baseUrl = Strings.withTrailingSlash(+ opts.baseUrl ?? getBaseUriWithoutQueryParams(),+ );const fullUrl = `${baseUrl}api${url}`;return fetch(fullUrl, {method: "GET",diff --git a/frontend/src/utils/strings.ts b/frontend/src/utils/strings.tsindex fd11725f83..9b69159c2d 100644--- a/frontend/src/utils/strings.ts+++ b/frontend/src/utils/strings.ts@@ -37,6 +37,9 @@ export const Strings = {.replaceAll("\n", " ");},+ withTrailingSlash(url: string): string {+ return url.endsWith("/") ? url : `${url}/`;+ },withoutTrailingSlash(url: string): string {return url.endsWith("/") ? url.slice(0, -1) : url;},