@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
ts 57 lines 2.38 KB
Raw
1 import { describe, expect, spyOn, test } from 'bun:test';
2 import { NextRequest, NextResponse } from 'next/server';
3 import { proxy } from '../../proxy';
4
5 function request(pathname: string, accept = 'text/html') {
6 return new NextRequest(`https://docs.composio.dev${pathname}`, {
7 headers: { Accept: accept },
8 });
9 }
10
11 function vary(response: Response) {
12 return (response.headers.get('Vary') ?? '').split(',').map(value => value.trim().toLowerCase());
13 }
14
15 describe('Markdown content negotiation', () => {
16 test('HTML responses vary on Accept', () => {
17 const response = proxy(request('/docs/quickstart'));
18 expect(vary(response)).toContain('accept');
19 });
20
21 for (const accept of ['text/markdown', 'text/plain']) {
22 test(`${accept} rewrites to Markdown and varies on Accept`, () => {
23 const response = proxy(request('/docs/quickstart', accept));
24 expect(vary(response)).toContain('accept');
25 expect(response.headers.get('x-middleware-rewrite')).toBe('https://docs.composio.dev/llms.mdx/docs/quickstart');
26 });
27 }
28
29 test('HTML-only Fern normalization redirects also vary on Accept', () => {
30 const pathname = '/reference/api-reference/files/post-files-upload-request';
31 const html = proxy(request(pathname));
32 expect(html.status).toBe(301);
33 expect(html.headers.get('Location')).toBe('https://docs.composio.dev/reference/api-reference/files/postFilesUploadRequest');
34 expect(vary(html)).toContain('accept');
35
36 const markdown = proxy(request(pathname, 'text/markdown'));
37 expect(markdown.headers.get('x-middleware-rewrite')).toBe(`https://docs.composio.dev/llms.mdx${pathname}`);
38 expect(vary(markdown)).toContain('accept');
39 });
40
41 for (const method of ['next', 'rewrite', 'redirect'] as const) {
42 test(`${method} preserves existing Vary dimensions`, () => {
43 const existing = new NextResponse(null, { headers: { Vary: 'Cookie, RSC, Next-Router-State-Tree' } });
44 const mocked = spyOn(NextResponse, method).mockReturnValue(existing);
45 try {
46 const pathname = method === 'redirect'
47 ? '/reference/api-reference/files/post-files-upload-request'
48 : '/docs/quickstart';
49 const response = proxy(request(pathname, method === 'rewrite' ? 'text/markdown' : 'text/html'));
50 expect(vary(response)).toEqual(['cookie', 'rsc', 'next-router-state-tree', 'accept']);
51 } finally {
52 mocked.mockRestore();
53 }
54 });
55 }
56 });
57