From c6c307c50ce6923b7c5746d0fa1dd575fd5e660c Mon Sep 17 00:00:00 2001 From: antelle Date: Sat, 21 Mar 2020 12:08:27 +0100 Subject: [PATCH] url format tests --- app/scripts/util/formatting/url-format.js | 2 +- test/src/formatting/url-format.js | 35 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/src/formatting/url-format.js diff --git a/app/scripts/util/formatting/url-format.js b/app/scripts/util/formatting/url-format.js index 462d9491..d4167786 100644 --- a/app/scripts/util/formatting/url-format.js +++ b/app/scripts/util/formatting/url-format.js @@ -1,6 +1,6 @@ const UrlFormat = { multiSlashRegex: /\/{2,}/g, - lastPartRegex: /\/?[^\/\\]+$/, + lastPartRegex: /[\/\\]?[^\/\\]+$/, kdbxEndRegex: /\.kdbx$/i, getDataFileName(url) { diff --git a/test/src/formatting/url-format.js b/test/src/formatting/url-format.js new file mode 100644 index 00000000..698c6d60 --- /dev/null +++ b/test/src/formatting/url-format.js @@ -0,0 +1,35 @@ +import { expect } from 'chai'; +import { UrlFormat } from 'util/formatting/url-format'; + +describe('UrlFormat', () => { + it('should extract file name from url', () => { + expect(UrlFormat.getDataFileName('https://example.com/data/My.file.kDBx?x=1')).to.eql( + 'My.file' + ); + }); + + it('should determine if url represents a kdbx file', () => { + expect(UrlFormat.isKdbx('//data/file.KdBx')).to.be.true; + expect(UrlFormat.isKdbx('//data/file.kdb')).to.be.false; + expect(UrlFormat.isKdbx('//data/file.kdbxx')).to.be.false; + }); + + it('should replace multiple slashes', () => { + expect(UrlFormat.fixSlashes('//data/file//ext')).to.eql('/data/file/ext'); + }); + + it('should get directory url by full url', () => { + expect(UrlFormat.fileToDir('/var/data/My.file.kdbx')).to.eql('/var/data'); + expect(UrlFormat.fileToDir('\\\\share\\data\\My.file.kdbx')).to.eql('\\\\share\\data'); + expect(UrlFormat.fileToDir('My.file.kdbx')).to.eql('/'); + }); + + it('should make url from parts', () => { + expect( + UrlFormat.makeUrl('/path', { + hello: 'world', + data: '= &' + }) + ).to.eql('/path?hello=world&data=%3D%20%26'); + }); +});