File size: 5,474 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import McpServerFormWrapper from './components/McpServerFormWrapper.svelte';
import { describe, expect, it } from 'vitest';
import { render } from 'vitest-browser-svelte';

const AUTHORIZATION_HEADER = 'Authorization';
const BEARER_PREFIX = 'Bearer ';
const BEARER_PLACEHOLDER = 'Paste token here';

/**
 * Client-side tests for the McpServerForm bearer UI.
 *
 * The dedicated UI only "owns" Authorization headers that already carry a
 * Bearer scheme (heuristic check on the value). Other Authorization values
 * stay in the KV section so the user can still edit them verbatim. Storage
 * always goes through the same custom-headers slot, so a round-trip via this
 * UI produces exactly one `Authorization: Bearer <token>` entry.
 *
 * Equivalent parser coverage lives in `tests/unit/headers.test.ts`.
 */
describe('McpServerForm - Authorization / bearer UI', () => {
	function bearerInput(screen: Awaited<ReturnType<typeof render>>) {
		return screen.locator.getByPlaceholder(BEARER_PLACEHOLDER);
	}

	function capturedHeaders(screen: Awaited<ReturnType<typeof render>>) {
		return screen.getByTestId('captured-headers');
	}

	it('mounts with the bearer input hidden when no auth header is present', async () => {
		const screen = await render(McpServerFormWrapper, { headers: '' });

		await expect.element(screen.getByRole('textbox', { name: /server url/i })).toBeVisible();

		await expect.element(bearerInput(screen)).not.toBeInTheDocument();
	});

	it('toggling Authorization shows the bearer input', async () => {
		const screen = await render(McpServerFormWrapper, { headers: '' });

		await screen.getByRole('switch', { name: /authorization/i }).click();

		await expect.element(bearerInput(screen)).toBeVisible();
	});

	it('typing a token writes the Authorization row with the Bearer prefix prepended', async () => {
		const screen = await render(McpServerFormWrapper, { headers: '' });

		await screen.getByRole('switch', { name: /authorization/i }).click();

		const token = 'super-secret';

		await bearerInput(screen).fill(token);

		const expected = JSON.stringify({ [AUTHORIZATION_HEADER]: `${BEARER_PREFIX}${token}` });

		await expect
			.element(capturedHeaders(screen))
			.toHaveAttribute('data-captured-headers', expected);
	});

	it('pre-existing Bearer header pre-fills the bearer input with the token stripped', async () => {
		const existing = JSON.stringify({
			[AUTHORIZATION_HEADER]: `${BEARER_PREFIX}preexisting`,
			'X-Trace-Id': 'abc'
		});
		const screen = await render(McpServerFormWrapper, { headers: existing });

		await expect.element(bearerInput(screen)).toBeVisible();
		await expect.element(bearerInput(screen)).toHaveValue('preexisting');
	});

	it('non-Bearer Authorization is ignored by the dedicated UI and stays in the KV section', async () => {
		const existing = JSON.stringify({ [AUTHORIZATION_HEADER]: 'Basic czNjcjpwYXNz' });
		const screen = await render(McpServerFormWrapper, { headers: existing });

		await expect.element(bearerInput(screen)).not.toBeInTheDocument();

		const headerKeyInput = screen.getByPlaceholder('Header name');

		await expect.element(headerKeyInput).toBeVisible();
	});

	it('engaging the token UI replaces a non-Bearer Authorization with the Bearer scheme', async () => {
		const existing = JSON.stringify({ [AUTHORIZATION_HEADER]: 'Basic old' });
		const screen = await render(McpServerFormWrapper, { headers: existing });

		await screen.getByRole('switch', { name: /authorization/i }).click();
		await bearerInput(screen).fill('new');

		const expected = JSON.stringify({ [AUTHORIZATION_HEADER]: `${BEARER_PREFIX}new` });

		await expect
			.element(capturedHeaders(screen))
			.toHaveAttribute('data-captured-headers', expected);
	});

	it('toggling Authorization off with no token drops the Bearer row but keeps non-Bearer schemes', async () => {
		const existing = JSON.stringify({ [AUTHORIZATION_HEADER]: `${BEARER_PREFIX}xyz` });
		const screen = await render(McpServerFormWrapper, { headers: existing });

		await screen.getByRole('switch', { name: /authorization/i }).click();

		await expect.element(capturedHeaders(screen)).toHaveAttribute('data-captured-headers', '');
	});

	it('toggling Authorization off when no Bearer row is present leaves headers untouched', async () => {
		const existing = JSON.stringify({ [AUTHORIZATION_HEADER]: 'Basic czNjcjpwYXNz' });
		const screen = await render(McpServerFormWrapper, { headers: existing });

		await screen.getByRole('switch', { name: /authorization/i }).click();
		await screen.getByRole('switch', { name: /authorization/i }).click();

		await expect
			.element(capturedHeaders(screen))
			.toHaveAttribute('data-captured-headers', existing);
	});

	it('clearing the bearer input drops the Authorization row', async () => {
		const existing = JSON.stringify({ [AUTHORIZATION_HEADER]: `${BEARER_PREFIX}xyz` });
		const screen = await render(McpServerFormWrapper, { headers: existing });

		await bearerInput(screen).fill('');

		await expect.element(capturedHeaders(screen)).toHaveAttribute('data-captured-headers', '');
	});

	it('does not surface Bearer Authorization in the KV section even when pre-existing', async () => {
		const existing = JSON.stringify({ [AUTHORIZATION_HEADER]: `${BEARER_PREFIX}xyz` });
		const screen = await render(McpServerFormWrapper, { headers: existing });
		const headerKeyInput = screen.getByPlaceholder('Header name');

		await expect.element(headerKeyInput).not.toBeInTheDocument();
	});
});