n8n-nodes-mcp
Version:
MCP nodes for n8n
208 lines • 8.04 kB
JavaScript
"use strict";
const parseEnvVars = (envString) => {
const env = {};
if (envString) {
const envLines = envString.split('\n');
for (const line of envLines) {
const equalsIndex = line.indexOf('=');
if (equalsIndex > 0) {
const name = line.substring(0, equalsIndex).trim();
const value = line.substring(equalsIndex + 1).trim();
if (name && value !== undefined) {
env[name] = value;
}
}
}
}
return env;
};
const parseHeaders = (headersString) => {
const headers = {};
if (headersString) {
const headerLines = headersString.split('\n');
for (const line of headerLines) {
const equalsIndex = line.indexOf('=');
if (equalsIndex > 0) {
const name = line.substring(0, equalsIndex).trim();
const value = line.substring(equalsIndex + 1).trim();
if (name && value !== undefined) {
headers[name] = value;
}
}
}
}
return headers;
};
describe('McpClient Node - Credentials Parsing', () => {
describe('cmdCredentials.environments parsing', () => {
const testCases = [
{
name: 'Basic Test',
input: 'FOO=bar\nBAZ=qux',
expected: { FOO: 'bar', BAZ: 'qux' },
},
{
name: 'Value with Spaces',
input: 'MY_VAR=hello world',
expected: { MY_VAR: 'hello world' },
},
{
name: 'Value with Commas',
input: 'LIST_OF_THINGS=apple,banana,orange',
expected: { LIST_OF_THINGS: 'apple,banana,orange' },
},
{
name: 'JSON Value',
input: 'JSON_DATA={"key": "value", "number": 123, "nested": {"foo": "bar"}}',
expected: { JSON_DATA: '{"key": "value", "number": 123, "nested": {"foo": "bar"}}' },
},
{
name: 'Equals Sign in Value',
input: 'FORMULA=E=mc^2',
expected: { FORMULA: 'E=mc^2' },
},
{
name: 'Whitespace Trimming',
input: ' KEY_WITH_SPACES = value with spaces \nANOTHER=value',
expected: { KEY_WITH_SPACES: 'value with spaces', ANOTHER: 'value' },
},
{
name: 'Empty Lines and Lines without Equals',
input: 'VALID=true\n\nINVALID_LINE\nANOTHER_VALID=false',
expected: { VALID: 'true', ANOTHER_VALID: 'false' },
},
{
name: 'Empty Input',
input: '',
expected: {},
},
{
name: 'Line with only Equals Sign',
input: '=',
expected: {},
},
{
name: 'Key starting with Equals',
input: '=INVALID_KEY=value',
expected: {},
},
{
name: 'Value is just whitespace',
input: 'KEY_WITH_WHITESPACE_VALUE= ',
expected: { KEY_WITH_WHITESPACE_VALUE: '' },
},
{
name: 'Value can be empty after key',
input: 'EMPTY_VALUE=',
expected: { EMPTY_VALUE: '' },
},
{
name: 'Mixed valid and invalid lines',
input: 'A=1\nB=\n C = 2 \nINVALID\n=startswithEquals\n D = 3 ',
expected: { A: '1', B: '', C: '2', D: '3' },
},
];
testCases.forEach(tc => {
it(`should correctly parse: ${tc.name}`, () => {
const result = parseEnvVars(tc.input);
expect(result).toEqual(tc.expected);
});
});
});
describe('httpCredentials.headers parsing', () => {
const testCases = [
{
name: 'Basic Header',
input: 'Content-Type=application/json\nAuthorization=Bearer token123',
expected: { 'Content-Type': 'application/json', Authorization: 'Bearer token123' },
},
{
name: 'Header Value with Spaces',
input: 'X-Custom-Header=some value with spaces',
expected: { 'X-Custom-Header': 'some value with spaces' },
},
{
name: 'Header Value with Commas',
input: 'Accept-Language=en-US,en;q=0.9,fr;q=0.8',
expected: { 'Accept-Language': 'en-US,en;q=0.9,fr;q=0.8' },
},
{
name: 'JSON in Header Value',
input: 'X-Json-Data={"id": 1, "name": "test", "tags": ["a", "b"]}',
expected: { 'X-Json-Data': '{"id": 1, "name": "test", "tags": ["a", "b"]}' },
},
{
name: 'Equals Sign in Header Value',
input: 'X-Query-Param=filter=some_value',
expected: { 'X-Query-Param': 'filter=some_value' },
},
{
name: 'Whitespace Trimming for Headers',
input: ' X-Padded-Header = padded value \nUser-Agent=N8N',
expected: { 'X-Padded-Header': 'padded value', 'User-Agent': 'N8N' },
},
{
name: 'Empty Lines and Invalid Header Lines',
input: 'Valid-Header=valid\n\nNotAValidHeaderLine\nAnother-Header=true',
expected: { 'Valid-Header': 'valid', 'Another-Header': 'true' },
},
{
name: 'Empty Input for Headers',
input: '',
expected: {},
},
{
name: 'Header value can be empty',
input: 'X-Empty-Value=',
expected: { 'X-Empty-Value': '' }
},
{
name: 'Mixed valid and invalid header lines',
input: 'Header-One=value1\nHeader-Two=\n Invalid Line \n Header-Three = value3 ',
expected: { 'Header-One': 'value1', 'Header-Two': '', 'Header-Three': 'value3' },
},
];
testCases.forEach(tc => {
it(`should correctly parse: ${tc.name}`, () => {
const result = parseHeaders(tc.input);
expect(result).toEqual(tc.expected);
});
});
});
describe('sseCredentials.headers parsing', () => {
const testCases = [
{
name: 'Basic Header (SSE)',
input: 'Content-Type=application/json\nAuthorization=Bearer token456',
expected: { 'Content-Type': 'application/json', Authorization: 'Bearer token456' },
},
{
name: 'Header Value with Spaces (SSE)',
input: 'X-Sse-Custom-Header=sse value with spaces',
expected: { 'X-Sse-Custom-Header': 'sse value with spaces' },
},
{
name: 'JSON in Header Value (SSE)',
input: 'X-Sse-Json-Data={"event": "update", "data": {"value": 42}}',
expected: { 'X-Sse-Json-Data': '{"event": "update", "data": {"value": 42}}' },
},
{
name: 'Empty Input for Headers (SSE)',
input: '',
expected: {},
},
{
name: 'Header value can be empty (SSE)',
input: 'X-Sse-Empty-Value=',
expected: { 'X-Sse-Empty-Value': '' }
},
];
testCases.forEach(tc => {
it(`should correctly parse: ${tc.name}`, () => {
const result = parseHeaders(tc.input);
expect(result).toEqual(tc.expected);
});
});
});
});
//# sourceMappingURL=McpClient.node.test.js.map