Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/helpers/buildURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import AxiosURLSearchParams from '../helpers/AxiosURLSearchParams.js';
*
* @returns {string} The encoded value.
*/
function encode(val) {
export function encode(val) {
return encodeURIComponent(val)
.replace(/%3A/gi, ':')
.replace(/%24/g, '$')
Expand Down
38 changes: 37 additions & 1 deletion tests/unit/helpers/buildURL.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi } from 'vitest';
import buildURL from '../../../lib/helpers/buildURL.js';
import buildURL, { encode } from '../../../lib/helpers/buildURL.js';

describe('helpers::buildURL', () => {
it('should support null params', () => {
Expand Down Expand Up @@ -124,3 +124,39 @@ describe('helpers::buildURL', () => {
expect(buildURL('/foo', params, customSerializer)).toEqual('/foo?rendered');
});
});

describe('helpers::encode', () => {
it('should be exported as a named export', () => {
expect(typeof encode).toBe('function');
});

it('should leave plain ASCII unchanged', () => {
expect(encode('foo')).toEqual('foo');
});

it('should preserve `:` rather than percent-encoding it', () => {
expect(encode(':')).toEqual(':');
});

it('should preserve `$` rather than percent-encoding it', () => {
expect(encode('$')).toEqual('$');
});

it('should preserve `,` rather than percent-encoding it', () => {
expect(encode(',')).toEqual(',');
});

it('should encode space as `+` (form-style) rather than `%20`', () => {
expect(encode(' ')).toEqual('+');
});

it('should still percent-encode characters outside the preserved set', () => {
expect(encode('a/b')).toEqual('a%2Fb');
expect(encode('a&b')).toEqual('a%26b');
expect(encode('a=b')).toEqual('a%3Db');
});

it('should apply all substitutions together', () => {
expect(encode('a:b$c,d e')).toEqual('a:b$c,d+e');
});
});
Loading