|
| 1 | + |
| 2 | +/** |
| 3 | + * Module dependencies. |
| 4 | + */ |
| 5 | + |
| 6 | +var Assert = require('validator.js').Assert; |
| 7 | +var Validator = require('validator.js').Validator; |
| 8 | +var Violation = require('validator.js').Violation; |
| 9 | +var assert = require('../../lib/asserts/uri-assert'); |
| 10 | +var fmt = require('util').format; |
| 11 | +var should = require('should'); |
| 12 | + |
| 13 | +/** |
| 14 | + * Test `UriAssert`. |
| 15 | + */ |
| 16 | + |
| 17 | +describe('UriAssert', function() { |
| 18 | + before(function() { |
| 19 | + Assert.prototype.Uri = assert; |
| 20 | + }); |
| 21 | + |
| 22 | + it('should throw an error if the constraint is invalid', function() { |
| 23 | + try { |
| 24 | + new Assert().Uri({ foo: 'bar' }); |
| 25 | + |
| 26 | + should.fail(); |
| 27 | + } catch (e) { |
| 28 | + e.should.be.instanceOf(Error); |
| 29 | + e.message.should.equal('Invalid constraint "foo=bar"'); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it('should throw an error if the input value is not a string', function() { |
| 34 | + var choices = [[], {}, 123]; |
| 35 | + |
| 36 | + choices.forEach(function(choice) { |
| 37 | + try { |
| 38 | + new Assert().Uri().validate(choice); |
| 39 | + |
| 40 | + should.fail(); |
| 41 | + } catch (e) { |
| 42 | + e.should.be.instanceOf(Violation); |
| 43 | + /* jshint camelcase: false */ |
| 44 | + e.violation.value.should.equal(Validator.errorCode.must_be_a_string); |
| 45 | + /* jshint camelcase: true */ |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should throw an error if the uri does not contain a `hostname`', function() { |
| 51 | + try { |
| 52 | + new Assert().Uri().validate(fmt('foo-%s-bar', new Array(248).join('-'))); |
| 53 | + |
| 54 | + should.fail(); |
| 55 | + } catch (e) { |
| 56 | + e.should.be.instanceOf(Violation); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw an error if the uri does not contain a `protocol`', function() { |
| 61 | + try { |
| 62 | + new Assert().Uri().validate('foobar.com'); |
| 63 | + |
| 64 | + should.fail(); |
| 65 | + } catch (e) { |
| 66 | + e.should.be.instanceOf(Violation); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it('should throw an error if the uri does not match the constraints', function() { |
| 71 | + try { |
| 72 | + new Assert().Uri({ protocol: 'http' }).validate(fmt('https://%[email protected]', new Array(248).join('-'))); |
| 73 | + |
| 74 | + should.fail(); |
| 75 | + } catch (e) { |
| 76 | + e.should.be.instanceOf(Violation); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + it('should expose `constraints` on the violation', function() { |
| 81 | + try { |
| 82 | + new Assert().Uri({ protocol: 'http' }).validate('https://foobar.com'); |
| 83 | + |
| 84 | + should.fail(); |
| 85 | + } catch(e) { |
| 86 | + e.show().violation.constraints.should.eql({ protocol: 'http' }); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + it('should expose `assert` equal to `Uri`', function() { |
| 91 | + try { |
| 92 | + new Assert().Uri().validate('foo'); |
| 93 | + |
| 94 | + should.fail(); |
| 95 | + } catch(e) { |
| 96 | + e.show().assert.should.equal('Uri'); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + it('should accept valid uris', function() { |
| 101 | + [ |
| 102 | + 'http://foobar.com', |
| 103 | + 'http://føøbåz.com', |
| 104 | + 'http://foobar.com', |
| 105 | + 'https://foobar.com', |
| 106 | + 'ftp://foobar.com', |
| 107 | + 'biz://foobar.com' |
| 108 | + ].forEach(function(choice) { |
| 109 | + new Assert().Uri().validate(choice); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments