fix: fixed issue where postman collection import was failing when the filename had / or ? chars (#147)

This commit is contained in:
Anoop M D 2023-04-20 11:46:55 +05:30
parent 86094cc054
commit b6528062f0
3 changed files with 34 additions and 1 deletions

View File

@ -49,3 +49,15 @@ export const safeStringifyJSON = (obj, indent=false) => {
return obj;
}
}
// Remove any characters that are not alphanumeric, spaces, hyphens, or underscores
export const normalizeFileName = (name) => {
if (!name) {
return name;
}
const validChars = /[^\w\s-]/g;
const formattedName = name.replace(validChars, '-');
return formattedName;
}

View File

@ -0,0 +1,19 @@
const { describe, it, expect } = require("@jest/globals");
import { normalizeFileName } from './index';
describe("common utils", () => {
describe("normalizeFileName", () => {
it("should remove special characters", () => {
expect(normalizeFileName("hello world")).toBe("hello world");
expect(normalizeFileName("hello-world")).toBe("hello-world");
expect(normalizeFileName("hello_world")).toBe("hello_world");
expect(normalizeFileName("hello_world-")).toBe("hello_world-");
expect(normalizeFileName("hello_world-123")).toBe("hello_world-123");
expect(normalizeFileName("hello_world-123!@#$%^&*()")).toBe("hello_world-123----------");
expect(normalizeFileName("hello_world?")).toBe("hello_world-");
expect(normalizeFileName("foo/bar/")).toBe("foo-bar-");
expect(normalizeFileName("foo\\bar\\")).toBe("foo-bar-");
});
});
});

View File

@ -3,7 +3,7 @@ import each from 'lodash/each';
import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep';
import { uuid } from 'utils/common';
import { uuid, normalizeFileName } from 'utils/common';
import { isItemARequest } from 'utils/collections';
import { collectionSchema } from '@usebruno/schema';
import { BrunoError } from 'utils/common/error';
@ -63,6 +63,8 @@ export const updateUidsInCollection = (_collection) => {
export const transformItemsInCollection = (collection) => {
const transformItems = (items = []) => {
each(items, (item) => {
item.name = normalizeFileName(item.name);
if (['http', 'graphql'].includes(item.type)) {
item.type = `${item.type}-request`;
if(item.request.query) {