-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
71 lines (63 loc) · 2.27 KB
/
index.test.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
const MBTiles = require("@mapbox/mbtiles");
const request = require("supertest");
const test = require("ava");
const fs = require("fs");
const zlib = require("zlib");
test.beforeEach(t => {
t.context = require("./index");
});
test("themes are returned verbatim from the themes directory", async t => {
const res = await request(t.context).get("/tiles/styles/1/disaster");
const expectation = fs.readFileSync("./themes/disaster.json");
t.is(res.status, 200);
t.deepEqual(res.body, JSON.parse(expectation));
});
test("the /styles/ segment in the theme URL is optional", async t => {
const res = await request(t.context).get("/tiles/1/disaster");
const expectation = fs.readFileSync("./themes/disaster.json");
t.is(res.status, 200);
t.deepEqual(res.body, JSON.parse(expectation));
});
test("the sprite.json endpoint is mocked to satisfy mapbox style loading", async t => {
const res = await request(t.context).get(
"/tiles/styles/1/disaster/sprite.json"
);
t.is(res.status, 200);
t.deepEqual(res.body, {});
});
test("the sprite.png endpoint is mocked with a static png to satisfy mapbox style loading", async t => {
const res = await request(t.context).get(
"/tiles/styles/1/disaster/sprite.png"
);
t.is(res.status, 200);
t.deepEqual(res.body, fs.readFileSync("./themes/sprite.png"));
});
test("map tiles are served appropriately out of the mbtiles archive", async t => {
// Sample coordinates that MUST be in the mbtiles archive
const [z, x, y] = [12, 657, 1463];
const res = await request(t.context).get(`/tiles/1/tiles/${z}/${x}/${y}.pbf`);
const expectation = await getTile(z, x, y);
t.is(res.status, 200);
t.is(res.text, expectation.toString());
});
// Promise-based helper to read a tile out of an mbtiles archive
function getTile(z, x, y) {
return new Promise((resolve, reject) => {
new MBTiles("./tiles/tiles.mbtiles", (err, mbtiles) => {
if (err) {
reject(500);
} else {
mbtiles.getTile(z, x, y, (err, tile, headers) => {
if (err) {
reject(404);
} else {
// Tiles are gzip-encoded so they must be unzipped for equivalence assertions
zlib.gunzip(tile, (err, unzipped) => {
resolve(unzipped);
});
}
});
}
});
});
}