Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1113,17 +1113,17 @@ describe('generateSnippet – encodeUrl setting', () => {
expect(result).not.toContain('%253D');
});

it('should double-encode pre-encoded %20 when encodeUrl is true', () => {
it('should preserve pre-encoded sequences when encodeUrl is true', () => {
const preEncodedUrl = 'https://example.com/api?token=abc%20123%3D%3D&type=test';
const item = {
...makeItem(preEncodedUrl, { encodeUrl: true }),
rawUrl: preEncodedUrl
};
const result = generateSnippet({ language, item, collection: baseCollection, shouldInterpolate: false });
// %20 → %2520 because encodeURIComponent encodes the literal '%' in the already-encoded value
expect(result).toContain('%2520');
// %3D → %253D for the same reason
expect(result).toContain('%253D');
expect(result).toContain('%20');
expect(result).toContain('%3D%3D');
expect(result).not.toContain('%2520');
expect(result).not.toContain('%253D');
});

it('should preserve OData-style paths with parenthesized params when encodeUrl is false', () => {
Expand Down
43 changes: 41 additions & 2 deletions packages/bruno-common/src/utils/url/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,56 @@ describe('encodeUrl', () => {

it('should handle already encoded URLs', () => {
const url = 'https://example.com/api?name=john%20doe&email=john%40example.com';
const expected = 'https://example.com/api?name=john%2520doe&email=john%2540example.com';
const expected = url;
expect(encodeUrl(url)).toBe(expected);
});

it('should handle pipe operator in already encoded URLs', () => {
const url = 'https://example.com/api?filter=status%7Cactive&sort=name%7Casc';
const expected = 'https://example.com/api?filter=status%257Cactive&sort=name%257Casc';
const expected = url;
expect(encodeUrl(url)).toBe(expected);
});
});

describe('already encoded sequence preservation', () => {
it('should preserve %20 and %40 sequences', () => {
const url = 'https://example.com/api?name=john%20doe&email=john%40example.com';
expect(encodeUrl(url)).toBe(url);
});

it('should preserve %2F-heavy values in generic URLs', () => {
const url = 'https://example.com/api?token=abc%2Fdef%2Fghi&other=x';
expect(encodeUrl(url)).toBe(url);
});

it('should preserve encoded pipe sequences', () => {
const url = 'https://example.com/api?filter=status%7Cactive&sort=name%7Casc';
expect(encodeUrl(url)).toBe(url);
});

it('should preserve encoded values while still encoding raw values in the same URL', () => {
const url = 'https://example.com/api?pre=abc%2Fdef&raw=john doe';
const expected = 'https://example.com/api?pre=abc%2Fdef&raw=john%20doe';
expect(encodeUrl(url)).toBe(expected);
});

it('should encode literal percent signs while preserving valid triplets in hybrid values', () => {
const url = 'https://example.com/api?v=50%off%2F';
const expected = 'https://example.com/api?v=50%25off%2F';
expect(encodeUrl(url)).toBe(expected);
});

it('should not throw on malformed percent sequences and should encode stray percent signs', () => {
const trailingPercentUrl = 'https://example.com/api?v=bad%';
const invalidHexUrl = 'https://example.com/api?v=bad%ZZ';

expect(() => encodeUrl(trailingPercentUrl)).not.toThrow();
expect(() => encodeUrl(invalidHexUrl)).not.toThrow();
expect(encodeUrl(trailingPercentUrl)).toBe('https://example.com/api?v=bad%25');
expect(encodeUrl(invalidHexUrl)).toBe('https://example.com/api?v=bad%25ZZ');
});
});

describe('real-world scenarios', () => {
it('should handle API URLs with complex query parameters', () => {
const url = 'https://api.github.com/search/repositories?q=language:javascript&sort=stars&order=desc&per_page=10';
Expand Down
16 changes: 14 additions & 2 deletions packages/bruno-common/src/utils/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,29 @@ interface ExtractQueryParamsOptions {
decode?: boolean;
}

const ENCODED_MARKER = '__BRUNO_ENCODED__';

const smartEncodeComponent = (value: string): string => {
if (!value) return value;

// replace % followed two hex characters with ENCODED_MARKER so they're not re-encoded
const maskedValue = value.replace(/%([0-9a-fA-F]{2})/g, `${ENCODED_MARKER}$1`);
const encodedValue = encodeURIComponent(maskedValue);

return encodedValue.split(ENCODED_MARKER).join('%');
};

function buildQueryString(paramsArray: QueryParam[], { encode = false }: BuildQueryStringOptions = {}): string {
return paramsArray
.filter(({ name }) => typeof name === 'string' && name.trim().length > 0)
.map(({ name, value }) => {
const finalName = encode ? encodeURIComponent(name) : name;
const finalName = encode ? smartEncodeComponent(name) : name;

if (value === undefined) {
return finalName;
}

const finalValue = encode ? encodeURIComponent(value) : value;
const finalValue = encode ? smartEncodeComponent(value) : value;
return `${finalName}=${finalValue}`;
})
.join('&');
Expand Down
4 changes: 2 additions & 2 deletions tests/request/encoding/curl-encoding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test.describe('Code Generation URL Encoding', () => {
await modal.closeButton().waitFor({ state: 'hidden' });
});

test('should double-encode pre-encoded URL (%20 to %2520)', async ({ pageWithUserData: page }) => {
test('should preserve pre-encoded URL (%20 stays %20)', async ({ pageWithUserData: page }) => {
const { sidebar, request, modal } = buildCommonLocators(page);

await openCollection(page, 'encoding-test');
Expand All @@ -36,7 +36,7 @@ test.describe('Code Generation URL Encoding', () => {
await expect(codeEditor).toBeVisible();

const generatedCode = await codeEditor.textContent();
expect(generatedCode).toContain('http://base.source?name=John%2520Doe');
expect(generatedCode).toContain('http://base.source?name=John%20Doe');

await modal.closeButton().click();
await modal.closeButton().waitFor({ state: 'hidden' });
Expand Down
Loading