-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
307 lines (280 loc) · 10.4 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* globals describe it */
var http = require('http');
var zlib = require('zlib');
var proxy = require('../index.js');
var tilestrata = require('tilestrata');
var assert = require('chai').assert;
describe('"tilestrata-proxy"', function() {
it('should set "name"', function() {
assert.equal(proxy('uri').name, 'proxy');
});
before(function(done) {
var failCount = 0;
http.createServer(function(req, res) {
if (req.url === '/dummy-3-2-1.txt') {
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('The content');
} else if (req.url === '/compressed.gzip') {
res.writeHead(200, {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Encoding': 'gzip',
});
var data = Buffer.from('The content', 'utf8');
zlib.gzip(data, function(err, data) {
if (err) throw err;
res.end(data);
});
} else if (req.url === '/doesnotexist-3-2-1.txt') {
res.writeHead(404, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('The content');
} else if (req.url === '/subdomain-b-3-2-2.txt') {
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('The content');
} else if (req.url === '/subdomain-array-world-3-2-1.txt') {
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('The content');
} else if (req.url === '/fn.txt') {
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('The content');
} else if (req.url === '/fail-2-times.txt') {
if (failCount < 2) {
++failCount;
res.writeHead(500, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('Request failed');
return;
}
res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('The content after ' + failCount + ' failures');
} else if (req.url === '/fail.txt') {
res.writeHead(500, {'Content-Type': 'text/plain; charset=utf-8'});
res.end('Request failed');
return;
} else {
throw new Error('Unexpected URL "' + req.url + '"');
}
}).listen(8889, function(err) {
if (err) throw err;
done();
});
});
it('should decompress gzipped content by default', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf'); // no accept-encoding header
req.headers['accept-encoding'] = 'gzip'; // should ignore
server.layer('layer').route('tile.pbf').use(proxy({
uri: 'http://localhost:8889/compressed.gzip'
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(headers['content-type'], 'text/plain; charset=utf-8', 'Content-Type should be set correctly');
assert.equal(headers['content-encoding'], undefined, 'Content-Encoding should be missing');
assert.equal(buffer.toString('utf8'), 'The content');
done();
});
});
});
it('should decompress gzipped content if decompress="client" and client doesn\'t send accept-encoding', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf'); // no accept-encoding
server.layer('layer').route('tile.pbf').use(proxy({
decompress: 'client',
uri: 'http://localhost:8889/compressed.gzip'
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(headers['content-type'], 'text/plain; charset=utf-8', 'Content-Type should be set correctly');
assert.equal(headers['content-encoding'], undefined, 'Content-Encoding should be missing');
assert.equal(buffer.toString('utf8'), 'The content');
done();
});
});
});
it('should not decompress content if decompress="never"', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf');
req.headers['accept-encoding'] = 'gzip'; // should acknowledge
server.layer('layer').route('tile.pbf').use(proxy({
decompress: 'never',
uri: 'http://localhost:8889/compressed.gzip'
}));
var expectedRawData = Buffer.from('The content', 'utf8');
zlib.gzip(expectedRawData, function(err, expectedGzippedData) {
if (err) throw err;
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(headers['content-type'], 'text/plain; charset=utf-8', 'Content-Type should be set correctly');
assert.equal(headers['content-encoding'], 'gzip', 'Content-Encoding should not be modified');
assert.equal(buffer.toString('hex'), expectedGzippedData.toString('hex'));
done();
});
});
});
});
it('should not decompress content if decompress="client" and client sends accept-encoding', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf');
req.headers['accept-encoding'] = 'gzip'; // should acknowledge
server.layer('layer').route('tile.pbf').use(proxy({
decompress: 'client',
uri: 'http://localhost:8889/compressed.gzip'
}));
var expectedRawData = Buffer.from('The content', 'utf8');
zlib.gzip(expectedRawData, function(err, expectedGzippedData) {
if (err) throw err;
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(headers['content-type'], 'text/plain; charset=utf-8', 'Content-Type should be set correctly');
assert.equal(headers['content-encoding'], 'gzip', 'Content-Encoding should not be modified');
assert.equal(buffer.toString('hex'), expectedGzippedData.toString('hex'));
done();
});
});
});
});
it('should return remote content', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf');
server.layer('layer').route('tile.pbf').use(proxy({
uri: 'http://localhost:8889/dummy-{z}-{x}-{y}.txt'
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 200);
assert.equal(headers['content-type'], 'text/plain; charset=utf-8', 'Content-Type should be set correctly');
assert.equal(buffer.toString('utf8'), 'The content');
done();
});
});
});
it('should return remote content (with {s} subdomain)', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/2/tile.pbf');
server.layer('layer').route('tile.pbf').use(proxy({
uri: 'http://localhost:8889/subdomain-{s}-{z}-{x}-{y}.txt'
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(buffer.toString('utf8'), 'The content');
done();
});
});
});
it('should return remote content (with {s} subdomain array)', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf');
server.layer('layer').route('tile.pbf').use(proxy({
uri: 'http://localhost:8889/subdomain-array-{s}-{z}-{x}-{y}.txt',
subdomains: ['hello', 'world'],
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(buffer.toString('utf8'), 'The content');
done();
});
});
});
it('should return remote content (with function)', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/5/6/7/tile.pbf');
server.layer('layer').route('tile.pbf').use(proxy({
uri: function(tile) {
assert.equal(tile.z, 5, 'tile.z');
assert.equal(tile.x, 6, 'tile.x');
assert.equal(tile.y, 7, 'tile.y');
return 'http://localhost:8889/fn.txt';
},
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(buffer.toString('utf8'), 'The content');
done();
});
});
});
it('should return 404 Not Found if function returns falsy value', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/5/6/7/tile.pbf');
server.layer('layer').route('tile.pbf').use(proxy({
uri: function(tile) {
return null;
},
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 404, 'status');
assert.equal(buffer.toString('utf8'), 'No proxy url defined for this tile');
done();
});
});
});
it('should return error if remote fails', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf');
server.layer('layer').route('tile.pbf').use(proxy({
uri: 'http://localhost:8889/fail.txt'
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 500);
assert.equal(buffer.toString('utf8'), 'Received non-200 status from upstream (500)');
done();
});
});
});
it('should allow retries', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf');
server.layer('layer').route('tile.pbf').use(proxy({
uri: 'http://localhost:8889/fail-2-times.txt',
retries: 3,
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 200, 'status');
assert.equal(buffer.toString('utf8'), 'The content after 2 failures');
done();
});
});
});
it('should return proper status code', function(done) {
this.timeout(5000);
var server = new tilestrata.TileServer();
var req = tilestrata.TileRequest.parse('/layer/3/2/1/tile.pbf');
server.layer('layer').route('tile.pbf').use(proxy({
uri: 'http://localhost:8889/doesnotexist-{z}-{x}-{y}.txt'
}));
server.initialize(function(err) {
if (err) throw err;
server.serve(req, false, function(status, buffer, headers) {
assert.equal(status, 404);
done();
});
});
});
});