|
| 1 | +var expect = require('chai').expect |
| 2 | + |
| 3 | +var Uri = (typeof(require) === 'function') ? require('../Uri') : window.Uri |
| 4 | + |
| 5 | +describe('Uri', function() { |
| 6 | + var u |
| 7 | + |
| 8 | + beforeEach(function() { |
| 9 | + u = new Uri('http://test.com') |
| 10 | + }) |
| 11 | + |
| 12 | + it('can replace protocol', function() { |
| 13 | + u.protocol('https') |
| 14 | + expect(u.toString()).to.equal('https://test.com') |
| 15 | + }) |
| 16 | + |
| 17 | + it('can replace protocol with colon suffix', function() { |
| 18 | + u.protocol('https:') |
| 19 | + expect(u.toString()).to.equal('https://test.com') |
| 20 | + }) |
| 21 | + |
| 22 | + it('keeps authority prefix when protocol is removed', function() { |
| 23 | + u.protocol(null) |
| 24 | + expect(u.toString()).to.equal('//test.com') |
| 25 | + }) |
| 26 | + |
| 27 | + it('can disable authority prefix but keep protocol', function() { |
| 28 | + u.hasAuthorityPrefix(false) |
| 29 | + expect(u.toString()).to.equal('http://test.com') |
| 30 | + }) |
| 31 | + |
| 32 | + it('can add user info', function() { |
| 33 | + u.userInfo('username:password') |
| 34 | + expect(u.toString()).to.equal('http://username:password@test.com') |
| 35 | + }) |
| 36 | + |
| 37 | + it('can add user info with trailing at', function() { |
| 38 | + u.userInfo('username:password@') |
| 39 | + expect(u.toString()).to.equal('http://username:password@test.com') |
| 40 | + }) |
| 41 | + |
| 42 | + it('can add a hostname to a relative path', function() { |
| 43 | + u = new Uri('/index.html') |
| 44 | + u.host('wherever.com') |
| 45 | + expect(u.toString()).to.equal('wherever.com/index.html') |
| 46 | + }) |
| 47 | + |
| 48 | + it('can change a hostname ', function() { |
| 49 | + u.host('wherever.com') |
| 50 | + expect(u.toString()).to.equal('http://wherever.com') |
| 51 | + }) |
| 52 | + |
| 53 | + it('should not add a port when there is no hostname', function() { |
| 54 | + u = new Uri('/index.html') |
| 55 | + u.port(8080) |
| 56 | + expect(u.toString()).to.equal('/index.html') |
| 57 | + }) |
| 58 | + |
| 59 | + it('should be able to change the port', function() { |
| 60 | + u.port(8080) |
| 61 | + expect(u.toString()).to.equal('http://test.com:8080') |
| 62 | + }) |
| 63 | + |
| 64 | + it('should be able to add a path to a domain', function() { |
| 65 | + u = new Uri('test.com') |
| 66 | + u.path('/some/article.html') |
| 67 | + expect(u.toString()).to.equal('test.com/some/article.html') |
| 68 | + }) |
| 69 | + |
| 70 | + it('should be able to change a path', function() { |
| 71 | + u.path('/some/article.html') |
| 72 | + expect(u.toString()).to.equal('http://test.com/some/article.html') |
| 73 | + }) |
| 74 | + |
| 75 | + it('should be able to delete a path', function() { |
| 76 | + u = new Uri('http://test.com/index.html') |
| 77 | + u.path(null) |
| 78 | + expect(u.toString()).to.equal('http://test.com') |
| 79 | + }) |
| 80 | + |
| 81 | + it('should be able to empty a path', function() { |
| 82 | + u = new Uri('http://test.com/index.html') |
| 83 | + u.path('') |
| 84 | + expect(u.toString()).to.equal('http://test.com') |
| 85 | + }) |
| 86 | + |
| 87 | + it('should be able to add a query to nothing', function() { |
| 88 | + u = new Uri('') |
| 89 | + u.query('this=that&something=else') |
| 90 | + expect(u.toString()).to.equal('?this=that&something=else') |
| 91 | + }) |
| 92 | + |
| 93 | + it('should be able to add a query to a relative path', function() { |
| 94 | + u = new Uri('/some/file.html') |
| 95 | + u.query('this=that&something=else') |
| 96 | + expect(u.toString()).to.equal('/some/file.html?this=that&something=else') |
| 97 | + }) |
| 98 | + |
| 99 | + it('should be able to add a query to a domain', function() { |
| 100 | + u = new Uri('test.com') |
| 101 | + u.query('this=that&something=else') |
| 102 | + expect(u.toString()).to.equal('test.com/?this=that&something=else') |
| 103 | + }) |
| 104 | + |
| 105 | + it('should be able to swap a query', function() { |
| 106 | + u = new Uri('www.test.com?this=that&a=1&b=2c=3') |
| 107 | + u.query('this=that&something=else') |
| 108 | + expect(u.toString()).to.equal('www.test.com/?this=that&something=else') |
| 109 | + }) |
| 110 | + |
| 111 | + it('should be able to delete a query', function() { |
| 112 | + u = new Uri('www.test.com?this=that&a=1&b=2c=3') |
| 113 | + u.query(null) |
| 114 | + expect(u.toString()).to.equal('www.test.com') |
| 115 | + }) |
| 116 | + |
| 117 | + it('should be able to empty a query', function() { |
| 118 | + u = new Uri('www.test.com?this=that&a=1&b=2c=3') |
| 119 | + u.query('') |
| 120 | + expect(u.toString()).to.equal('www.test.com') |
| 121 | + }) |
| 122 | + |
| 123 | + it('should be able to add an anchor to a domain', function() { |
| 124 | + u = new Uri('test.com') |
| 125 | + u.anchor('content') |
| 126 | + expect(u.toString()).to.equal('test.com/#content') |
| 127 | + }) |
| 128 | + |
| 129 | + it('should be able to add an anchor with a hash prefix to a domain', function() { |
| 130 | + u = new Uri('test.com') |
| 131 | + u.anchor('#content') |
| 132 | + expect(u.toString()).to.equal('test.com/#content') |
| 133 | + }) |
| 134 | + |
| 135 | + it('should be able to add an anchor to a path', function() { |
| 136 | + u = new Uri('a/b/c/123.html') |
| 137 | + u.anchor('content') |
| 138 | + expect(u.toString()).to.equal('a/b/c/123.html#content') |
| 139 | + }) |
| 140 | + |
| 141 | + it('should be able to change an anchor', function() { |
| 142 | + u = new Uri('/a/b/c/index.html#content') |
| 143 | + u.anchor('about') |
| 144 | + expect(u.toString()).to.equal('/a/b/c/index.html#about') |
| 145 | + }) |
| 146 | + |
| 147 | + it('should be able to empty an anchor', function() { |
| 148 | + u = new Uri('/a/b/c/index.html#content') |
| 149 | + u.anchor('') |
| 150 | + expect(u.toString()).to.equal('/a/b/c/index.html') |
| 151 | + }) |
| 152 | + |
| 153 | + it('should be able to delete an anchor', function() { |
| 154 | + u = new Uri('/a/b/c/index.html#content') |
| 155 | + u.anchor(null) |
| 156 | + expect(u.toString()).to.equal('/a/b/c/index.html') |
| 157 | + }) |
| 158 | + |
| 159 | + it('should insert missing slash when origin and path have no slash', function () { |
| 160 | + u = new Uri('http://test.com') |
| 161 | + u.setPath('relativePath') |
| 162 | + expect(u.toString()).to.equal('http://test.com/relativePath') |
| 163 | + }) |
| 164 | + |
| 165 | + it('should remove extra slash when origin and path both provide a slash', function () { |
| 166 | + u = new Uri('http://test.com/') |
| 167 | + u.setPath('/relativePath') |
| 168 | + expect(u.toString()).to.equal('http://test.com/relativePath') |
| 169 | + }) |
| 170 | + |
| 171 | + it('should remove extra slashes when origin and path both provide too many slashes', function () { |
| 172 | + u = new Uri('http://test.com//') |
| 173 | + u.setPath('//relativePath') |
| 174 | + expect(u.toString()).to.equal('http://test.com/relativePath') |
| 175 | + }) |
| 176 | + |
| 177 | + it('should be able to clone a separate copy which does not share state', function() { |
| 178 | + var a = new Uri('?a=1'), |
| 179 | + b = a.clone().addQueryParam('b', '2') |
| 180 | + expect(a.toString()).to.not.equal(b.toString()) |
| 181 | + }) |
| 182 | +}) |
0 commit comments