Skip to content
Open
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
6 changes: 3 additions & 3 deletions core/src/core-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ const normalizeHttpHeaders = (headers: HttpHeaders = {}): HttpHeaders => {
* @param params A map of url parameters
* @param shouldEncode true if you should encodeURIComponent() the values (true by default)
*/
const buildUrlParams = (params?: HttpParams, shouldEncode = true): string | null => {
export const buildUrlParams = (params?: HttpParams, shouldEncode = true): string | null => {
if (!params) return null;

const output = Object.entries(params).reduce((accumulator, entry) => {
Expand All @@ -333,7 +333,7 @@ const buildUrlParams = (params?: HttpParams, shouldEncode = true): string | null
item += `${key}=${encodedValue}&`;
});
// last character will always be "&" so slice it off
item.slice(0, -1);
item = item.slice(0, -1);
} else {
encodedValue = shouldEncode ? encodeURIComponent(value) : value;
item = `${key}=${encodedValue}`;
Expand All @@ -343,7 +343,7 @@ const buildUrlParams = (params?: HttpParams, shouldEncode = true): string | null
}, '');

// Remove initial "&" from the reduce
return output.substr(1);
return output.substring(1);
};

/**
Expand Down
34 changes: 34 additions & 0 deletions core/src/tests/http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @jest-environment jsdom
*/

import { buildUrlParams } from '../core-plugins';

describe('buildUrlParams', () => {
it('should handle array parameters correctly', () => {
const params = {
tags: ['javascript', 'typescript', 'react'],
single: 'value',
};

const result = buildUrlParams(params, true);

// The bug: array values will have a trailing \u0026
// Expected: "tags=javascript\u0026tags=typescript\u0026tags=react\u0026single=value"
// Actual: "tags=javascript\u0026tags=typescript\u0026tags=react\u0026\u0026single=value"

expect(result).not.toContain('\u0026\u0026');
expect(result).toBe('tags=javascript\u0026tags=typescript\u0026tags=react\u0026single=value');
});

it('should remove initial ampersand', () => {
const params = {
key: 'value',
};

const result = buildUrlParams(params, true);

expect(result).toBe('key=value');
expect(result?.[0]).not.toBe('\u0026');
});
});