Skip to content

Commit edf50d6

Browse files
author
Capacitor+ Bot
committed
chore: sync upstream PR ionic-team#8272 from @maniktyagi04
2 parents bdd6226 + b8fbf3b commit edf50d6

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

core/src/core-plugins.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ const normalizeHttpHeaders = (headers: HttpHeaders = {}): HttpHeaders => {
318318
* @param params A map of url parameters
319319
* @param shouldEncode true if you should encodeURIComponent() the values (true by default)
320320
*/
321-
const buildUrlParams = (params?: HttpParams, shouldEncode = true): string | null => {
321+
export const buildUrlParams = (params?: HttpParams, shouldEncode = true): string | null => {
322322
if (!params) return null;
323323

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

345345
// Remove initial "&" from the reduce
346-
return output.substr(1);
346+
return output.substring(1);
347347
};
348348

349349
/**

core/src/tests/http.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
import { buildUrlParams } from '../core-plugins';
6+
7+
describe('buildUrlParams', () => {
8+
it('should handle array parameters correctly', () => {
9+
const params = {
10+
tags: ['javascript', 'typescript', 'react'],
11+
single: 'value',
12+
};
13+
14+
const result = buildUrlParams(params, true);
15+
16+
// The bug: array values will have a trailing \u0026
17+
// Expected: "tags=javascript\u0026tags=typescript\u0026tags=react\u0026single=value"
18+
// Actual: "tags=javascript\u0026tags=typescript\u0026tags=react\u0026\u0026single=value"
19+
20+
expect(result).not.toContain('\u0026\u0026');
21+
expect(result).toBe('tags=javascript\u0026tags=typescript\u0026tags=react\u0026single=value');
22+
});
23+
24+
it('should remove initial ampersand', () => {
25+
const params = {
26+
key: 'value',
27+
};
28+
29+
const result = buildUrlParams(params, true);
30+
31+
expect(result).toBe('key=value');
32+
expect(result?.[0]).not.toBe('\u0026');
33+
});
34+
});

0 commit comments

Comments
 (0)