Open History to see the last commit
1import { test, expect } from './fixtures'
2import type { APIRequestContext } from '@playwright/test'
3
4type WireResult = {
5 ok: boolean
6 error?: string
7 text?: string
8 wire?: {
9 authorization: string | null
10 xApiKey: string | null
11 anthropicBeta: string | null
12 xApp: string | null
13 system?: unknown
14 }
15}
16
17async function run(
18 request: APIRequestContext,
19 mode: 'api-key' | 'bearer' | 'oauth',
20) {
21 const res = await request.post(`/api/anthropic-auth-wire?mode=${mode}`)
22 expect(res.ok()).toBe(true)
23 const result = (await res.json()) as WireResult
24 expect(result.error ?? null).toBeNull()
25 expect(result.ok).toBe(true)
26 expect(result.text).toBe('Hello from aimock')
27 return result.wire
28}
29
30/**
31 * The route records the headers and `system` field of the request that the
32 * Anthropic SDK sends to aimock. The credentials are fake.
33 */
34test.describe('anthropic — auth on the wire', () => {
35 test("auth: 'api-key' sends x-api-key and no Authorization", async ({
36 request,
37 }) => {
38 const wire = await run(request, 'api-key')
39 expect(wire).toMatchObject({
40 authorization: null,
41 xApiKey: 'sk-ant-e2e-test-dummy-key',
42 anthropicBeta: null,
43 xApp: null,
44 })
45 })
46
47 test("auth: 'bearer' sends Authorization: Bearer and no x-api-key", async ({
48 request,
49 }) => {
50 const wire = await run(request, 'bearer')
51 expect(wire).toMatchObject({
52 authorization: 'Bearer e2e-dummy-bearer-token',
53 xApiKey: null,
54 anthropicBeta: null,
55 xApp: null,
56 })
57 })
58
59 test("auth: 'oauth' adds the Claude Code betas, headers, and system block", async ({
60 request,
61 }) => {
62 const wire = await run(request, 'oauth')
63 expect(wire).toMatchObject({
64 authorization: 'Bearer e2e-dummy-bearer-token',
65 xApiKey: null,
66 xApp: 'cli',
67 })
68 expect(wire?.anthropicBeta?.split(',')).toEqual([
69 'claude-code-20250219',
70 'oauth-2025-04-20',
71 ])
72 // The default prompt cache marks the system blocks, so the identity block
73 // has the automatic cache marker too.
74 expect(wire?.system).toEqual([
75 {
76 type: 'text',
77 text: "You are Claude Code, Anthropic's official CLI for Claude.",
78 cache_control: { type: 'ephemeral' },
79 },
80 ])
81 })
82})