redux-form-validators
Version:
Simple validations with redux-form / react-final-form
479 lines (333 loc) • 16.1 kB
JavaScript
"use strict";
var _assert = _interopRequireDefault(require("assert"));
var _index = _interopRequireWildcard(require("../index"));
var _helper = _interopRequireDefault(require("./helper"));
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var ERROR_ID = 'form.errors.url';
function test(value, params) {
return (0, _helper["default"])((0, _index.url)(params)(value));
}
describe('Validator: url', function () {
it('should be invalid when `value` is not a valid url', function () {
_assert["default"].strictEqual(ERROR_ID, test(''));
_assert["default"].strictEqual(ERROR_ID, test('http://'));
_assert["default"].strictEqual(ERROR_ID, test('http://.'));
_assert["default"].strictEqual(ERROR_ID, test('http://..'));
_assert["default"].strictEqual(ERROR_ID, test('http://../'));
_assert["default"].strictEqual(ERROR_ID, test('http://?'));
_assert["default"].strictEqual(ERROR_ID, test('http://??'));
_assert["default"].strictEqual(ERROR_ID, test('http://??/'));
_assert["default"].strictEqual(ERROR_ID, test('http://#'));
_assert["default"].strictEqual(ERROR_ID, test('http://##'));
_assert["default"].strictEqual(ERROR_ID, test('http://##/'));
_assert["default"].strictEqual(ERROR_ID, test('http://foo.bar?q=Spaces should be encoded'));
_assert["default"].strictEqual(ERROR_ID, test('//'));
_assert["default"].strictEqual(ERROR_ID, test('//a'));
_assert["default"].strictEqual(ERROR_ID, test('///a'));
_assert["default"].strictEqual(ERROR_ID, test('///'));
_assert["default"].strictEqual(ERROR_ID, test('http:///a'));
_assert["default"].strictEqual(ERROR_ID, test('foo.com'));
_assert["default"].strictEqual(ERROR_ID, test('rdar://1234'));
_assert["default"].strictEqual(ERROR_ID, test('h://test'));
_assert["default"].strictEqual(ERROR_ID, test('http:// shouldfail.com'));
_assert["default"].strictEqual(ERROR_ID, test(':// should fail'));
_assert["default"].strictEqual(ERROR_ID, test('http://foo.bar/foo(bar)baz quux'));
_assert["default"].strictEqual(ERROR_ID, test('ftps://foo.bar/'));
_assert["default"].strictEqual(ERROR_ID, test('http://-error-.invalid/'));
_assert["default"].strictEqual(ERROR_ID, test('http://-a.b.co'));
_assert["default"].strictEqual(ERROR_ID, test('http://a.b-.co'));
_assert["default"].strictEqual(ERROR_ID, test('http://1.1.1.1.1'));
_assert["default"].strictEqual(ERROR_ID, test('http://123.123.123'));
_assert["default"].strictEqual(ERROR_ID, test('http://3628126748'));
_assert["default"].strictEqual(ERROR_ID, test('http://.www.foo.bar/'));
_assert["default"].strictEqual(ERROR_ID, test('http://.www.foo.bar./'));
});
it('should be valid when `value` is a valid url', function () {
_assert["default"].ok(!test('http://foo.com/blah_blah'));
_assert["default"].ok(!test('http://foo.com/blah_blah/'));
_assert["default"].ok(!test('http://foo.com/blah_blah_(wikipedia)'));
_assert["default"].ok(!test('http://foo.com/blah_blah_(wikipedia)_(again)'));
_assert["default"].ok(!test('http://www.example.com/wpstyle/?p=364'));
_assert["default"].ok(!test('https://www.example.com/foo/?bar=baz&inga=42&quux'));
_assert["default"].ok(!test('http://✪df.ws/123'));
_assert["default"].ok(!test('http://userid:password@example.com:8080'));
_assert["default"].ok(!test('http://userid:password@example.com:8080/'));
_assert["default"].ok(!test('http://userid@example.com'));
_assert["default"].ok(!test('http://userid@example.com/'));
_assert["default"].ok(!test('http://userid@example.com:8080'));
_assert["default"].ok(!test('http://userid@example.com:8080/'));
_assert["default"].ok(!test('http://userid:password@example.com'));
_assert["default"].ok(!test('http://userid:password@example.com/'));
_assert["default"].ok(!test('http://142.42.1.1/'));
_assert["default"].ok(!test('http://142.42.1.1:8080/'));
_assert["default"].ok(!test('https://userid::password@[2001:db8:85a3::8a2e:370:7334]:483/path/example?foobar=8&abc=2&bcd=5#product=browser+what&stuff'));
_assert["default"].ok(!test('http://➡.ws/䨹'));
_assert["default"].ok(!test('http://⌘.ws'));
_assert["default"].ok(!test('http://⌘.ws/'));
_assert["default"].ok(!test('http://foo.com/blah_(wikipedia)#cite-1'));
_assert["default"].ok(!test('http://foo.com/blah_(wikipedia)_blah#cite-1'));
_assert["default"].ok(!test('http://foo.com/unicode_(✪)_in_parens'));
_assert["default"].ok(!test('http://foo.com/(something)?after=parens'));
_assert["default"].ok(!test('http://☺.damowmow.com/'));
_assert["default"].ok(!test('http://code.google.com/events/#&product=browser'));
_assert["default"].ok(!test('http://j.mp'));
_assert["default"].ok(!test('http://foo.bar/?q=Test%20URL-encoded%20stuff'));
_assert["default"].ok(!test('http://مثال.إختبار'));
_assert["default"].ok(!test('http://例子.测试'));
_assert["default"].ok(!test('http://उदाहरण.परीक्षा'));
_assert["default"].ok(!test("http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com"));
_assert["default"].ok(!test('http://1337.net'));
_assert["default"].ok(!test('http://a.b-c.de'));
_assert["default"].ok(!test('http://223.255.255.254'));
_assert["default"].ok(!test('https://foo_bar.example.com/'));
_assert["default"].ok(!test('//example.net'));
});
it('should be invalid if the protocol is empty', function () {
_assert["default"].strictEqual(ERROR_ID, test('//google.com', {
emptyProtocol: false
}));
_assert["default"].strictEqual(ERROR_ID, test('//foo:bar@128.193.1.32:3000/foo?foo=bar', {
emptyProtocol: false
}));
});
it("should be invalid if the protocols don't match", function () {
_assert["default"].strictEqual(ERROR_ID, test('http://google.com', {
protocol: 'ftp'
}));
_assert["default"].strictEqual(ERROR_ID, test('http://google.com', {
protocol: ['ftp', 'https']
}));
_assert["default"].strictEqual(ERROR_ID, test('http://google.com', {
protocols: 'https'
}));
_assert["default"].strictEqual(ERROR_ID, test('http://google.com', {
protocols: ['ftp', 'https']
}));
_assert["default"].strictEqual(ERROR_ID, test('https://www.google.com', {
protocol: 'ftp'
}));
_assert["default"].strictEqual(ERROR_ID, test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar', {
protocol: 'http'
}));
});
it('should only be valid with certain protocols', function () {
_assert["default"].ok(!test('ftp://foo.bar/baz', {
protocol: 'ftp'
}));
_assert["default"].ok(!test('http://google.com', {
protocol: 'http'
}));
_assert["default"].ok(!test('http://google.com', {
protocol: ['http', 'https']
}));
_assert["default"].ok(!test('http://google.com', {
protocol: ['ftp', 'http', 'https']
}));
_assert["default"].ok(!test('http://google.com', {
protocols: 'http'
}));
_assert["default"].ok(!test('http://google.com', {
protocols: ['ftp', 'http', 'https']
}));
_assert["default"].ok(!test('https://www.google.com', {
protocol: 'https'
}));
_assert["default"].ok(!test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar', {
protocol: 'ftp'
}));
});
it('should be valid with no protocol identifier', function () {
_assert["default"].ok(!test('google.com', {
protocolIdentifier: false
}));
_assert["default"].ok(!test('userid:password@google.com/path?search=foo#stuff', {
protocolIdentifier: false
}));
});
it('should be invalid if basic auth not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@google.com', {
basicAuth: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid@google.com', {
basicAuth: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@217.64.0.1:3838/path?search=foo#stuff', {
basicAuth: false
}));
});
it('should be invalid if ipv4 not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://217.64.0.1', {
ipv4: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://217.64.0.1:3838', {
ipv4: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@217.64.0.1:3838/path?search=foo#stuff', {
ipv4: false
}));
});
it('should be invalid if ipv6 not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://[::1]', {
ipv6: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://[2001:db8:85a3::8a2e:370:7334]:3838', {
ipv6: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@[2001:db8:85a3::8a2e:370:7334]:3838/path?search=foo#stuff', {
ipv6: false
}));
});
it('should be invalid if host not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://google.com', {
host: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://www.google.com', {
host: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@google.com/path?search=foo#stuff', {
host: false
}));
});
it('should be invalid if local not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://localhost', {
local: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://localhost:3000', {
local: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@localhost:4000/path?search=foo#stuff', {
local: false
}));
});
it('should be invalid if port not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://localhost:2000', {
port: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://217.64.0.1:3000', {
port: false
}));
_assert["default"].strictEqual(ERROR_ID, test('https://userid:password@google.com:483/path?search=foo#stuff', {
port: false
}));
});
it('should be invalid if path not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://localhost/path', {
path: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://217.64.0.1:3000/path', {
path: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@google.com:8080/path?search=foo#stuff', {
path: false
}));
});
it('should be invalid if search not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://localhost?search', {
search: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://217.64.0.1:3000/?search', {
search: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@google.com:8080/path?search=foo#stuff', {
search: false
}));
});
it('should be invalid if hash not allowed', function () {
_assert["default"].strictEqual(ERROR_ID, test('http://localhost#hash', {
hash: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://217.64.0.1:3000/#hash99', {
hash: false
}));
_assert["default"].strictEqual(ERROR_ID, test('http://userid:password@google.com:8080/path?search=foo#stuff', {
hash: false
}));
});
it('should use default urlProtocols option', function () {
var defaultValue = _index["default"].defaultOptions.urlProtocols;
_index["default"].defaultOptions.urlProtocols = ['ftp'];
_assert["default"].strictEqual(ERROR_ID, test('http://google.com'));
_assert["default"].strictEqual(ERROR_ID, test('https://www.google.com'));
_assert["default"].strictEqual(ERROR_ID, test('http://foo:bar@128.193.1.32:3000/foo?foo=bar'));
_assert["default"].ok(!test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar'));
_index["default"].defaultOptions.urlProtocols = ['http', 'https'];
_assert["default"].ok(!test('http://google.com'));
_assert["default"].ok(!test('https://www.google.com'));
_assert["default"].ok(!test('http://foo:bar@128.193.1.32:3000/foo?foo=bar'));
_assert["default"].strictEqual(ERROR_ID, test('ftp://foo:bar@128.193.1.32:3000/foo?foo=bar'));
_index["default"].defaultOptions.urlProtocols = defaultValue;
});
it('should use formatMessage', function () {
var defaultValue = _index["default"].formatMessage;
_index["default"].formatMessage = function (msg) {
return Object.assign({}, msg, {
id: msg.id + '2'
});
};
_assert["default"].strictEqual(ERROR_ID + '2', test('http:/:/'));
_index["default"].formatMessage = defaultValue;
});
it('should parse a URL', function () {
var h = _index.url.parseURL('http://userid:password@google.com:8080/path?search=foo#stuff');
_assert["default"].strictEqual(h.protocol, 'http');
_assert["default"].strictEqual(h.basicAuth.username, 'userid');
_assert["default"].strictEqual(h.basicAuth.password, 'password');
_assert["default"].strictEqual(h.host, 'google.com');
_assert["default"].strictEqual(h.port, 8080);
_assert["default"].strictEqual(h.path, '/path');
_assert["default"].strictEqual(h.search, '?search=foo');
_assert["default"].strictEqual(h.hash, '#stuff');
h = _index.url.parseURL('http://userid@google.com');
_assert["default"].strictEqual(h.basicAuth.username, 'userid');
_assert["default"].strictEqual(h.basicAuth.password, void 0);
_assert["default"].deepStrictEqual(_index.url.parseURL('http://google.com', {
basicAuth: false,
ipv4: false,
ipv6: false,
local: false,
port: false,
path: false,
search: false,
hash: false
}), {
protocol: 'http',
host: 'google.com'
});
_assert["default"].deepStrictEqual(_index.url.parseURL('https://localhost', {
host: false
}), {
protocol: 'https',
host: 'localhost'
});
h = _index.url.parseURL('http://userid:password@localhost:8080/path?search=foo#stuff');
_assert["default"].strictEqual(h.host, 'localhost');
h = _index.url.parseURL('http://userid:password@128.193.1.32:8080/path?search=foo#stuff');
_assert["default"].strictEqual(h.ipv4, '128.193.1.32');
h = _index.url.parseURL('http://userid:password@[2001:db8:85a3::8a2e:370:7334]:8080/path?search=foo#stuff');
_assert["default"].strictEqual(h.ipv6, '2001:db8:85a3::8a2e:370:7334');
});
it('should not parse a URL', function () {
_assert["default"].strictEqual(_index.url.parseURL('http://userid:password@google.com', {
basicAuth: false
}), null);
_assert["default"].strictEqual(_index.url.parseURL('http://google.com', {
host: false
}), null);
_assert["default"].strictEqual(_index.url.parseURL('http://128.193.1.32', {
ipv4: false
}), null);
_assert["default"].strictEqual(_index.url.parseURL('http://[2001:db8:85a3::8a2e:370:7334]', {
ipv6: false
}), null);
_assert["default"].strictEqual(_index.url.parseURL('http://google.com/path', {
path: false
}), null);
_assert["default"].strictEqual(_index.url.parseURL('http://google.com?search', {
search: false
}), null);
_assert["default"].strictEqual(_index.url.parseURL('http://google.com#hash', {
hash: false
}), null);
});
});