-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathutils-test.mjs
More file actions
112 lines (94 loc) · 3.42 KB
/
utils-test.mjs
File metadata and controls
112 lines (94 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { describe, it } from 'mocha'
import { assert } from 'chai'
import * as utils from '../../lib/utils.mjs'
const {
pathBasename,
stripLineEndings,
debrack,
fullUrlForReq,
getContentType
} = utils
describe('Utility functions', function () {
describe('pathBasename', function () {
it('should return bar as relative path for /foo/bar', function () {
assert.equal(pathBasename('/foo/bar'), 'bar')
})
it('should return empty as relative path for /foo/', function () {
assert.equal(pathBasename('/foo/'), '')
})
it('should return empty as relative path for /', function () {
assert.equal(pathBasename('/'), '')
})
it('should return empty as relative path for empty path', function () {
assert.equal(pathBasename(''), '')
})
it('should return empty as relative path for undefined path', function () {
assert.equal(pathBasename(undefined), '')
})
})
describe('stripLineEndings()', () => {
it('should pass through falsy string arguments', () => {
assert.equal(stripLineEndings(''), '')
assert.equal(stripLineEndings(null), null)
assert.equal(stripLineEndings(undefined), undefined)
})
it('should remove line-endings characters', () => {
let str = '123\n456'
assert.equal(stripLineEndings(str), '123456')
str = `123
456`
assert.equal(stripLineEndings(str), '123456')
})
})
describe('debrack()', () => {
it('should return null if no string is passed', () => {
assert.equal(debrack(), null)
})
it('should return the string if no brackets are present', () => {
assert.equal(debrack('test string'), 'test string')
})
it('should return the string if less than 2 chars long', () => {
assert.equal(debrack(''), '')
assert.equal(debrack('<'), '<')
})
it('should remove brackets if wrapping the string', () => {
assert.equal(debrack('<test string>'), 'test string')
})
})
describe('fullUrlForReq()', () => {
it('should extract a fully-qualified url from an Express request', () => {
const req = {
protocol: 'https:',
get: (host) => 'example.com',
baseUrl: '/',
path: '/resource1',
query: { sort: 'desc' }
}
assert.equal(fullUrlForReq(req), 'https://example.com/resource1?sort=desc')
})
})
describe('getContentType()', () => {
describe('for Express headers', () => {
it('should not default', () => {
assert.equal(getContentType({}), '')
})
it('should get a basic content type', () => {
assert.equal(getContentType({ 'content-type': 'text/html' }), 'text/html')
})
it('should get a content type without its charset', () => {
assert.equal(getContentType({ 'content-type': 'text/html; charset=us-ascii' }), 'text/html')
})
})
describe('for Fetch API headers', () => {
it('should not default', () => {
assert.equal(getContentType(new Headers({})), '')
})
it('should get a basic content type', () => {
assert.equal(getContentType(new Headers({ 'content-type': 'text/html' })), 'text/html')
})
it('should get a content type without its charset', () => {
assert.equal(getContentType(new Headers({ 'content-type': 'text/html; charset=us-ascii' })), 'text/html')
})
})
})
})