|
| 1 | +'use strict'; |
| 2 | +const assert = require('assert'); |
| 3 | +const extractDomain = require('./index'); |
| 4 | + |
| 5 | +const urls = [ |
| 6 | + 'https://www.npmjs.com/package/extract-domain', |
| 7 | + 'http://www.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument', |
| 8 | + 'https://npmjs.com/package/extract-domain', |
| 9 | + 'http://example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument', |
| 10 | + 'http://www.so.many.sub.domains.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument', |
| 11 | + 'http://user:[email protected]:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument', |
| 12 | + 'ftp://example.org/resource.txt' |
| 13 | +]; |
| 14 | + |
| 15 | +const expected = [ |
| 16 | + 'npmjs.com', |
| 17 | + 'example.com', |
| 18 | + 'npmjs.com', |
| 19 | + 'example.com', |
| 20 | + 'example.com', |
| 21 | + 'example.com', |
| 22 | + 'example.org' |
| 23 | +]; |
| 24 | + |
| 25 | +describe('extract domain', () => { |
| 26 | + it('should extract given domain from string without sub domain', () => { |
| 27 | + assert.equal(extractDomain(urls[3]), expected[3]); |
| 28 | + assert.equal(extractDomain(urls[1]), expected[1]); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should extract given domain from string', () => { |
| 32 | + assert.equal(extractDomain(urls[1]), expected[1]); |
| 33 | + |
| 34 | + assert.equal(extractDomain(urls[0]), expected[0]); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should extract given domain from an array of strings', () => { |
| 38 | + const domains = extractDomain(urls); |
| 39 | + |
| 40 | + domains.map(domain => assert(expected.indexOf(domain) > -1)); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return empty string if it is not a domain', () => { |
| 44 | + assert.equal(extractDomain('/i.am/just.astring//7test'), ''); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should throw syntax error exception if the argument is not string nor array', () => { |
| 48 | + try { |
| 49 | + extractDomain('{}'); |
| 50 | + } catch (e) { |
| 51 | + assert.equal(e.name, 'TypeError'); |
| 52 | + |
| 53 | + assert.equal( |
| 54 | + e.message, |
| 55 | + 'The given URL is not a string. Please verify your string|array.' |
| 56 | + ); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw syntax error exception if the array value is not a string', () => { |
| 61 | + try { |
| 62 | + extractDomain([ [ 'wow' ] ]); |
| 63 | + } catch (e) { |
| 64 | + assert.equal(e.name, 'TypeError'); |
| 65 | + |
| 66 | + assert.equal( |
| 67 | + e.message, |
| 68 | + 'The given URL is not a string. Please verify your string|array.' |
| 69 | + ); |
| 70 | + } |
| 71 | + }); |
| 72 | +}); |
0 commit comments