Skip to content
Open
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: 1 addition & 1 deletion src/middleware/openaiAuthMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function openaiAuthMiddleware() {
const configuredKey = c.env.OPENAI_API_KEY;

if (!configuredKey) {
await next();
return c.json({ error: { message: "Server configuration error" } }, 500);
}

if (providedKey !== configuredKey) {
Expand Down
75 changes: 55 additions & 20 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
import { describe, it, expect } from 'vitest';
import worker from '../src/index';

// For now, you'll need to do something like this to get a correctly-typed
// `Request` to pass to `worker.fetch()`.
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;

describe('Hello World worker', () => {
it('responds with Hello World! (unit style)', async () => {
const request = new IncomingRequest('http://example.com');
// Create an empty context to pass to `worker.fetch()`.
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
await waitOnExecutionContext(ctx);
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
import { describe, expect, it } from "vitest";
import { Hono } from "hono";
import { openaiAuthMiddleware } from "../src/middleware/openaiAuthMiddleware";
import type { Env } from "../src/types";

const buildApp = () => {
const app = new Hono<{ Bindings: Env }>();
app.use("*", openaiAuthMiddleware());
app.get("/", (c) => c.json({ ok: true }, 200));
return app;
};

describe("openaiAuthMiddleware", () => {
it("returns 401 when Authorization header is missing", async () => {
const app = buildApp();
const response = await app.request("http://localhost/", {}, { OPENAI_API_KEY: "sk-configured" } as Env);

expect(response.status).toBe(401);
expect(await response.json()).toEqual({ error: { message: "Missing Authorization header" } });
});

it("returns 401 for invalid bearer format", async () => {
const app = buildApp();
const response = await app.request(
"http://localhost/",
{ headers: { Authorization: "Token sk-configured" } },
{ OPENAI_API_KEY: "sk-configured" } as Env
);

expect(response.status).toBe(401);
expect(await response.json()).toEqual({
error: { message: "Invalid Authorization header format. Expected: Bearer <token>" }
});
});

it('responds with Hello World! (integration style)', async () => {
const response = await SELF.fetch('https://example.com');
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
it("returns 500 when OPENAI_API_KEY is missing", async () => {
const app = buildApp();
const response = await app.request(
"http://localhost/",
{ headers: { Authorization: "Bearer sk-any" } },
{} as Env
);

expect(response.status).toBe(500);
expect(await response.json()).toEqual({ error: { message: "Server configuration error" } });
});

it("allows the request to proceed with a valid key", async () => {
const app = buildApp();
const response = await app.request(
"http://localhost/",
{ headers: { Authorization: "Bearer sk-valid" } },
{ OPENAI_API_KEY: "sk-valid" } as Env
);

expect(response.status).toBe(200);
expect(await response.json()).toEqual({ ok: true });
});
});