2018-05-23 13:14:38 +02:00
2021-05-21 15:17:21 +02:00
const urlUtils = require ( './urlUtils.js' ) ;
2018-05-23 13:14:38 +02:00
describe ( 'urlUtils' , function ( ) {
2020-12-01 20:05:24 +02:00
it ( 'should prepend a base URL' , ( async ( ) => {
2018-05-23 13:14:38 +02:00
expect ( urlUtils . prependBaseUrl ( 'testing.html' , 'http://example.com' ) ) . toBe ( 'http://example.com/testing.html' ) ;
expect ( urlUtils . prependBaseUrl ( 'testing.html' , 'http://example.com/' ) ) . toBe ( 'http://example.com/testing.html' ) ;
expect ( urlUtils . prependBaseUrl ( '/jmp/?id=123&u=http://something.com/test' , 'http://example.com/' ) ) . toBe ( 'http://example.com/jmp/?id=123&u=http://something.com/test' ) ;
expect ( urlUtils . prependBaseUrl ( '/testing.html' , 'http://example.com/' ) ) . toBe ( 'http://example.com/testing.html' ) ;
expect ( urlUtils . prependBaseUrl ( '/testing.html' , 'http://example.com/something' ) ) . toBe ( 'http://example.com/testing.html' ) ;
expect ( urlUtils . prependBaseUrl ( '/testing.html' , 'https://example.com/something' ) ) . toBe ( 'https://example.com/testing.html' ) ;
expect ( urlUtils . prependBaseUrl ( '//somewhereelse.com/testing.html' , 'https://example.com/something' ) ) . toBe ( 'https://somewhereelse.com/testing.html' ) ;
expect ( urlUtils . prependBaseUrl ( '//somewhereelse.com/testing.html' , 'http://example.com/something' ) ) . toBe ( 'http://somewhereelse.com/testing.html' ) ;
expect ( urlUtils . prependBaseUrl ( '' , 'http://example.com/something' ) ) . toBe ( 'http://example.com/something' ) ;
expect ( urlUtils . prependBaseUrl ( 'testing.html' , '' ) ) . toBe ( 'testing.html' ) ;
2019-10-29 11:02:42 +02:00
// It shouldn't prepend anything for these:
2018-05-24 13:44:13 +02:00
expect ( urlUtils . prependBaseUrl ( 'mailto:emailme@example.com' , 'http://example.com' ) ) . toBe ( 'mailto:emailme@example.com' ) ;
expect ( urlUtils . prependBaseUrl ( 'javascript:var%20testing=true' , 'http://example.com' ) ) . toBe ( 'javascript:var%20testing=true' ) ;
expect ( urlUtils . prependBaseUrl ( 'http://alreadyabsolute.com' , 'http://example.com' ) ) . toBe ( 'http://alreadyabsolute.com' ) ;
2019-06-13 01:26:09 +02:00
expect ( urlUtils . prependBaseUrl ( '#local-anchor' , 'http://example.com' ) ) . toBe ( '#local-anchor' ) ;
2019-09-10 10:25:58 +02:00
} ) ) ;
2020-12-01 20:05:24 +02:00
it ( 'should detect resource URLs' , ( async ( ) => {
2019-09-10 10:25:58 +02:00
const testCases = [
[ ':/1234abcd1234abcd1234abcd1234abcd' , { itemId : '1234abcd1234abcd1234abcd1234abcd' , hash : '' } ] ,
2019-09-12 23:48:10 +02:00
[ ':/1234abcd1234abcd1234abcd1234abcd "some text"' , { itemId : '1234abcd1234abcd1234abcd1234abcd' , hash : '' } ] ,
2019-09-10 10:25:58 +02:00
[ ':/1234abcd1234abcd1234abcd1234abcd#hash' , { itemId : '1234abcd1234abcd1234abcd1234abcd' , hash : 'hash' } ] ,
2019-09-12 23:48:10 +02:00
[ ':/1234abcd1234abcd1234abcd1234abcd#Книги-из-номер' , { itemId : '1234abcd1234abcd1234abcd1234abcd' , hash : 'Книги-из-номер' } ] ,
[ ':/1234abcd1234abcd1234abcd1234abcd#hash "some text"' , { itemId : '1234abcd1234abcd1234abcd1234abcd' , hash : 'hash' } ] ,
2019-09-10 10:25:58 +02:00
[ 'joplin://1234abcd1234abcd1234abcd1234abcd' , { itemId : '1234abcd1234abcd1234abcd1234abcd' , hash : '' } ] ,
[ 'joplin://1234abcd1234abcd1234abcd1234abcd#hash' , { itemId : '1234abcd1234abcd1234abcd1234abcd' , hash : 'hash' } ] ,
[ ':/1234abcd1234abcd1234abcd1234abc' , null ] ,
[ 'joplin://1234abcd1234abcd1234abcd1234abc' , null ] ,
] ;
for ( const t of testCases ) {
const u = urlUtils . parseResourceUrl ( t [ 0 ] ) ;
const expected = t [ 1 ] ;
if ( ! expected ) {
expect ( ! u ) . toBe ( true ) ;
} else {
2019-09-12 23:48:10 +02:00
if ( ! u ) {
expect ( ! ! u ) . toBe ( true ) ;
} else {
expect ( u . itemId ) . toBe ( expected . itemId ) ;
expect ( u . hash ) . toBe ( expected . hash ) ;
}
2019-09-10 10:25:58 +02:00
}
}
} ) ) ;
2018-05-23 13:14:38 +02:00
2020-12-01 20:05:24 +02:00
it ( 'should extract resource URLs' , ( async ( ) => {
2019-09-12 23:48:10 +02:00
const testCases = [
[ 'Bla [](:/11111111111111111111111111111111) bla [](:/22222222222222222222222222222222) bla' , [ '11111111111111111111111111111111' , '22222222222222222222222222222222' ] ] ,
[ 'Bla [](:/11111111111111111111111111111111 "Some title") bla [](:/22222222222222222222222222222222 "something else") bla' , [ '11111111111111111111111111111111' , '22222222222222222222222222222222' ] ] ,
[ 'Bla <img src=":/fcca2938a96a22570e8eae2565bc6b0b"/> bla [](:/22222222222222222222222222222222) bla' , [ 'fcca2938a96a22570e8eae2565bc6b0b' , '22222222222222222222222222222222' ] ] ,
[ 'Bla <img src=":/fcca2938a96a22570e8eae2565bc6b0b"/> bla <a href=":/33333333333333333333333333333333"/>Some note link</a> blu [](:/22222222222222222222222222222222) bla' , [ 'fcca2938a96a22570e8eae2565bc6b0b' , '33333333333333333333333333333333' , '22222222222222222222222222222222' ] ] ,
[ 'nothing here' , [ ] ] ,
[ '' , [ ] ] ,
] ;
for ( const t of testCases ) {
const result = urlUtils . extractResourceUrls ( t [ 0 ] ) ;
const expected = t [ 1 ] ;
2020-05-21 10:14:33 +02:00
const itemIds = result . map ( r => r . itemId ) ;
2019-09-12 23:48:10 +02:00
expect ( itemIds . sort ( ) . join ( ',' ) ) . toBe ( expected . sort ( ) . join ( ',' ) ) ;
}
} ) ) ;
2021-11-22 19:17:28 +02:00
it ( 'should convert a file URI to a file path' , ( async ( ) => {
2021-11-23 14:12:27 +02:00
// Tests imported from https://github.com/TooTallNate/file-uri-to-path/tree/master/test
const testCases = {
'file://host/path' : '//host/path' ,
'file://localhost/etc/fstab' : '/etc/fstab' ,
'file:///etc/fstab' : '/etc/fstab' ,
'file:///c:/WINDOWS/clock.avi' : 'c:/WINDOWS/clock.avi' ,
'file://localhost/c|/WINDOWS/clock.avi' : 'c:/WINDOWS/clock.avi' ,
'file:///c|/WINDOWS/clock.avi' : 'c:/WINDOWS/clock.avi' ,
'file://localhost/c:/WINDOWS/clock.avi' : 'c:/WINDOWS/clock.avi' ,
'file://hostname/path/to/the%20file.txt' : '//hostname/path/to/the file.txt' ,
'file:///c:/path/to/the%20file.txt' : 'c:/path/to/the file.txt' ,
'file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc' : 'C:/Documents and Settings/davris/FileSchemeURIs.doc' ,
'file:///C:/caf%C3%A9/%C3%A5r/d%C3%BCnn/%E7%89%9B%E9%93%83/Ph%E1%BB%9F/%F0%9F%98%B5.exe' : 'C:/café/år/dünn/牛铃/Phở/😵.exe' ,
} ;
for ( const [ input , expected ] of Object . entries ( testCases ) ) {
const actual = urlUtils . fileUriToPath ( input ) ;
expect ( actual ) . toBe ( expected ) ;
}
expect ( urlUtils . fileUriToPath ( 'file://c:/not/quite/right' ) ) . toBe ( 'c:/not/quite/right' ) ;
expect ( urlUtils . fileUriToPath ( 'file:///d:/better' ) ) . toBe ( 'd:/better' ) ;
expect ( urlUtils . fileUriToPath ( 'file:///c:/AUTOEXEC.BAT' , 'win32' ) ) . toBe ( 'c:\\AUTOEXEC.BAT' ) ;
2021-11-22 19:17:28 +02:00
} ) ) ;
2019-07-30 09:35:42 +02:00
} ) ;