forked from restify/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticLegacy.test.js
More file actions
165 lines (126 loc) · 4.47 KB
/
staticLegacy.test.js
File metadata and controls
165 lines (126 loc) · 4.47 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
// core requires
var fs = require('fs');
var path = require('path');
// external requires
var assert = require('chai').assert;
var mkdirp = require('mkdirp');
var restify = require('restify');
var restifyClients = require('restify-clients');
var rimraf = require('rimraf');
// local files
var helper = require('./lib/helper');
var plugins = require('../lib');
// local globals
var SERVER;
var CLIENT;
var PORT;
var FILES_TO_DELETE = [];
var DIRS_TO_DELETE = [];
describe('legacy static resource plugin', function () {
beforeEach(function (done) {
SERVER = restify.createServer({
dtrace: helper.dtrace,
log: helper.getLog('server')
});
SERVER.listen(PORT, '127.0.0.1', function () {
PORT = SERVER.address().port;
CLIENT = restifyClients.createJsonClient({
url: 'http://127.0.0.1:' + PORT,
dtrace: helper.dtrace,
retry: false
});
done();
});
});
afterEach(function (done) {
var i;
for (i = 0; i < FILES_TO_DELETE.length; ++i) {
try {
fs.unlinkSync(FILES_TO_DELETE[i]);
}
catch (err) {
/* normal */
}
}
for (i = 0; i < DIRS_TO_DELETE.length; ++i) {
try {
rimraf.sync(DIRS_TO_DELETE[i]);
}
catch (err) {
/* normal */
}
}
CLIENT.close();
SERVER.close(done);
});
function serveStaticTest(done, testDefault, tmpDir, regex, staticFile) {
var staticContent = '{"content": "abcdefg"}';
var staticObj = JSON.parse(staticContent);
var testDir = 'public';
var testFileName = 'index.json';
var routeName = 'GET wildcard';
var tmpPath = path.join(__dirname, '../', tmpDir);
mkdirp(tmpPath, function (err) {
assert.ifError(err);
DIRS_TO_DELETE.push(tmpPath);
var folderPath = path.join(tmpPath, testDir);
mkdirp(folderPath, function (err2) {
assert.ifError(err2);
DIRS_TO_DELETE.push(folderPath);
var file = path.join(folderPath, testFileName);
fs.writeFile(file, staticContent, function (err3) {
assert.ifError(err3);
FILES_TO_DELETE.push(file);
var p = '/' + testDir + '/' + testFileName;
var opts = { directory: tmpPath };
if (staticFile) {
opts.file = p;
}
if (testDefault) {
opts.defaultFile = testFileName;
routeName += ' with default';
}
var re = regex ||
new RegExp('/' + testDir + '/?.*');
SERVER.get({
path: re,
name: routeName
}, plugins.serveStaticLegacy(opts));
CLIENT.get(p, function (err4, req, res, obj) {
assert.ifError(err4);
assert.equal(res.headers['cache-control'],
'public, max-age=3600');
assert.deepEqual(obj, staticObj);
done();
});
});
});
});
}
it('static serves static files', function (done) {
serveStaticTest(done, false, '.tmp');
});
it('static serves static files in nested folders', function (done) {
serveStaticTest(done, false, '.tmp/folder');
});
it('static serves static files in with a root regex', function (done) {
serveStaticTest(done, false, '.tmp', new RegExp('/.*'));
});
it('static serves static files with a root, !greedy, regex',
function (done) {
serveStaticTest(done, false, '.tmp', new RegExp('/?.*'));
});
it('static serves default file', function (done) {
serveStaticTest(done, true, '.tmp');
});
it('restify-GH-379 static serves file with parentheses in path',
function (done) {
serveStaticTest(done, false, '.(tmp)');
});
it('restify-GH-719 serve a specific static file', function (done) {
// serve the same default file .tmp/public/index.json
// but get it from opts.file
serveStaticTest(done, false, '.tmp', null, true);
});
});