Respond with 204 when vector tile is empty

This commit is contained in:
Daniel García Aubert 2017-07-17 19:43:59 +02:00
parent a35b1e3e86
commit eccc3597aa
3 changed files with 73 additions and 5 deletions

View File

@ -201,6 +201,10 @@ BaseController.prototype.sendError = function(req, res, err, label) {
var statusCode = findStatusCode(err); var statusCode = findStatusCode(err);
if (err.message === 'Tile does not exist' && req.params.format === 'mvt') {
statusCode = 204;
}
debug('[%s ERROR] -- %d: %s, %s', label, statusCode, err, err.stack); debug('[%s ERROR] -- %d: %s, %s', label, statusCode, err, err.stack);
// If a callback was requested, force status to 200 // If a callback was requested, force status to 200

58
test/acceptance/mvt.js Normal file
View File

@ -0,0 +1,58 @@
require('../support/test_helper');
var assert = require('../support/assert');
var TestClient = require('../support/test-client');
function createMapConfig (sql) {
sql = sql || [
'select',
' *',
'from',
' populated_places_simple_reduced',
].join('\n');
return {
version: '1.6.0',
layers: [{
type: "cartodb",
options: {
sql: sql,
cartocss: TestClient.CARTOCSS.POINTS,
cartocss_version: '2.3.0',
interactivity: 'cartodb_id'
}
}]
};
}
describe('mvt', function () {
const testCases = [
{
desc: 'should get empty mvt with code 204 (no content)',
coords: { z: 0, x: 0, y: 0 },
format: 'mvt',
mapConfig: createMapConfig('select 1 as cartodb_id, null::geometry as the_geom_webmercator')
}
];
testCases.forEach(function (test) {
it(test.desc, done => {
const testClient = new TestClient(test.mapConfig, 1234);
const { z, x, y } = test.coords;
const options = {
format: test.format,
status: 204
};
testClient.getTile(z, x, y, options, (err, res) => {
assert.ifError(err);
assert.ifError(err);
assert.equal(res.statusCode, 204);
assert.equal(res.body, '');
testClient.drain(done);
});
});
});
});

View File

@ -525,7 +525,7 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) {
}; };
var expectedResponse = { var expectedResponse = {
status: 200, status: params.status || 200,
headers: { headers: {
'Content-Type': 'application/json; charset=utf-8' 'Content-Type': 'application/json; charset=utf-8'
} }
@ -542,7 +542,12 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) {
if (isMvt) { if (isMvt) {
request.encoding = 'binary'; request.encoding = 'binary';
if (expectedResponse.status === 200) {
expectedResponse.headers['Content-Type'] = 'application/x-protobuf'; expectedResponse.headers['Content-Type'] = 'application/x-protobuf';
} else if (expectedResponse.status === 204) {
expectedResponse.headers['Content-Type'] = undefined;
}
} }
var isGeojson = format.match(/geojson$/); var isGeojson = format.match(/geojson$/);
@ -561,16 +566,17 @@ TestClient.prototype.getTile = function(z, x, y, params, callback) {
assert.response(server, request, expectedResponse, function(res, err) { assert.response(server, request, expectedResponse, function(res, err) {
assert.ifError(err); assert.ifError(err);
var obj; var obj;
if (isPng) { if (isPng) {
obj = mapnik.Image.fromBytes(new Buffer(res.body, 'binary')); obj = mapnik.Image.fromBytes(new Buffer(res.body, 'binary'));
} }
else if (isMvt) { else if (isMvt) {
if (res.body) {
obj = new mapnik.VectorTile(z, x, y); obj = new mapnik.VectorTile(z, x, y);
obj.setDataSync(new Buffer(res.body, 'binary')); obj.setDataSync(new Buffer(res.body, 'binary'));
} }
}
else { else {
obj = JSON.parse(res.body); obj = JSON.parse(res.body);
} }