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:
parent
140ac5d17c
commit
76bcdfa2b8
12 changed files with 181 additions and 91 deletions
46
app/portainer/services/axios.test.ts
Normal file
46
app/portainer/services/axios.test.ts
Normal 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();
|
||||
});
|
||||
});
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue