Skip to content

Commit

Permalink
fix: params test (#2395)
Browse files Browse the repository at this point in the history
  • Loading branch information
lohxt1 committed May 31, 2024
1 parent 46df2e9 commit b63ae36
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,7 @@ import StyledWrapper from './StyledWrapper';
import { isValidUrl } from 'utils/url';
import { find, get } from 'lodash';
import { findEnvironmentInCollection } from 'utils/collections';

// Todo: Fix this
// import { interpolate } from '@usebruno/common';
import brunoCommon from '@usebruno/common';
const { interpolate } = brunoCommon;

const interpolateUrl = ({ url, envVars, collectionVariables, processEnvVars }) => {
if (!url || !url.length || typeof url !== 'string') {
return;
}

return interpolate(url, {
...envVars,
...collectionVariables,
process: {
env: {
...processEnvVars
}
}
});
};

// interpolate URL paths
const interpolateUrlPathParams = (url, params) => {
const getInterpolatedBasePath = (pathname, params) => {
return pathname
.split('/')
.map((segment) => {
if (segment.startsWith(':')) {
const pathParamName = segment.slice(1);
const pathParam = params.find((p) => p?.name === pathParamName && p?.type === 'path');
return pathParam ? pathParam.value : segment;
}
return segment;
})
.join('/');
};

let uri;
try {
uri = new URL(url);
} catch (error) {
uri = new URL(`http://${url}`);
}

const basePath = getInterpolatedBasePath(uri.pathname, params);

return `${uri.origin}${basePath}${uri?.search || ''}`;
};
import { interpolateUrl, interpolateUrlPathParams } from 'utils/url/index';

const languages = [
{
Expand Down Expand Up @@ -117,7 +69,7 @@ const GenerateCodeItem = ({ collection, item, onClose }) => {
const requestUrl =
get(item, 'draft.request.url') !== undefined ? get(item, 'draft.request.url') : get(item, 'request.url');

// interpolate the query params
// interpolate the url
const interpolatedUrl = interpolateUrl({
url: requestUrl,
envVars,
Expand Down
52 changes: 52 additions & 0 deletions packages/bruno-app/src/utils/url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import each from 'lodash/each';
import filter from 'lodash/filter';
import find from 'lodash/find';

import brunoCommon from '@usebruno/common';
const { interpolate } = brunoCommon;

const hasLength = (str) => {
if (!str || !str.length) {
return false;
Expand Down Expand Up @@ -103,3 +106,52 @@ export const isValidUrl = (url) => {
return false;
}
};

export const interpolateUrl = ({ url, envVars, collectionVariables, processEnvVars }) => {
if (!url || !url.length || typeof url !== 'string') {
return;
}

return interpolate(url, {
...envVars,
...collectionVariables,
process: {
env: {
...processEnvVars
}
}
});
};

export const interpolateUrlPathParams = (url, params) => {
const getInterpolatedBasePath = (pathname, params) => {
return pathname
.split('/')
.map((segment) => {
if (segment.startsWith(':')) {
const pathParamName = segment.slice(1);
const pathParam = params.find((p) => p?.name === pathParamName && p?.type === 'path');
return pathParam ? pathParam.value : segment;
}
return segment;
})
.join('/');
};

let uri;

if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = `http://${url}`;
}

try {
uri = new URL(url);
} catch (error) {
// if the URL is invalid, return the URL as is
return url;
}

const basePath = getInterpolatedBasePath(uri.pathname, params);

return `${uri.origin}${basePath}${uri?.search || ''}`;
};
72 changes: 71 additions & 1 deletion packages/bruno-app/src/utils/url/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseQueryParams, splitOnFirst, parsePathParams } from './index';
import { parseQueryParams, splitOnFirst, parsePathParams, interpolateUrl, interpolateUrlPathParams } from './index';

describe('Url Utils - parseQueryParams', () => {
it('should parse query - case 1', () => {
Expand Down Expand Up @@ -122,3 +122,73 @@ describe('Url Utils - splitOnFirst', () => {
expect(params).toEqual(['a=1', 'b=2']);
});
});

describe('Url Utils - interpolateUrl, interpolateUrlPathParams', () => {
it('should interpolate url correctly', () => {
const url = '{{host}}/api/:id/path?foo={{foo}}&bar={{bar}}&baz={{process.env.baz}}';
const expectedUrl = 'https://example.com/api/:id/path?foo=foo_value&bar=bar_value&baz=baz_value';

const envVars = { host: 'https://example.com', foo: 'foo_value' };
const collectionVariables = { bar: 'bar_value' };
const processEnvVars = { baz: 'baz_value' };

const result = interpolateUrl({ url, envVars, collectionVariables, processEnvVars });

expect(result).toEqual(expectedUrl);
});

it('should interpolate path params correctly', () => {
const url = 'https://example.com/api/:id/path';
const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }];
const expectedUrl = 'https://example.com/api/123/path';

const result = interpolateUrlPathParams(url, params);

expect(result).toEqual(expectedUrl);
});

it('should interpolate url and path params correctly', () => {
const url = '{{host}}/api/:id/path?foo={{foo}}&bar={{bar}}&baz={{process.env.baz}}';
const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }];
const expectedUrl = 'https://example.com/api/123/path?foo=foo_value&bar=bar_value&baz=baz_value';

const envVars = { host: 'https://example.com', foo: 'foo_value' };
const collectionVariables = { bar: 'bar_value' };
const processEnvVars = { baz: 'baz_value' };

const intermediateResult = interpolateUrl({ url, envVars, collectionVariables, processEnvVars });
const result = interpolateUrlPathParams(intermediateResult, params);

expect(result).toEqual(expectedUrl);
});

it('should handle empty params', () => {
const url = 'https://example.com/api';
const params = [];
const expectedUrl = 'https://example.com/api';

const result = interpolateUrlPathParams(url, params);

expect(result).toEqual(expectedUrl);
});

it('should handle invalid URL, case 1', () => {
const url = 'example.com/api/:id';
const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }];
const expectedUrl = 'http://example.com/api/123';

const result = interpolateUrlPathParams(url, params);

expect(result).toEqual(expectedUrl);
});

it('should handle invalid URL, case 2', () => {
const url = 'http://1.1.1.1:3000:id';
const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }];
const expectedUrl = 'http://1.1.1.1:3000:id';

const result = interpolateUrlPathParams(url, params);

expect(result).toEqual(expectedUrl);
});
});
45 changes: 0 additions & 45 deletions packages/bruno-toml/tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,3 @@ describe('bruno toml', () => {
});
});
});
describe('joinPathUrl', () => {
it('should join path and query params correctly', () => {
const url = 'https://example.com/api/:id';
const params = [
{ name: 'id', type: 'path', enabled: true, value: '123' },
{ name: 'sort', type: 'query', enabled: true, value: 'asc' },
{ name: 'filter', type: 'query', enabled: true, value: 'active' }
];
const expectedUrl = 'https://example.com/api/123?sort=asc&filter=active';

const result = joinPathUrl(url, params);

expect(result).toEqual(expectedUrl);
});

it('should handle empty path and query params', () => {
const url = 'https://example.com/api';
const params = [];
const expectedUrl = 'https://example.com/api';

const result = joinPathUrl(url, params);

expect(result).toEqual(expectedUrl);
});

it('should handle empty query params', () => {
const url = 'https://example.com/api/:id';
const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }];
const expectedUrl = 'https://example.com/api/123';

const result = joinPathUrl(url, params);

expect(result).toEqual(expectedUrl);
});

it('should handle invalid URL', () => {
const url = 'example.com/api/:id';
const params = [{ name: 'id', type: 'path', enabled: true, value: '123' }];
const expectedUrl = 'http://example.com/api/123';

const result = joinPathUrl(url, params);

expect(result).toEqual(expectedUrl);
});
});

0 comments on commit b63ae36

Please sign in to comment.