-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Increase delay before firing off probe * Update routes on every incoming request * Update package.json * Modify sendRoutes to optionally use a promise store * Add unit tests to client library (for routes helpers) and update sendRoutes * Fix filter for running client library tests * Add changelog entry
- Loading branch information
Showing
10 changed files
with
214 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
"author": "Fiberplane<[email protected]>", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"version": "0.3.1-beta.1", | ||
"version": "0.3.1-beta.2", | ||
"dependencies": { | ||
"@opentelemetry/api": "~1.9.0", | ||
"@opentelemetry/exporter-trace-otlp-http": "^0.52.1", | ||
|
@@ -25,7 +25,8 @@ | |
"nodemon": "^3.1.7", | ||
"rimraf": "^6.0.1", | ||
"tsc-alias": "^1.8.10", | ||
"typescript": "^5.4.5" | ||
"typescript": "^5.4.5", | ||
"vitest": "^1.6.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
|
@@ -50,6 +51,7 @@ | |
"dev": "wrangler dev sample/index.ts", | ||
"prepublishOnly": "pnpm run build", | ||
"clean": "rimraf dist", | ||
"test": "vitest --run", | ||
"watch": "nodemon --watch src --ext ts,js,json --exec \"pnpm run build\"" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import type { FpxLogger } from "./logger"; | ||
import type { PromiseStore } from "./promiseStore"; | ||
import { | ||
isRouteInspectorRequest, | ||
respondWithRoutes, | ||
sendRoutes, | ||
} from "./routes"; | ||
import type { HonoLikeApp, HonoLikeFetch } from "./types"; | ||
|
||
describe("routes", () => { | ||
describe("isRouteInspectorRequest", () => { | ||
it("should return true when X-Fpx-Route-Inspector header is present", () => { | ||
const request = new Request("https://example.com", { | ||
headers: { "X-Fpx-Route-Inspector": "true" }, | ||
}); | ||
expect(isRouteInspectorRequest(request)).toBe(true); | ||
}); | ||
|
||
it("should return false when X-Fpx-Route-Inspector header is not present", () => { | ||
const request = new Request("https://example.com"); | ||
expect(isRouteInspectorRequest(request)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("sendRoutes", () => { | ||
let mockFetch: ReturnType<typeof vi.fn<[HonoLikeFetch], unknown>>; | ||
let mockApp: HonoLikeApp; | ||
let mockLogger: FpxLogger; | ||
let mockPromiseStore: PromiseStore; | ||
|
||
beforeEach(() => { | ||
mockFetch = vi.fn().mockResolvedValue(new Response("OK")); | ||
mockApp = { | ||
routes: [{ method: "GET", path: "/test", handler: () => {} }], | ||
fetch: vi.fn(), | ||
} as unknown as HonoLikeApp; | ||
mockLogger = { debug: vi.fn() } as unknown as FpxLogger; | ||
mockPromiseStore = { add: vi.fn() } as unknown as PromiseStore; | ||
}); | ||
|
||
it("should send routes successfully", async () => { | ||
const result = await sendRoutes( | ||
mockFetch as unknown as typeof fetch, | ||
"https://fpx.example.com", | ||
mockApp, | ||
mockLogger, | ||
); | ||
expect(result).toBe(true); | ||
expect(mockFetch).toHaveBeenCalledWith( | ||
"https://fpx.example.com/v0/probed-routes", | ||
expect.objectContaining({ | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: expect.any(String), | ||
}), | ||
); | ||
}); | ||
|
||
it("should use promiseStore when provided", async () => { | ||
await sendRoutes( | ||
mockFetch as unknown as typeof fetch, | ||
"https://fpx.example.com", | ||
mockApp, | ||
mockLogger, | ||
mockPromiseStore, | ||
); | ||
expect(mockPromiseStore.add).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should return false and log error on failure", async () => { | ||
mockFetch.mockRejectedValue(new Error("Network error")); | ||
const result = await sendRoutes( | ||
mockFetch as unknown as typeof fetch, | ||
"https://fpx.example.com", | ||
mockApp, | ||
mockLogger, | ||
); | ||
expect(result).toBe(false); | ||
expect(mockLogger.debug).toHaveBeenCalledWith( | ||
"Error sending routes to FPX:", | ||
"Network error", | ||
); | ||
}); | ||
}); | ||
|
||
describe("respondWithRoutes", () => { | ||
let mockFetch: ReturnType<typeof vi.fn<[HonoLikeFetch], unknown>>; | ||
let mockApp: HonoLikeApp; | ||
let mockLogger: FpxLogger; | ||
|
||
beforeEach(() => { | ||
mockFetch = vi.fn().mockResolvedValue(new Response("OK")); | ||
mockApp = { | ||
routes: [{ method: "GET", path: "/test", handler: () => {} }], | ||
fetch: vi.fn(), | ||
} as unknown as HonoLikeApp; | ||
mockLogger = { debug: vi.fn() } as unknown as FpxLogger; | ||
}); | ||
|
||
it("should respond with 200 OK when routes are sent successfully", async () => { | ||
const response = await respondWithRoutes( | ||
mockFetch as unknown as typeof fetch, | ||
"https://fpx.example.com", | ||
mockApp, | ||
mockLogger, | ||
); | ||
expect(response.status).toBe(200); | ||
expect(await response.text()).toBe("OK"); | ||
}); | ||
|
||
it("should respond with 500 error when sending routes fails", async () => { | ||
mockFetch.mockRejectedValue(new Error("Network error")); | ||
const response = await respondWithRoutes( | ||
mockFetch as unknown as typeof fetch, | ||
"https://fpx.example.com", | ||
mockApp, | ||
mockLogger, | ||
); | ||
expect(response.status).toBe(500); | ||
expect(await response.text()).toBe("Error sending routes to FPX"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from "vitest/config"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
globals: true, | ||
}, | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters