n8n/packages/nodes-base/nodes/HttpRequest/test/GenericFunctions.test.ts
n8n-assistant[bot] 2d9a2ec76e
chore: Bundle 2026-W9 (#27532)
Co-authored-by: Matsu <matias.huhta@n8n.io>
Co-authored-by: Dimitri Lavrenük <20122620+dlavrenuek@users.noreply.github.com>
Co-authored-by: Charlie Kolb <charlie@n8n.io>
Co-authored-by: RomanDavydchuk <roman.davydchuk@n8n.io>
Co-authored-by: Jaakko Husso <jaakko@n8n.io>
Co-authored-by: Dawid Myslak <dawid.myslak@gmail.com>
Co-authored-by: Svetoslav Dekov <svetoslav.dekov@n8n.io>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Guillaume Jacquart <jacquart.guillaume@gmail.com>
Co-authored-by: Sandra Zollner <sandra.zollner@n8n.io>
Co-authored-by: Milorad FIlipović <milorad@n8n.io>
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
Co-authored-by: Ricardo Espinoza <ricardo@n8n.io>
2026-03-25 07:51:06 +00:00

122 lines
3.8 KiB
TypeScript

import { mock } from 'jest-mock-extended';
import type { INode } from 'n8n-workflow';
import { getAllowedDomains, updadeQueryParameterConfig } from '../GenericFunctions';
describe('updadeQueryParameterConfig', () => {
describe('version < 4.3 (legacy behavior)', () => {
const updateQueryParam = updadeQueryParameterConfig(4.2);
it('should set simple key-value pairs', () => {
const qs = {};
updateQueryParam(qs, 'key1', 'value1');
expect(qs).toEqual({ key1: 'value1' });
});
it('should overwrite existing values', () => {
const qs = { key1: 'oldValue' };
updateQueryParam(qs, 'key1', 'newValue');
expect(qs).toEqual({ key1: 'newValue' });
});
});
describe('version >= 4.3 (array behavior)', () => {
const updateQueryParam = updadeQueryParameterConfig(4.3);
it('should set initial value when key does not exist', () => {
const qs = {};
updateQueryParam(qs, 'key1', 'value1');
expect(qs).toEqual({ key1: 'value1' });
});
it('should create array when adding second value', () => {
const qs = { key1: 'value1' };
updateQueryParam(qs, 'key1', 'value2');
expect(qs).toEqual({ key1: ['value1', 'value2'] });
});
it('should append to existing array', () => {
const qs = { key1: ['value1', 'value2'] };
updateQueryParam(qs, 'key1', 'value3');
expect(qs).toEqual({ key1: ['value1', 'value2', 'value3'] });
});
it('should handle undefined values correctly', () => {
const qs = {};
updateQueryParam(qs, 'newKey', 'value');
expect(qs).toEqual({ newKey: 'value' });
});
});
describe('version boundary', () => {
it('should use legacy behavior for version 4.2', () => {
const updateQueryParam = updadeQueryParameterConfig(4.2);
const qs = { key: 'first' };
updateQueryParam(qs, 'key', 'second');
expect(qs.key).toBe('second');
});
it('should use array behavior for version 4.3', () => {
const updateQueryParam = updadeQueryParameterConfig(4.3);
const qs = { key: 'first' };
updateQueryParam(qs, 'key', 'second');
expect(qs.key).toEqual(['first', 'second']);
});
});
});
describe('getAllowedDomains', () => {
const node = mock<INode>();
it('should return undefined when allowedHttpRequestDomains is not set', () => {
const result = getAllowedDomains(node, {});
expect(result).toBeUndefined();
});
it('should return undefined when allowedHttpRequestDomains is "all"', () => {
const result = getAllowedDomains(node, { allowedHttpRequestDomains: 'all' });
expect(result).toBeUndefined();
});
it('should throw when allowedHttpRequestDomains is "none"', () => {
expect(() => getAllowedDomains(node, { allowedHttpRequestDomains: 'none' })).toThrow(
'This credential is configured to prevent use within an HTTP Request node',
);
});
it('should throw when allowedHttpRequestDomains is "domains" but allowedDomains is empty', () => {
expect(() =>
getAllowedDomains(node, {
allowedHttpRequestDomains: 'domains',
allowedDomains: '',
}),
).toThrow('No allowed domains specified');
});
it('should throw when allowedHttpRequestDomains is "domains" but allowedDomains is whitespace', () => {
expect(() =>
getAllowedDomains(node, {
allowedHttpRequestDomains: 'domains',
allowedDomains: ' ',
}),
).toThrow('No allowed domains specified');
});
it('should throw when allowedHttpRequestDomains is "domains" but allowedDomains is undefined', () => {
expect(() =>
getAllowedDomains(node, {
allowedHttpRequestDomains: 'domains',
}),
).toThrow('No allowed domains specified');
});
it('should return allowedDomains string when allowedHttpRequestDomains is "domains" with valid domains', () => {
const result = getAllowedDomains(node, {
allowedHttpRequestDomains: 'domains',
allowedDomains: 'example.com, *.api.io',
});
expect(result).toBe('example.com, *.api.io');
});
});