|
| 1 | +/** |
| 2 | + * Cookie management tests — add_cookie, get_cookies, delete_cookie. |
| 3 | + * |
| 4 | + * Cookies require an HTTP domain (file:// URLs don't support cookies), |
| 5 | + * so we spin up a tiny local HTTP server for the duration of the suite. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, before, after, beforeEach } from 'node:test'; |
| 9 | +import assert from 'node:assert/strict'; |
| 10 | +import http from 'node:http'; |
| 11 | +import fs from 'node:fs'; |
| 12 | +import path from 'node:path'; |
| 13 | +import { fileURLToPath } from 'node:url'; |
| 14 | +import { McpClient, getResponseText } from './mcp-client.mjs'; |
| 15 | + |
| 16 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 17 | + |
| 18 | +function startServer() { |
| 19 | + return new Promise((resolve, reject) => { |
| 20 | + const html = fs.readFileSync(path.join(__dirname, 'fixtures', 'cookies.html'), 'utf-8'); |
| 21 | + const server = http.createServer((req, res) => { |
| 22 | + res.writeHead(200, { 'Content-Type': 'text/html' }); |
| 23 | + res.end(html); |
| 24 | + }); |
| 25 | + server.once('error', reject); |
| 26 | + server.listen(0, '127.0.0.1', () => { |
| 27 | + const { port } = server.address(); |
| 28 | + resolve({ server, url: `http://127.0.0.1:${port}` }); |
| 29 | + }); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +describe('Cookie Management', () => { |
| 34 | + let client; |
| 35 | + let httpServer; |
| 36 | + let baseUrl; |
| 37 | + |
| 38 | + before(async () => { |
| 39 | + const srv = await startServer(); |
| 40 | + httpServer = srv.server; |
| 41 | + baseUrl = srv.url; |
| 42 | + |
| 43 | + client = new McpClient(); |
| 44 | + await client.start(); |
| 45 | + await client.callTool('start_browser', { |
| 46 | + browser: 'chrome', |
| 47 | + options: { headless: true, arguments: ['--no-sandbox', '--disable-dev-shm-usage'] }, |
| 48 | + }); |
| 49 | + await client.callTool('navigate', { url: baseUrl }); |
| 50 | + }); |
| 51 | + |
| 52 | + after(async () => { |
| 53 | + try { await client.callTool('close_session'); } catch { /* ignore */ } |
| 54 | + await client.stop(); |
| 55 | + await new Promise((resolve) => httpServer.close(resolve)); |
| 56 | + }); |
| 57 | + |
| 58 | + beforeEach(async () => { |
| 59 | + await client.callTool('delete_cookie', {}); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('add_cookie', () => { |
| 63 | + it('should add a cookie and verify it was set', async () => { |
| 64 | + const result = await client.callTool('add_cookie', { |
| 65 | + name: 'test_cookie', |
| 66 | + value: 'hello123', |
| 67 | + }); |
| 68 | + assert.ok(getResponseText(result).includes('Cookie "test_cookie" added')); |
| 69 | + |
| 70 | + const getResult = await client.callTool('get_cookies', { name: 'test_cookie' }); |
| 71 | + const cookie = JSON.parse(getResponseText(getResult)); |
| 72 | + assert.equal(cookie.name, 'test_cookie'); |
| 73 | + assert.equal(cookie.value, 'hello123'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should respect optional properties', async () => { |
| 77 | + await client.callTool('add_cookie', { |
| 78 | + name: 'opts_cookie', |
| 79 | + value: 'secret', |
| 80 | + path: '/', |
| 81 | + httpOnly: true, |
| 82 | + }); |
| 83 | + |
| 84 | + const getResult = await client.callTool('get_cookies', { name: 'opts_cookie' }); |
| 85 | + const cookie = JSON.parse(getResponseText(getResult)); |
| 86 | + assert.equal(cookie.path, '/'); |
| 87 | + assert.equal(cookie.httpOnly, true); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('get_cookies', () => { |
| 92 | + it('should return all cookies as an array', async () => { |
| 93 | + await client.callTool('add_cookie', { name: 'a', value: '1' }); |
| 94 | + await client.callTool('add_cookie', { name: 'b', value: '2' }); |
| 95 | + |
| 96 | + const result = await client.callTool('get_cookies', {}); |
| 97 | + const cookies = JSON.parse(getResponseText(result)); |
| 98 | + assert.ok(Array.isArray(cookies)); |
| 99 | + const names = cookies.map(c => c.name); |
| 100 | + assert.ok(names.includes('a') && names.includes('b')); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should return empty array when no cookies exist', async () => { |
| 104 | + const result = await client.callTool('get_cookies', {}); |
| 105 | + const cookies = JSON.parse(getResponseText(result)); |
| 106 | + assert.equal(cookies.length, 0); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should error when a named cookie is not found', async () => { |
| 110 | + const result = await client.callTool('get_cookies', { name: 'ghost' }); |
| 111 | + assert.strictEqual(result.isError, true); |
| 112 | + assert.ok(getResponseText(result).includes('not found')); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('delete_cookie', () => { |
| 117 | + it('should delete a specific cookie and leave others', async () => { |
| 118 | + await client.callTool('add_cookie', { name: 'delete_me', value: 'bye' }); |
| 119 | + await client.callTool('add_cookie', { name: 'keep_me', value: 'stay' }); |
| 120 | + |
| 121 | + await client.callTool('delete_cookie', { name: 'delete_me' }); |
| 122 | + |
| 123 | + const gone = await client.callTool('get_cookies', { name: 'delete_me' }); |
| 124 | + assert.strictEqual(gone.isError, true); |
| 125 | + |
| 126 | + const kept = JSON.parse(getResponseText( |
| 127 | + await client.callTool('get_cookies', { name: 'keep_me' }) |
| 128 | + )); |
| 129 | + assert.equal(kept.name, 'keep_me'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should delete all cookies when no name is provided', async () => { |
| 133 | + await client.callTool('add_cookie', { name: 'x', value: '1' }); |
| 134 | + await client.callTool('add_cookie', { name: 'y', value: '2' }); |
| 135 | + |
| 136 | + const result = await client.callTool('delete_cookie', {}); |
| 137 | + assert.ok(getResponseText(result).includes('All cookies deleted')); |
| 138 | + |
| 139 | + const cookies = JSON.parse(getResponseText( |
| 140 | + await client.callTool('get_cookies', {}) |
| 141 | + )); |
| 142 | + assert.equal(cookies.length, 0); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
0 commit comments