---
title: Test /api/chat
description: "Call your chat route handler in Node with a scripted model: sessions, the tool allowlist, cross-site requests and guards, with no provider key."
sidebar:
  label: Test /api/chat
related:
  - /build/add-the-chat
  - /security
---

`createChatHandler` returns a plain `(Request) => Promise<Response>` function, so you can call it in a Node test like
any function. `mockModel` stands in for the model, so no key is needed and nothing leaves the machine. Test the
locks you turned on: they are what keep your key from being used as a free model.

## A complete example

```ts title="chat.test.ts"
import { createChatHandler } from "@webmcp-agents/chat/server";
import { expect, test } from "@webmcp-agents/testing";
import {
  chatRequest,
  mockModel,
  readChatStream,
  toolCall,
  withSession,
} from "@webmcp-agents/testing/server";

import { addToCart } from "./app/add-to-cart";

const model = mockModel([
  toolCall("add_to_cart", { id: "mug-01", qty: 1 }),
]);
const handler = createChatHandler({
  model,
  tools: [addToCart],
  session: true,
});
const hi = { messages: ["hi"] };

test("needs a session", async () => {
  expect((await handler(chatRequest(hi))).status).toBe(401);
  expect(model.calls).toHaveLength(0);
});

test("refuses another site", async () => {
  const evil = { origin: "https://evil.example" };
  expect((await handler(chatRequest(hi, evil))).status).toBe(403);
  expect(model.calls).toHaveLength(0);
});

test("drops tools off the allowlist", async () => {
  const send = await withSession(handler);
  const tools = ["add_to_cart", "delete_everything"];
  const res = await send(chatRequest({ ...hi, tools }));
  const parts = await readChatStream(res);

  expect(parts).toContainEqual(
    expect.objectContaining({ type: "tool-call", name: "add_to_cart" }),
  );
  const offered = model.calls[0]?.tools.map((tool) => tool.name);
  expect(offered).toEqual(["add_to_cart"]);
});
```

`chatRequest(body, options?)` builds a request the way the page sends it (same origin by default; pass `origin` to
play another site). `withSession(handler)` gets a chat session first and adds it to every request it sends.
`readChatStream` reads the streamed answer into parts. `model.calls` records what the model was sent, here the tools
left after the allowlist; a refused request leaves it empty (the model was never called).

## What to cover

- **Sessions**: a request without one is `401` ([Sessions](/docs/security/sessions)).
- **Cross-site requests**: `403` ([Secure the chat](/docs/security)).
- **The tool allowlist**: a tool the page did not register is never offered
  ([Tool allowlist](/docs/security/tool-allowlist)).
- **Guards and limits**: your `guard` answers its own `Response` ([Limits](/docs/security/limits)).

## See also

- [`mockModel`](/docs/reference/testing/mock-model), [`chatRequest`, `readChatStream`, `withSession`](/docs/reference/testing/server-requests)
- [`createChatHandler`](/docs/reference/chat/create-chat-handler)
