-
Notifications
You must be signed in to change notification settings - Fork 2
/
ponse.js
140 lines (110 loc) · 3.08 KB
/
ponse.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
const test = require('supertape');
const stub = require('@cloudcmd/stub');
const ponse = require('..');
const {
getPathName,
getQuery,
send,
} = ponse;
const {request} = require('serve-once')(ponse.static);
const {keys} = Object;
test('ponse: path traversal: status', async (t) => {
const {status} = await request.get('/../../../../../../etc/passwd', {
options: {
root: __dirname,
},
});
t.equal(status, 404, 'should equal');
t.end();
});
test('ponse: path traversal: message', async (t) => {
const {body} = await request.get('/../../../../../../etc/passwd', {
options: {
root: __dirname,
},
});
const expect = `Path /etc/passwd beyond root ${__dirname}!`;
t.equal(body, expect, 'should equal');
t.end();
});
test('ponse: path traversal: status: ok', async (t) => {
const {status} = await request.get('/ponse.js', {
options: {
root: __dirname,
},
});
t.equal(status, 200, 'should equal');
t.end();
});
test('ponse: path traversal: status: ok', async (t) => {
const {status} = await request.get('/ponse.js', {
options: {
root: `${__dirname}/../lib`,
},
});
t.equal(status, 200, 'should equal');
t.end();
});
test('ponse: getPathName: res', (t) => {
const url = '/hello?world=1';
const name = getPathName({
url,
});
t.equal(name, '/hello', 'should equal');
t.end();
});
test('ponse: getPathName: string', (t) => {
const url = '/hello?world=1';
const name = getPathName(url);
t.equal(name, '/hello', 'should equal');
t.end();
});
test('ponse: getQuery', (t) => {
const req = {
url: 'hi?world',
};
const query = getQuery(req);
t.equal(query, 'world', 'should equal');
t.end();
});
test('ponse: getQuery: string', (t) => {
const url = 'hi?world';
const query = getQuery(url);
t.equal(query, 'world', 'should equal');
t.end();
});
test('ponse: send', (t) => {
const request = {};
const response = {
writableFinished: true,
end: stub(),
setHeader: stub(),
};
send('hello', {
response,
request,
});
t.notOk(response.end.called, 'should not call response.end');
t.end();
});
test('ponse: get: content-type', async (t) => {
const filesToMimes = {
'320px-Floppy_disk_2009_G1' : 'image/jpeg',
'320px-Floppy_disk_2009_G1.jpg' : 'image/jpeg',
'294px-Railroad1860.png' : 'image/png',
'294px-Railroad1860' : 'image/png',
'hello.txt' : 'text/plain; charset=UTF-8',
};
for (const file of keys(filesToMimes)) {
const {headers} = await request.get('/' + file, {
options: {
root: __dirname + '/../test/fixtures/mimetype/',
},
});
const reportedMime = headers.get('content-type');
const expect = filesToMimes[file];
t.equal(reportedMime, expect, 'should equal');
}
t.end();
});