|
| 1 | +import { normalizeUrl } from './index.js'; |
| 2 | +import { assert, describe, it } from 'vitest'; |
| 3 | + |
| 4 | +describe('normalizeUrl', () => { |
| 5 | + it('noop for regular url', () => { |
| 6 | + const original = new URL('http://example.com/foo/bar'); |
| 7 | + const { url, wasNormalized, denormalize } = normalizeUrl(original); |
| 8 | + |
| 9 | + assert.equal(wasNormalized, false); |
| 10 | + assert.equal(url.href, original.href); |
| 11 | + assert.equal(denormalize().href, original.href); |
| 12 | + assert.equal(denormalize('/baz').href, 'http://example.com/baz'); |
| 13 | + assert.equal( |
| 14 | + denormalize('?some=query#hash').href, |
| 15 | + 'http://example.com/foo/bar?some=query#hash' |
| 16 | + ); |
| 17 | + assert.equal(denormalize('http://somethingelse.com/').href, 'http://somethingelse.com/'); |
| 18 | + assert.equal( |
| 19 | + denormalize(new URL('http://somethingelse.com/')).href, |
| 20 | + 'http://somethingelse.com/' |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should normalize trailing slash', () => { |
| 25 | + const original = new URL('http://example.com/foo/bar/'); |
| 26 | + const { url, wasNormalized, denormalize } = normalizeUrl(original); |
| 27 | + |
| 28 | + assert.equal(wasNormalized, true); |
| 29 | + assert.equal(url.href, original.href.slice(0, -1)); |
| 30 | + assert.equal(denormalize().href, original.href); |
| 31 | + assert.equal(denormalize('/baz').href, 'http://example.com/baz/'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should normalize data request route', () => { |
| 35 | + const original = new URL('http://example.com/foo/__data.json'); |
| 36 | + const { url, wasNormalized, denormalize } = normalizeUrl(original); |
| 37 | + |
| 38 | + assert.equal(wasNormalized, true); |
| 39 | + assert.equal(url.href, 'http://example.com/foo'); |
| 40 | + assert.equal(denormalize().href, original.href); |
| 41 | + assert.equal(denormalize('/baz').href, 'http://example.com/baz/__data.json'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should normalize route request route', () => { |
| 45 | + const original = new URL('http://example.com/foo/__route.js'); |
| 46 | + const { url, wasNormalized, denormalize } = normalizeUrl(original); |
| 47 | + |
| 48 | + assert.equal(wasNormalized, true); |
| 49 | + assert.equal(url.href, 'http://example.com/foo'); |
| 50 | + assert.equal(denormalize().href, original.href); |
| 51 | + assert.equal(denormalize('/baz').href, 'http://example.com/baz/__route.js'); |
| 52 | + }); |
| 53 | +}); |
0 commit comments