forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils-test.js
More file actions
108 lines (90 loc) · 3.57 KB
/
utils-test.js
File metadata and controls
108 lines (90 loc) · 3.57 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
var assert = require('chai').assert
var utils = require('../../lib/utils')
var { Headers } = require('node-fetch')
describe('Utility functions', function () {
describe('pathBasename', function () {
it('should return bar as relative path for /foo/bar', function () {
assert.equal(utils.pathBasename('/foo/bar'), 'bar')
})
it('should return empty as relative path for /foo/', function () {
assert.equal(utils.pathBasename('/foo/'), '')
})
it('should return empty as relative path for /', function () {
assert.equal(utils.pathBasename('/'), '')
})
it('should return empty as relative path for empty path', function () {
assert.equal(utils.pathBasename(''), '')
})
it('should return empty as relative path for undefined path', function () {
assert.equal(utils.pathBasename(undefined), '')
})
})
describe('stripLineEndings()', () => {
it('should pass through falsy string arguments', () => {
assert.equal(utils.stripLineEndings(''), '')
assert.equal(utils.stripLineEndings(null), null)
assert.equal(utils.stripLineEndings(undefined), undefined)
})
it('should remove line-endings characters', () => {
let str = '123\n456'
assert.equal(utils.stripLineEndings(str), '123456')
str = `123
456`
assert.equal(utils.stripLineEndings(str), '123456')
})
})
describe('debrack()', () => {
it('should return null if no string is passed', () => {
assert.equal(utils.debrack(), null)
})
it('should return the string if no brackets are present', () => {
assert.equal(utils.debrack('test string'), 'test string')
})
it('should return the string if less than 2 chars long', () => {
assert.equal(utils.debrack(''), '')
assert.equal(utils.debrack('<'), '<')
})
it('should remove brackets if wrapping the string', () => {
assert.equal(utils.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(utils.fullUrlForReq(req), 'https://example.com/resource1?sort=desc')
})
})
describe('getContentType()', () => {
describe('for Express headers', () => {
it('should default to application/octet-stream', () => {
assert.equal(utils.getContentType({}), 'application/octet-stream')
})
it('should get a basic content type', () => {
assert.equal(utils.getContentType({ 'content-type': 'text/html' }), 'text/html')
})
it('should get a content type without its charset', () => {
assert.equal(utils.getContentType({ 'content-type': 'text/html; charset=us-ascii' }), 'text/html')
})
})
describe('for Fetch API headers', () => {
it('should default to application/octet-stream', () => {
// eslint-disable-next-line no-undef
assert.equal(utils.getContentType(new Headers({})), 'application/octet-stream')
})
it('should get a basic content type', () => {
// eslint-disable-next-line no-undef
assert.equal(utils.getContentType(new Headers({ 'content-type': 'text/html' })), 'text/html')
})
it('should get a content type without its charset', () => {
// eslint-disable-next-line no-undef
assert.equal(utils.getContentType(new Headers({ 'content-type': 'text/html; charset=us-ascii' })), 'text/html')
})
})
})
})