|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { test, expect } from './fixtures'; |
| 18 | + |
| 19 | +test('headers tool requires capability', async ({ client, startClient }) => { |
| 20 | + const { tools } = await client.listTools(); |
| 21 | + expect(tools.map(tool => tool.name)).not.toContain('browser_set_headers'); |
| 22 | + |
| 23 | + const { client: headersClient } = await startClient({ args: ['--caps=headers'] }); |
| 24 | + const headersToolList = await headersClient.listTools(); |
| 25 | + expect(headersToolList.tools.map(tool => tool.name)).toContain('browser_set_headers'); |
| 26 | +}); |
| 27 | + |
| 28 | +test('browser_set_headers rejects empty input', async ({ startClient }) => { |
| 29 | + const { client } = await startClient({ args: ['--caps=headers'] }); |
| 30 | + |
| 31 | + const response = await client.callTool({ |
| 32 | + name: 'browser_set_headers', |
| 33 | + arguments: { headers: {} }, |
| 34 | + }); |
| 35 | + |
| 36 | + expect(response).toHaveResponse({ |
| 37 | + isError: true, |
| 38 | + result: 'Please provide at least one header to set.', |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +test('browser_set_headers rejects header names without characters', async ({ startClient }) => { |
| 43 | + const { client } = await startClient({ args: ['--caps=headers'] }); |
| 44 | + |
| 45 | + const response = await client.callTool({ |
| 46 | + name: 'browser_set_headers', |
| 47 | + arguments: { headers: { ' ': 'value' } }, |
| 48 | + }); |
| 49 | + |
| 50 | + expect(response).toHaveResponse({ |
| 51 | + isError: true, |
| 52 | + result: 'Header names must be non-empty strings.', |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +test('browser_set_headers persists headers across navigations', async ({ startClient, server }) => { |
| 57 | + server.setContent('/first', '<title>First</title>', 'text/html'); |
| 58 | + server.setContent('/second', '<title>Second</title>', 'text/html'); |
| 59 | + |
| 60 | + const { client } = await startClient({ args: ['--caps=headers'] }); |
| 61 | + |
| 62 | + expect(await client.callTool({ |
| 63 | + name: 'browser_set_headers', |
| 64 | + arguments: { |
| 65 | + headers: { 'X-Tenant-ID': 'tenant-123' }, |
| 66 | + }, |
| 67 | + })).toHaveResponse({ |
| 68 | + result: 'Configured 1 header for this session.', |
| 69 | + }); |
| 70 | + |
| 71 | + const firstRequestPromise = server.waitForRequest('/first'); |
| 72 | + await client.callTool({ |
| 73 | + name: 'browser_navigate', |
| 74 | + arguments: { url: `${server.PREFIX}/first` }, |
| 75 | + }); |
| 76 | + const firstRequest = await firstRequestPromise; |
| 77 | + expect(firstRequest.headers['x-tenant-id']).toBe('tenant-123'); |
| 78 | + |
| 79 | + const secondRequestPromise = server.waitForRequest('/second'); |
| 80 | + await client.callTool({ |
| 81 | + name: 'browser_navigate', |
| 82 | + arguments: { url: `${server.PREFIX}/second` }, |
| 83 | + }); |
| 84 | + const secondRequest = await secondRequestPromise; |
| 85 | + expect(secondRequest.headers['x-tenant-id']).toBe('tenant-123'); |
| 86 | +}); |
| 87 | + |
| 88 | +test('browser_set_headers sends headers with requests', async ({ startClient, server }) => { |
| 89 | + server.setContent('/page', '<title>Page</title>', 'text/html'); |
| 90 | + |
| 91 | + const { client } = await startClient({ args: ['--caps=headers'] }); |
| 92 | + |
| 93 | + expect(await client.callTool({ |
| 94 | + name: 'browser_set_headers', |
| 95 | + arguments: { |
| 96 | + headers: { 'X-Custom-Header': 'custom-value' }, |
| 97 | + }, |
| 98 | + })).toHaveResponse({ |
| 99 | + result: 'Configured 1 header for this session.', |
| 100 | + }); |
| 101 | + |
| 102 | + const requestPromise = server.waitForRequest('/page'); |
| 103 | + await client.callTool({ |
| 104 | + name: 'browser_navigate', |
| 105 | + arguments: { url: `${server.PREFIX}/page` }, |
| 106 | + }); |
| 107 | + |
| 108 | + const request = await requestPromise; |
| 109 | + expect(request.headers['x-custom-header']).toBe('custom-value'); |
| 110 | +}); |
0 commit comments