-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
183 lines (171 loc) · 6.33 KB
/
index.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
var _ = require('lodash');
var TileRequest = require('../lib/TileRequest.js');
var TileRequestHandler = require('../lib/TileRequestHandler.js');
var TileServer = require('../lib/TileServer.js');
var TileLayer = require('../lib/TileLayer.js');
var assert = require('chai').assert;
var tilestrata = require('../index.js');
var version = require('../package.json').version;
describe('require("tilestrata")', function() {
it('should have "TileServer" property', function() {
assert.equal(tilestrata.TileServer, TileServer);
});
it('should have "TileRequest" property', function() {
assert.equal(tilestrata.TileRequest, TileRequest);
});
it('should have "TileRequestHandler" property', function() {
assert.equal(tilestrata.TileRequestHandler, TileRequestHandler);
});
it('should have "TileLayer" property', function() {
assert.equal(tilestrata.TileLayer, TileLayer);
});
it('should be a function that returns a TileServer instance w/options', function() {
var opts = {};
var result = tilestrata(opts);
assert.instanceOf(result, TileServer);
assert.equal(result.options, opts);
});
describe('middleware()', function() {
function testMiddleware(middleware, requrl, expect_next, expected_details, callback) {
var next = function(err) {
if (err) throw err;
if (expect_next) callback();
else throw new Error('Unexpected "next" call');
};
var _write_called = false;
var _writeHead_called = false;
var httpReq = {url: requrl, method: 'GET'};
var httpRes = {
writeHead: function(status, headers) {
if (expect_next) throw new Error('Unexpected "writeHead" call');
_writeHead_called = true;
var actual_headers = headers;
// don't test content-length when the buffer is variable
if (typeof expected_details.buffer === 'function') {
actual_headers = _.clone(actual_headers);
delete actual_headers['Content-Length'];
}
assert.equal(status, expected_details.status);
assert.deepEqual(actual_headers, expected_details.headers);
},
write: function(buffer) {
if (expect_next) throw new Error('Unexpected "write" call');
_write_called = true;
assert.isTrue(_writeHead_called);
assert.instanceOf(buffer, Buffer);
if (typeof expected_details.buffer === 'function') {
expected_details.buffer(buffer);
} else {
assert.equal(buffer.toString('utf8'), expected_details.buffer.toString('utf8'));
}
},
end: function() {
if (expect_next) throw new Error('Unexpected "end" call');
assert.isTrue(_writeHead_called, 'writeHead called by end');
assert.isTrue(_write_called, 'write called by end');
callback();
}
};
middleware(httpReq, httpRes, next);
}
it('should return 200 for /health', function(done) {
var server = new TileServer();
var middleware = tilestrata.middleware({server: server, prefix: '/tiles'});
var expected_headers = {'Content-Type': 'application/json'};
var expected_body = JSON.stringify();
process.env.TILESTRATA_HIDEHOSTNAME = '1';
testMiddleware(middleware, '/tiles/health', false, {status: 200, headers: expected_headers, buffer: function(actual_buffer) {
var parsedBody = JSON.parse(actual_buffer);
delete parsedBody.uptime;
delete parsedBody.uptime_s;
assert.deepEqual(parsedBody, {ok: true, version: version, host: '(hidden)'});
}}, done);
});
it('should return 200 when tile matches', function(done) {
var reqhook_called = false;
var reshook_called = false;
var expected_headers = {
'X-Test':'1',
'X-Powered-By': 'TileStrata/' + version,
'Content-Length': 4,
'Cache-Control': 'max-age=60'
};
var server = new TileServer();
server.layer('basemap').route('file.txt')
.use({
reqhook: function(_server, _tile, _req, _res, callback) {
reqhook_called = true;
assert.equal(_server, server);
assert.instanceOf(_tile, TileRequest);
assert.equal(_req.url, '/basemap/3/2/1/file.txt');
assert.isFunction(_res.writeHead);
callback();
}
})
.use({
reshook: function(_server, _tile, _req, _res, _result, callback) {
reshook_called = true;
assert.equal(_server, server);
assert.deepEqual(_result.headers, {
'X-Test':'1',
'X-Powered-By': 'TileStrata/' + version,
'Cache-Control': 'max-age=60'
});
assert.instanceOf(_result.buffer, Buffer);
assert.instanceOf(_tile, TileRequest);
assert.equal(_req.url, '/basemap/3/2/1/file.txt');
assert.isFunction(_res.writeHead);
callback();
}
})
.use({serve: function(server, req, callback) {
callback(null, Buffer.from('tile', 'utf8'), {'X-Test':'1'});
}});
var middleware = tilestrata.middleware({server: server, prefix: '/tiles'});
testMiddleware(middleware, '/tiles/basemap/3/2/1/file.txt', false, {status: 200, headers: expected_headers, buffer: Buffer.from('tile', 'utf8')}, function() {
assert.isTrue(reqhook_called, 'Request hook called');
assert.isTrue(reshook_called, 'Response hook called');
done();
});
});
it('should should initialize plugins', function(done) {
var reqhook_called = false;
var reshook_called = false;
var expected_headers = {
'X-Powered-By': 'TileStrata/' + version,
'Content-Length': 4,
'Cache-Control': 'max-age=60'
};
var initCalled = false;
var server = new TileServer();
server.layer('basemap').route('file.txt')
.use({
init: function(server, callback) {
setTimeout(function() {
initCalled = true;
callback();
}, 100);
},
serve: function(server, req, callback) {
callback(null, Buffer.from(String(initCalled), 'utf8'), {});
}
});
var middleware = tilestrata.middleware({server: server, prefix: '/tiles'});
testMiddleware(middleware, '/tiles/basemap/3/2/1/file.txt', false, {
status: 200,
headers: expected_headers,
buffer: Buffer.from('true', 'utf8')
}, function() {
done();
});
});
it('should call next() when route url doesn\'t match', function(done) {
var server = new TileServer();
server.layer('basemap').route('file.txt').use({serve: function(server, req, callback) {
callback(null, Buffer.from('tile', 'utf8'), {'X-Test':'1'});
}});
var middleware = tilestrata.middleware({server: server, prefix: '/tiles'});
testMiddleware(middleware, '/basemap/3/2/1/file.txt', true, null, done);
});
});
});