2026-01-29 19:48:36 +09:00
|
|
|
import { describe, expect, it } from "bun:test"
|
|
|
|
|
import { addResourceToParams, getResourceIndicator } from "./resource-indicator"
|
|
|
|
|
|
|
|
|
|
describe("getResourceIndicator", () => {
|
|
|
|
|
it("returns URL unchanged when already normalized", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const url = "https://mcp.example.com"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const result = getResourceIndicator(url)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(result).toBe("https://mcp.example.com")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("strips trailing slash", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const url = "https://mcp.example.com/"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const result = getResourceIndicator(url)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(result).toBe("https://mcp.example.com")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("strips query parameters", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const url = "https://mcp.example.com/v1?token=abc&debug=true"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const result = getResourceIndicator(url)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(result).toBe("https://mcp.example.com/v1")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("strips fragment", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const url = "https://mcp.example.com/v1#section"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const result = getResourceIndicator(url)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(result).toBe("https://mcp.example.com/v1")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("strips query and trailing slash together", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const url = "https://mcp.example.com/api/?key=val"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const result = getResourceIndicator(url)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(result).toBe("https://mcp.example.com/api")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("preserves path segments", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const url = "https://mcp.example.com/org/project/v2"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const result = getResourceIndicator(url)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(result).toBe("https://mcp.example.com/org/project/v2")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("preserves port number", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const url = "https://mcp.example.com:8443/api/"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
const result = getResourceIndicator(url)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(result).toBe("https://mcp.example.com:8443/api")
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("addResourceToParams", () => {
|
|
|
|
|
it("sets resource parameter on empty params", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const params = new URLSearchParams()
|
|
|
|
|
const resource = "https://mcp.example.com"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
addResourceToParams(params, resource)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(params.get("resource")).toBe("https://mcp.example.com")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("adds resource alongside existing parameters", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const params = new URLSearchParams({ grant_type: "authorization_code" })
|
|
|
|
|
const resource = "https://mcp.example.com/v1"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
addResourceToParams(params, resource)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(params.get("grant_type")).toBe("authorization_code")
|
|
|
|
|
expect(params.get("resource")).toBe("https://mcp.example.com/v1")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("overwrites existing resource parameter", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
2026-01-29 19:48:36 +09:00
|
|
|
const params = new URLSearchParams({ resource: "https://old.example.com" })
|
|
|
|
|
const resource = "https://new.example.com"
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when
|
2026-01-29 19:48:36 +09:00
|
|
|
addResourceToParams(params, resource)
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then
|
2026-01-29 19:48:36 +09:00
|
|
|
expect(params.get("resource")).toBe("https://new.example.com")
|
|
|
|
|
expect(params.getAll("resource")).toHaveLength(1)
|
|
|
|
|
})
|
|
|
|
|
})
|