From aae2677b15e233aa4e7af925c622a57acbbeab90 Mon Sep 17 00:00:00 2001 From: Ramunas Date: Tue, 19 Apr 2016 19:44:12 +0300 Subject: [PATCH] fix #53 wrong response code on overzoom requests, introducing functional tests --- lib/app.js | 36 +++++++++++--------- package.json | 8 ++++- test/fixtures/empty.mbtiles | Bin 0 -> 26624 bytes test/mbtiles.js | 64 ++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 test/fixtures/empty.mbtiles create mode 100644 test/mbtiles.js diff --git a/lib/app.js b/lib/app.js index 032758d..d70850f 100644 --- a/lib/app.js +++ b/lib/app.js @@ -154,13 +154,13 @@ module.exports = function(tilelive, options) { if (ext !== format) { debug("Invalid format '%s', expected '%s'", format, ext); - return callback(null, null, populateHeaders({}, params, { 404: true, invalidFormat: true })); + return callback(null, null, populateHeaders({}, params, { 404: true, invalidFormat: true }), 404); } // validate zoom if (z < info.minzoom || z > info.maxzoom) { debug("Invalid zoom:", z); - return callback(null, null, populateHeaders({}, params, { 404: true, invalidZoom: true })); + return callback(null, null, populateHeaders({}, params, { 404: true, invalidZoom: true }), 404); } // validate coords against bounds @@ -171,22 +171,24 @@ module.exports = function(tilelive, options) { y < xyz.minY || y > xyz.maxY) { debug("Invalid coordinates: %d,%d relative to bounds:", x, y, xyz); - return callback(null, null, populateHeaders({}, params, { 404: true, invalidCoordinates: true })); + return callback(null, null, populateHeaders({}, params, { 404: true, invalidCoordinates: true }), 404); } return source.getTile(z, x, y, function(err, data, headers) { headers = normalizeHeaders(headers || {}); if (err) { - if (err.message.match(/(Tile|Grid) does not exist/)) { - return callback(null, null, populateHeaders(headers, params, { 404: true })); + if (!err.message.match(/(Tile|Grid) does not exist/)) { + return callback(err); } - - return callback(err); } if (data === null || data === undefined) { - return callback(null, null, populateHeaders(headers, params, { 404: true })); + if (ext != "pbf") { // return 404 error for missing rasters + return callback(null, null, populateHeaders(headers, params, {404: true}), 404); + } else { + data = ""; + } } if (!headers["content-md5"]) { @@ -199,7 +201,7 @@ module.exports = function(tilelive, options) { headers["content-encoding"] = headers["content-encoding"] || "gzip"; } - return callback(null, data, populateHeaders(headers, params, { 200: true })); + return callback(null, data, populateHeaders(headers, params, { 200: true }), 200); }); }); }); @@ -211,16 +213,20 @@ module.exports = function(tilelive, options) { y = req.params.y | 0, scale = getScale(req.params.scale), format = req.params.format; - return getTile(z, x, y, scale, format, function(err, data, headers) { + return getTile(z, x, y, scale, format, function(err, data, headers, status) { if (err) { return next(err); } - if (data == null && format !== "pbf") { - return res.status(404).send("Not found"); - } else { - res.set(headers); - return res.status(200).send(data); + + status = status || 200; + if (status != 200) { + return res.sendStatus(status); } + + return res + .status(200) + .set(headers) + .send(data); }, res, next); }); diff --git a/package.json b/package.json index cbde107..02744ac 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "url": "https://github.com/mojodna/tessera.git" }, "scripts": { - "prepublish": "npm install bower && bower install" + "prepublish": "npm install bower && bower install", + "test": "mocha" }, "dependencies": { "abaculus": "^1.2.1", @@ -28,5 +29,10 @@ "tilelive": "^5.3.3", "tilelive-cache": "^0.6.1", "tilelive-modules": "^0.2.0" + }, + "devDependencies": { + "mocha": "^1.18.0", + "supertest": "^0.3", + "mbtiles": "^0.2.9" } } diff --git a/test/fixtures/empty.mbtiles b/test/fixtures/empty.mbtiles new file mode 100644 index 0000000000000000000000000000000000000000..4370b1ba34d79f78960232a25e22e1066028a016 GIT binary patch literal 26624 zcmeHPOKcm*8Q$ecq(q696i zU3zz^hXtXK+am3?K#%UBKyN+doTBGml6wyYilRUcJq10ahX6tUdF)Fh{fH@v5QdP+ z*?Ihr@BinYe`jaC{{D*LVRh4XYMQ6cNj;J*OFvXqNs?ya-!=H>e_n?|kADHba-`g@ zI4j-!qQ4hf6X+|_=NkH&71|9D^#TEbz!@NLX&9Ic_GbvI=&U{i)`EpVKp@Z^2#ERL z9d-~>2n5ap0Wts219ibuAkZBMi22_gb`Vks1kM8iG5^m4b-`00&>aYny?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qb zrdrlbtqKW}Is5c#nm5NUtx|~@O>?jo!rsOkS{U_PL-!nS2)&=;^-6Yc z*8co`{bY=)YIVcY%$QH;M$AOf=@Cpe(}qdY&T;m@Z=6$xu9f4QW!Q)~ifz1zt|YB_ ziG|+5n^B|e;ff1q7^77d_V#Rt1g*d-nyx!Injf^8P~Up?w3vy{nNZD{L)4#wyrzXe z5wLcQP`wawl2E(|Hr#hgSFf&E)?=N7v}TicVT`cjYe;fP)xX} zjEy_ckFF2#o873X6q80fZdv1-h)IkS|>BLF0ExxJ(NCAizU^U_*%U%JY)8Ny;)i@Y#MCSuqI<_~shd{Y_$m|TCN%dY8zG(AiHS3M z#tsnW+#$B1J{pEo&!?tN$fu$?jzR7k;BLD^N<0IrO2aU9vVqnaD}`!szbo6|tl*QJ zJ~;a*ng2-ImgIjVzJQ-o`#gDcB{e!R(f8~5T?6mY!`kF9a!}yMnSZ=o`U!hkjviIM zyH4KWQ`bu?rKJs8B$x0$(@c0IUcI{>dHIjMh2{66C$#z7f6y&**`gnL8_@3r<*)}k ztv=%)RwteOa?4Ki{X5=?l&RovKUlj@B&Qeet}Vl>0{$_3au^&`qSx@++nu1ruL{R7 zQG$c*a2bkqhaamCYM!KCON}Z@-!C5#%xUnaUs0@S1!zQBoHNesT>T8S5;s3H8M$Z! zB0q~CoXTgTDT)MBO3TonC?gb!Uk&@-=MuEtC=#@yOOT+hz~5S6PT;@70B%JZ+n^C0 P7dT0bPJj&UYUuw5eRs(L literal 0 HcmV?d00001 diff --git a/test/mbtiles.js b/test/mbtiles.js new file mode 100644 index 0000000..8fa72c9 --- /dev/null +++ b/test/mbtiles.js @@ -0,0 +1,64 @@ +"use strict"; + +var assert = require("assert"), + express = require("express"), + request = require("supertest"), + tilelive = require("tilelive-cache")(require("tilelive"), {}), + serve = require("../lib/app"), + tessera = require("../lib/index"); + +// load and register tilelive modules +require("tilelive-modules/loader")(tilelive, {}); + +describe("MBTiles", function () { + var app; + before(function (done) { + var uri = "mbtiles://./test/fixtures/empty.mbtiles"; + app = express(); + app.use(serve(tilelive, uri)); + + return done(); + }); + + it("should return TileJSON", function (done) { + request(app) + .get("/index.json") + .expect(200) + .end(function (err, res) { + assert.equal(err, null); + assert(typeof res.body == "object"); + + assert.equal(res.body.format, "pbf"); + assert.equal(res.body.minzoom, 0); + assert.equal(res.body.maxzoom, 14); + + done(); + }); + }); + + it("should return empty vector tile on valid request", function (done) { + request(app) + .get("/5/1/1.pbf") + .expect(200) + .end(function (err, res) { + console.error(err); + assert.equal(err, null); + assert.equal(typeof res.text, "undefined"); + assert.deepEqual(res.body, {}); + + done(); + }); + }); + + it("should return 404 on overzoom request", function (done) { + request(app) + .get("/15/1/1.pbf") + .expect(404) + .end(function (err, res) { + assert.equal(err, null); + assert.equal(res.text, "Not Found"); + + done(); + }); + }); +});