Skip to content

Commit

Permalink
Add unit test for Utils.parseHttpHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
dsilhavy committed Jan 11, 2023
1 parent b679325 commit 461ece4
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions test/unit/core.Utils.js
Expand Up @@ -7,84 +7,91 @@ describe('Utils', () => {
it('Should return complete url if no original url is given', () => {
const b = 'https://localhost:3000/d/e/f.mp4';

expect(Utils.getRelativeUrl(undefined,b)).to.be.equal('https://localhost:3000/d/e/f.mp4');
expect(Utils.getRelativeUrl(undefined, b)).to.be.equal('https://localhost:3000/d/e/f.mp4');
})

it('Should return relative url if strings are different after server name', () => {
const a = 'https://localhost:3000/a/b/c.mp4';
const b = 'https://localhost:3000/d/e/f.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('/d/e/f.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('/d/e/f.mp4');
})

it('Should return relative url if strings are similar up to one element before filename', () => {
const a = 'https://localhost:3000/a/b/c.mp4';
const b = 'https://localhost:3000/a/c/f.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('../c/f.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('../c/f.mp4');
})

it('Should return relative url if strings are similar up to filename', () => {
const a = 'https://localhost:3000/a/b/c.mp4';
const b = 'https://localhost:3000/a/b/f.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('f.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('f.mp4');
})

it('Should return complete url if origin is different', () => {
const a = 'https://localhost:3000/a/b/c.mp4';
const b = 'https://loca:3000/a/b/f.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('https://loca:3000/a/b/f.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('https://loca:3000/a/b/f.mp4');
})

it('Should return filename if origin differs in terms of SSL', () => {
const a = 'https://localhost:3000/a/b/c.mp4';
const b = 'http://localhost:3000/a/b/e.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('e.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('e.mp4');
})

it('Should return relative url if part of the pathnames are not equal', () => {
const a = 'https://localhost:3000/a/b/c.mp4';
const b = 'https://localhost:3000/ab/b/f.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('/ab/b/f.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('/ab/b/f.mp4');
})

it('Should return relative url if target pathname is longer than source pathname ', () => {
const a = 'https://localhost:3000/a/b/f.mp4';
const b = 'https://localhost:3000/a/b/f/e/c.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('f/e/c.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('f/e/c.mp4');
})

it('Should return relative url if source pathname is longer than target pathname ', () => {
const a = 'https://localhost:3000/a/b/f/e/c.mp4';
const b = 'https://localhost:3000/a/b/f.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('/a/b/f.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('/a/b/f.mp4');
})

it('Should return relative url if source contains slash in the end ', () => {
const a = 'https://localhost:3000/a/';
const b = 'https://localhost:3000/a/b.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('b.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('b.mp4');
})

it('Should return relative url if source pathnames are exceptionally long ', () => {
const a = 'https://localhost:3000/a/b/c/d/e/f/g/h/1.mp4';
const b = 'https://localhost:3000/a/b/c/d/e/change/i/j/k/l/m/n/2.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('../../../change/i/j/k/l/m/n/2.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('../../../change/i/j/k/l/m/n/2.mp4');
})

it('Should return relative url if source contains slash in the end and multiple elements in the path', () => {
const a = 'https://localhost:3000/a/b/c/';
const b = 'https://localhost:3000/a/b/x/c.mp4';

expect(Utils.getRelativeUrl(a,b)).to.be.equal('../x/c.mp4');
expect(Utils.getRelativeUrl(a, b)).to.be.equal('../x/c.mp4');
})
})

describe('parseHttpHeaders', () => {

it('Should handle null header', () => {
expect(Utils.parseHttpHeaders(null)).to.be.empty;
})
})
})

0 comments on commit 461ece4

Please sign in to comment.