Skip to content

Commit 945b671

Browse files
committed
Add combination unit test for URL matching
1 parent 736caf9 commit 945b671

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

tests/autolinker-url.spec.ts

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ describe( "Autolinker Url Matching -", () => {
7878
let result = autolinker.link( "Joe went to https://www.yahoo.com" );
7979
expect( result ).toBe( 'Joe went to <a href="https://www.yahoo.com">yahoo.com</a>' );
8080
} );
81-
82-
81+
82+
8383
it( 'should automatically link URLs with IP addresses', function() {
8484
let result = autolinker.link( 'http://66.102.7.147' );
8585
expect( result ).toBe( '<a href="http://66.102.7.147">66.102.7.147</a>' );
8686
} );
87-
87+
8888

8989
it( 'should automatically link URLs with IP addresses and a port number', function() {
9090
let result = autolinker.link( 'http://10.0.0.108:9000/' );
9191
expect( result ).toBe( '<a href="http://10.0.0.108:9000/">10.0.0.108:9000</a>' );
9292
} );
93-
93+
9494

9595
it( "should automatically link capitalized URLs", function() {
9696
let result = autolinker.link( "Joe went to HTTP://WWW.YAHOO.COM" );
@@ -114,7 +114,7 @@ describe( "Autolinker Url Matching -", () => {
114114
let result = autolinker.link( "Joe went to http://yahoo.com." );
115115
expect( result ).toBe( 'Joe went to <a href="http://yahoo.com">yahoo.com</a>.' );
116116
} );
117-
117+
118118

119119
it( "should automatically link URLs with a port number", function() {
120120
let result = autolinker.link( "Joe went to http://yahoo.com:8000 today." );
@@ -138,13 +138,13 @@ describe( "Autolinker Url Matching -", () => {
138138
let result = autolinker.link( "Joe went to http://localhost:8000/my-page today." );
139139
expect( result ).toBe( 'Joe went to <a href="http://localhost:8000/my-page">localhost:8000/my-page</a> today.' );
140140
} );
141-
141+
142142

143143
it( "should automatically link URLs with a port number and a query string", function() {
144144
let result = autolinker.link( "Joe went to http://yahoo.com:8000?page=index today." );
145145
expect( result ).toBe( 'Joe went to <a href="http://yahoo.com:8000?page=index">yahoo.com:8000?page=index</a> today.' );
146146
} );
147-
147+
148148

149149
it( "should automatically link a localhost URL with a port number and a query string", function() {
150150
let result = autolinker.link( "Joe went to http://localhost:8000?page=index today." );
@@ -163,31 +163,37 @@ describe( "Autolinker Url Matching -", () => {
163163
expect( result ).toBe( 'Joe went to <a href="http://localhost:8000#page=index">localhost:8000#page=index</a> today.' );
164164
} );
165165

166+
166167
it( "should automatically link domain names, paths, query strings, and hashes with numbers in them", function() {
167168
let result = autolinker.link( "Joe went to https://abc123def.org/path1/2path?param1=value1#hash123z" );
168169
expect( result ).toBe( 'Joe went to <a href="https://abc123def.org/path1/2path?param1=value1#hash123z">abc123def.org/path1/2path?param1=value1#hash123z</a>' );
169170
} );
170171

172+
171173
it( "should automatically link domain names, paths, query strings, and hashes with dashes in them", function() {
172174
let result = autolinker.link( "Joe went to https://abc-def.org/his-path/?the-param=the-value#the-hash" );
173175
expect( result ).toBe( 'Joe went to <a href="https://abc-def.org/his-path/?the-param=the-value#the-hash">abc-def.org/his-path/?the-param=the-value#the-hash</a>' );
174176
} );
175177

178+
176179
it( "should automatically link domain names, paths, query strings, and hashes with the set of allowed special characters in them", function() {
177180
let result = autolinker.link( "Link: https://abc123def.org/-+&@#/%=~_()|\'$*[]?!:,.;/?param1=value-+&@#/%=~_()|\'$*[]?!:,.;#hash-+&@#/%=~_()|\'$*[]?!:,.;z" );
178181
expect( result ).toBe( 'Link: <a href="https://abc123def.org/-+&@#/%=~_()|\'$*[]?!:,.;/?param1=value-+&@#/%=~_()|\'$*[]?!:,.;#hash-+&@#/%=~_()|\'$*[]?!:,.;z">abc123def.org/-+&@#/%=~_()|\'$*[]?!:,.;/?param1=value-+&@#/%=~_()|\'$*[]?!:,.;#hash-+&@#/%=~_()|\'$*[]?!:,.;z</a>' );
179182
} );
183+
180184

181185
it( "should automatically link a URL with accented characters", function() {
182186
let result = autolinker.link( "Joe went to http://mañana.com/mañana?mañana=1#mañana today." );
183187
expect( result ).toBe( 'Joe went to <a href="http://mañana.com/mañana?mañana=1#mañana">mañana.com/mañana?mañana=1#mañana</a> today.' );
184188
} );
185189

190+
186191
it( "should automatically link cyrillic URLs", function() {
187192
let result = autolinker.link( "Joe went to https://ru.wikipedia.org/wiki/Кириллица?Кириллица=1#Кириллица" );
188193
expect( result ).toBe( 'Joe went to <a href="https://ru.wikipedia.org/wiki/Кириллица?Кириллица=1#Кириллица">ru.wikipedia.org/wiki/Кириллица?Кириллица=1#Кириллица</a>' );
189194
} );
190195

196+
191197
it( "should automatically link international domain names", function() {
192198
let result1 = autolinker.link( "Русским гораздо проще набрать россия.рф на клавиатуре." );
193199
expect( result1 ).toBe( 'Русским гораздо проще набрать <a href="http://россия.рф">россия.рф</a> на клавиатуре.' );
@@ -199,6 +205,7 @@ describe( "Autolinker Url Matching -", () => {
199205
expect( result3 ).toBe( 'Русским гораздо проще набрать <a href="//россия.рф">россия.рф</a> на клавиатуре.' );
200206
} );
201207

208+
202209
it( "should automatically link domain names represented in punicode", function() {
203210
let result1 = autolinker.link( "For compatibility reasons, xn--d1acufc.xn--p1ai is an acceptable form of an international domain." );
204211
expect( result1 ).toBe( 'For compatibility reasons, <a href="http://xn--d1acufc.xn--p1ai">xn--d1acufc.xn--p1ai</a> is an acceptable form of an international domain.' );
@@ -207,6 +214,7 @@ describe( "Autolinker Url Matching -", () => {
207214
expect( result2 ).toBe( 'For compatibility reasons, <a href="http://xn--d1acufc.xn--p1ai">xn--d1acufc.xn--p1ai</a> is an acceptable form of an international domain.' );
208215
} );
209216

217+
210218
it( 'should match local urls with numbers when prefixed with http://', function() {
211219
let result1 = autolinker.link( 'http://localhost.local001/test' );
212220
expect( result1 ).toBe( '<a href="http://localhost.local001/test">localhost.local001/test</a>' );
@@ -230,6 +238,7 @@ describe( "Autolinker Url Matching -", () => {
230238
expect( autolinker.link( 'hello:wo.....rld' ) ).toBe( 'hello:wo.....rld' );
231239
});
232240

241+
233242
describe( "protocol linking", function() {
234243

235244
it( "should NOT include preceding ':' introductions without a space", function() {
@@ -808,4 +817,58 @@ describe( "Autolinker Url Matching -", () => {
808817
expect( result ).toBe( 'Joe went to <a href="http://yahoo.com">yahoo.com</a> and <a href="http://google.com">google.com</a>' );
809818
} );
810819

820+
821+
describe( 'combination example', () => {
822+
823+
it( `should automatically link all of the URLs of many different forms`, () => {
824+
let inputStr = `
825+
Joe went to http://yahoo.com and http://localhost today along with http://localhost:8000.
826+
He also had a path on localhost: http://localhost:8000/abc, and a query string: http://localhost:8000?abc
827+
But who could forget about hashes like http://localhost:8000#abc
828+
It seems http://www.google.com is a good site, but might want to be secure with https://www.google.com
829+
Sometimes people just need an IP http://66.102.7.147, and a port like http://10.0.0.108:9000
830+
Capitalized URLs are interesting: HTTP://WWW.YAHOO.COM
831+
We all like known TLDs like yahoo.com, but shouldn't go to unknown TLDs like sencha.etc
832+
And definitely shouldn't go to abc.123
833+
Don't want to include periods at the end of sentences like http://yahoo.com.
834+
Sometimes you need to go to a path like yahoo.com/my-page
835+
And hit query strings like yahoo.com?page=index
836+
Port numbers on known TLDs are important too like yahoo.com:8000.
837+
Hashes too yahoo.com:8000/#some-link.
838+
Sometimes you need a lot of things in the URL like https://abc123def.org/path1/2path?param1=value1#hash123z
839+
Do you see the need for dashes in these things too https://abc-def.org/his-path/?the-param=the-value#the-hash?
840+
There's a time for lots and lots of special characters like in https://abc123def.org/-+&@#/%=~_()|\'$*[]?!:,.;/?param1=value-+&@#/%=~_()|\'$*[]?!:,.;#hash-+&@#/%=~_()|\'$*[]?!:,.;z
841+
Don't forget about good times with unicode https://ru.wikipedia.org/wiki/Кириллица?Кириллица=1#Кириллица
842+
and this unicode http://россия.рф
843+
along with punycode http://xn--d1acufc.xn--p1ai
844+
Oh good old www links like www.yahoo.com
845+
`;
846+
847+
let result = autolinker.link( inputStr );
848+
expect( result ).toBe( `
849+
Joe went to <a href="http://yahoo.com">yahoo.com</a> and <a href="http://localhost">localhost</a> today along with <a href="http://localhost:8000">localhost:8000</a>.
850+
He also had a path on localhost: <a href="http://localhost:8000/abc">localhost:8000/abc</a>, and a query string: <a href="http://localhost:8000?abc">localhost:8000?abc</a>
851+
But who could forget about hashes like <a href="http://localhost:8000#abc">localhost:8000#abc</a>
852+
It seems <a href="http://www.google.com">google.com</a> is a good site, but might want to be secure with <a href="https://www.google.com">google.com</a>
853+
Sometimes people just need an IP <a href="http://66.102.7.147">66.102.7.147</a>, and a port like <a href="http://10.0.0.108:9000">10.0.0.108:9000</a>
854+
Capitalized URLs are interesting: <a href="HTTP://WWW.YAHOO.COM">YAHOO.COM</a>
855+
We all like known TLDs like <a href="http://yahoo.com">yahoo.com</a>, but shouldn't go to unknown TLDs like sencha.etc
856+
And definitely shouldn't go to abc.123
857+
Don't want to include periods at the end of sentences like <a href="http://yahoo.com">yahoo.com</a>.
858+
Sometimes you need to go to a path like <a href="http://yahoo.com/my-page">yahoo.com/my-page</a>
859+
And hit query strings like <a href="http://yahoo.com?page=index">yahoo.com?page=index</a>
860+
Port numbers on known TLDs are important too like <a href="http://yahoo.com:8000">yahoo.com:8000</a>.
861+
Hashes too <a href="http://yahoo.com:8000/#some-link">yahoo.com:8000/#some-link</a>.
862+
Sometimes you need a lot of things in the URL like <a href="https://abc123def.org/path1/2path?param1=value1#hash123z">abc123def.org/path1/2path?param1=value1#hash123z</a>
863+
Do you see the need for dashes in these things too <a href="https://abc-def.org/his-path/?the-param=the-value#the-hash">abc-def.org/his-path/?the-param=the-value#the-hash</a>?
864+
There's a time for lots and lots of special characters like in <a href="https://abc123def.org/-+&@#/%=~_()|'$*[]?!:,.;/?param1=value-+&@#/%=~_()|'$*[]?!:,.;#hash-+&@#/%=~_()|'$*[]?!:,.;z">abc123def.org/-+&@#/%=~_()|'$*[]?!:,.;/?param1=value-+&@#/%=~_()|'$*[]?!:,.;#hash-+&@#/%=~_()|'$*[]?!:,.;z</a>
865+
Don't forget about good times with unicode <a href="https://ru.wikipedia.org/wiki/Кириллица?Кириллица=1#Кириллица">ru.wikipedia.org/wiki/Кириллица?Кириллица=1#Кириллица</a>
866+
and this unicode <a href="http://россия.рф">россия.рф</a>
867+
along with punycode <a href="http://xn--d1acufc.xn--p1ai">xn--d1acufc.xn--p1ai</a>
868+
Oh good old www links like <a href="http://www.yahoo.com">yahoo.com</a>
869+
` );
870+
} );
871+
872+
} );
873+
811874
} );

0 commit comments

Comments
 (0)