mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
f2dc449547
The regex used `(:?\/\/|:)` where `:?` was intended to be a non-capturing group `(?:` but instead made the colon an optional literal character. This caused URLs without a colon separator like `http//example.com` to incorrectly match and return `http` as the protocol. Fix the regex to `:(?:\/\/)?` which requires the colon and optionally matches `//`, correctly rejecting malformed URLs. Signed-off-by: Nikita Skovoroda <chalkerx@gmail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Nikita Skovoroda <chalkerx@gmail.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
import { describe, it } from 'vitest';
|
|
import assert from 'assert';
|
|
import utils from '../../lib/utils.js';
|
|
import parseProtocol from '../../lib/helpers/parseProtocol.js';
|
|
|
|
describe('helpers::parseProtocol', () => {
|
|
it('should parse protocol part if it exists', () => {
|
|
utils.forEach(
|
|
{
|
|
'http://username:password@example.com/': 'http',
|
|
'ftp:google.com': 'ftp',
|
|
'sms:+15105550101?body=hello%20there': 'sms',
|
|
'tel:0123456789': 'tel',
|
|
'//google.com': '',
|
|
'google.com': '',
|
|
'admin://etc/default/grub': 'admin',
|
|
'stratum+tcp://server:port': 'stratum+tcp',
|
|
'/api/resource:customVerb': '',
|
|
'https://stackoverflow.com/questions/': 'https',
|
|
'mailto:jsmith@example.com': 'mailto',
|
|
'chrome-extension://1234/<pageName>.html': 'chrome-extension',
|
|
},
|
|
(expectedProtocol, url) => {
|
|
assert.strictEqual(parseProtocol(url), expectedProtocol);
|
|
}
|
|
);
|
|
});
|
|
|
|
it('should not match URLs without a colon separator', () => {
|
|
assert.strictEqual(parseProtocol('http//example.com'), '');
|
|
});
|
|
});
|