|
1 | 1 | /** |
2 | 2 | * Tests for keyless request errors |
3 | 3 | * |
4 | | - * The API links every keyless prompt to signup tagged `utm_medium=api`. The CLI |
5 | | - * must retag that link as `cli` so signups started from the CLI are attributed |
6 | | - * to it. |
| 4 | + * The API links every keyless prompt to the caller's own opaque signup link, |
| 5 | + * https://firecrawl.dev/k/<id>, which the site resolves to CLI attribution when |
| 6 | + * the request came from the CLI. The CLI prints that link unchanged and tells |
| 7 | + * the API it is the CLI with X-Origin, including on requests without a body. |
7 | 8 | */ |
8 | 9 |
|
9 | 10 | import { describe, it, expect, vi, afterEach } from 'vitest'; |
10 | | -import { |
11 | | - keylessGet, |
12 | | - keylessRequest, |
13 | | - withCliSignupTag, |
14 | | -} from '../../utils/client'; |
| 11 | +import { keylessGet, keylessRequest } from '../../utils/client'; |
15 | 12 |
|
16 | | -const API_LIMIT_MESSAGE = `You've hit Firecrawl's keyless free tier rate limit. To continue now, create a free API key at https://www.firecrawl.dev/signin?utm_source=keyless&utm_medium=api |
| 13 | +const OWN_SIGNUP_URL = 'https://firecrawl.dev/k/7fq2xab9'; |
| 14 | + |
| 15 | +const API_LIMIT_MESSAGE = `You've hit Firecrawl's keyless free tier rate limit. To continue now, create a free API key at ${OWN_SIGNUP_URL} |
17 | 16 |
|
18 | 17 | Then authenticate with: |
19 | 18 | Authorization: Bearer YOUR_API_KEY`; |
20 | 19 |
|
21 | | -const CLI_SIGNUP_URL = |
22 | | - 'https://www.firecrawl.dev/signin?utm_source=keyless&utm_medium=cli'; |
| 20 | +// Before the /k links, the API sent a UTM-tagged link. An API still sending it |
| 21 | +// must not be rewritten into something else. |
| 22 | +const LEGACY_LIMIT_MESSAGE = |
| 23 | + "You've hit Firecrawl's keyless free tier rate limit. To continue now, create a free API key at https://www.firecrawl.dev/signin?utm_source=keyless&utm_medium=api"; |
23 | 24 |
|
24 | 25 | function stubFetch(status: number, body: unknown) { |
25 | | - vi.stubGlobal( |
26 | | - 'fetch', |
27 | | - vi.fn(async () => new Response(JSON.stringify(body), { status })) |
| 26 | + const fetchMock = vi.fn( |
| 27 | + async (_url: string, _init?: RequestInit) => |
| 28 | + new Response(JSON.stringify(body), { status }) |
28 | 29 | ); |
| 30 | + vi.stubGlobal('fetch', fetchMock); |
| 31 | + return fetchMock; |
29 | 32 | } |
30 | 33 |
|
31 | | -describe('withCliSignupTag', () => { |
32 | | - it('retags the keyless signup link as cli', () => { |
33 | | - const message = withCliSignupTag(API_LIMIT_MESSAGE); |
34 | | - |
35 | | - expect(message).toContain(CLI_SIGNUP_URL); |
36 | | - expect(message).not.toContain('utm_medium=api'); |
37 | | - }); |
38 | | - |
39 | | - it('leaves messages without the keyless signup link unchanged', () => { |
40 | | - expect(withCliSignupTag('Firecrawl request failed (HTTP 500)')).toBe( |
41 | | - 'Firecrawl request failed (HTTP 500)' |
42 | | - ); |
43 | | - }); |
44 | | -}); |
45 | | - |
46 | 34 | describe('keyless requests', () => { |
47 | 35 | afterEach(() => { |
48 | 36 | vi.unstubAllGlobals(); |
49 | 37 | }); |
50 | 38 |
|
51 | | - it('reports the keyless limit with the cli signup link', async () => { |
52 | | - stubFetch(429, { success: false, error: API_LIMIT_MESSAGE }); |
| 39 | + it('reports the keyless limit with the API-issued signup link unchanged', async () => { |
| 40 | + stubFetch(429, { |
| 41 | + success: false, |
| 42 | + error: API_LIMIT_MESSAGE, |
| 43 | + signup_url: OWN_SIGNUP_URL, |
| 44 | + }); |
53 | 45 |
|
54 | 46 | await expect( |
55 | 47 | keylessRequest('/v2/scrape', { url: 'https://example.com' }) |
56 | | - ).rejects.toThrow(CLI_SIGNUP_URL); |
| 48 | + ).rejects.toThrow(API_LIMIT_MESSAGE); |
57 | 49 | }); |
58 | 50 |
|
59 | | - it('reports the keyless limit on GET requests with the cli signup link', async () => { |
| 51 | + it('reports the keyless limit on GET requests with the API-issued link', async () => { |
60 | 52 | stubFetch(429, { success: false, error: API_LIMIT_MESSAGE }); |
61 | 53 |
|
62 | 54 | await expect(keylessGet('/v2/research/search?q=test')).rejects.toThrow( |
63 | | - CLI_SIGNUP_URL |
| 55 | + OWN_SIGNUP_URL |
64 | 56 | ); |
65 | 57 | }); |
| 58 | + |
| 59 | + it('no longer rewrites a legacy UTM link', async () => { |
| 60 | + stubFetch(429, { success: false, error: LEGACY_LIMIT_MESSAGE }); |
| 61 | + |
| 62 | + await expect( |
| 63 | + keylessRequest('/v2/scrape', { url: 'https://example.com' }) |
| 64 | + ).rejects.toThrow(LEGACY_LIMIT_MESSAGE); |
| 65 | + }); |
| 66 | + |
| 67 | + it('identifies the CLI with X-Origin on POST and GET requests', async () => { |
| 68 | + const fetchMock = stubFetch(200, { success: true }); |
| 69 | + |
| 70 | + await keylessRequest('/v2/scrape', { url: 'https://example.com' }); |
| 71 | + await keylessGet('/v2/research/search?q=test'); |
| 72 | + |
| 73 | + for (const [, init] of fetchMock.mock.calls) { |
| 74 | + const headers = init?.headers as Record<string, string>; |
| 75 | + expect(headers['X-Origin']).toBe('cli'); |
| 76 | + expect(headers.Authorization).toBeUndefined(); |
| 77 | + } |
| 78 | + }); |
66 | 79 | }); |
0 commit comments