1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-08 07:15:23 +02:00

fix(edge/templates): fix issues [EE-6328] (#10656)

This commit is contained in:
Chaim Lev-Ari 2023-11-27 09:56:15 +02:00 committed by GitHub
parent 140ac5d17c
commit 76bcdfa2b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 181 additions and 91 deletions

View file

@ -0,0 +1,46 @@
import { json2formData } from './axios';
describe('json2formData', () => {
it('should handle undefined and null values', () => {
const json = { key1: undefined, key2: null };
const formData = json2formData(json);
expect(formData.has('key1')).toBe(false);
expect(formData.has('key2')).toBe(false);
});
it('should handle File instances', () => {
const file = new File([''], 'filename');
const json = { key: file };
const formData = json2formData(json);
expect(formData.get('key')).toBe(file);
});
it('should handle arrays', () => {
const json = { key: [1, 2, 3] };
const formData = json2formData(json);
expect(formData.get('key')).toBe('[1,2,3]');
});
it('should handle objects', () => {
const json = { key: { subkey: 'value' } };
const formData = json2formData(json);
expect(formData.get('key')).toBe('{"subkey":"value"}');
});
it('should handle other types of values', () => {
const json = { key1: 'value', key2: 123, key3: true };
const formData = json2formData(json);
expect(formData.get('key1')).toBe('value');
expect(formData.get('key2')).toBe('123');
expect(formData.get('key3')).toBe('true');
});
it('should fail when handling circular references', () => {
const circularReference = { self: undefined };
// @ts-expect-error test
circularReference.self = circularReference;
const json = { key: circularReference };
expect(() => json2formData(json)).toThrow();
});
});

View file

@ -159,12 +159,22 @@ export function json2formData(json: Record<string, unknown>) {
return;
}
if (value instanceof File) {
formData.append(key, value);
return;
}
if (Array.isArray(value)) {
formData.append(key, arrayToJson(value));
return;
}
formData.append(key, value as string);
if (typeof value === 'object') {
formData.append(key, JSON.stringify(value));
return;
}
formData.append(key, value.toString());
});
return formData;