Use a more aggressive cache control header for node status endpoint

This commit is contained in:
Raul Ochoa 2016-05-05 17:52:37 +02:00
parent 73aa159b98
commit c77ea49594
3 changed files with 116 additions and 1 deletions

View File

@ -116,7 +116,10 @@ LayergroupController.prototype.analysisNodeStatus = function(req, res) {
if (err) {
self.sendError(req, res, err, 'GET NODE STATUS');
} else {
self.sendResponse(req, res, nodeStatus, 200);
self.sendResponse(req, res, nodeStatus, 200, {
'Cache-Control': 'public,max-age=5',
'Last-Modified': new Date().toUTCString()
});
}
}
);

View File

@ -304,4 +304,27 @@ describe('analysis-layers', function() {
testClient.drain(done);
});
});
it('should response with custom cache headers for node status endpoints', function(done) {
var useCase = useCases[1];
// No API key here
var testClient = new TestClient(useCase.mapConfig, 1234);
testClient.getNodeStatus('HEAD', function(err, response, nodeStatus) {
assert.ok(!err, err);
assert.equal(nodeStatus.status, 'ready');
var headers = response.headers;
assert.equal(headers['cache-control'], 'public,max-age=5');
var lastModified = new Date(headers['last-modified']);
var tenSecondsInMs = 1e5;
assert.ok(Date.now() - lastModified.getTime() < tenSecondsInMs);
testClient.drain(done);
});
});
});

View File

@ -2,6 +2,7 @@
var qs = require('querystring');
var step = require('step');
var urlParser = require('url');
var mapnik = require('windshaft').mapnik;
@ -379,6 +380,94 @@ TestClient.prototype.getLayergroup = function(expectedResponse, callback) {
);
};
TestClient.prototype.getNodeStatus = function(nodeName, callback) {
var self = this;
var url = '/api/v1/map';
if (this.apiKey) {
url += '?' + qs.stringify({api_key: this.apiKey});
}
var layergroupId;
var nodes = {};
step(
function createLayergroup() {
var next = this;
assert.response(server,
{
url: url,
method: 'POST',
headers: {
host: 'localhost',
'Content-Type': 'application/json'
},
data: JSON.stringify(self.mapConfig)
},
{
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
},
function(res, err) {
if (err) {
return next(err);
}
var parsedBody = JSON.parse(res.body);
nodes = parsedBody.metadata.analyses.reduce(function(nodes, analysis) {
return Object.keys(analysis.nodes).reduce(function(nodes, nodeName) {
var node = analysis.nodes[nodeName];
nodes[nodeName] = node.url.http;
return nodes;
}, nodes);
}, nodes);
return next(null, parsedBody.layergroupid);
}
);
},
function getNodeStatusResult(err, _layergroupId) {
assert.ifError(err);
var next = this;
layergroupId = _layergroupId;
url = urlParser.parse(nodes[nodeName]).path;
if (self.apiKey) {
url += '?' + qs.stringify({api_key: self.apiKey});
}
var request = {
url: url,
method: 'GET',
headers: {
host: 'localhost'
}
};
var expectedResponse = {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
assert.response(server, request, expectedResponse, function(res, err) {
assert.ifError(err);
next(null, res, JSON.parse(res.body));
});
},
function finish(err, res, image) {
self.keysToDelete['map_cfg|' + LayergroupToken.parse(layergroupId).token] = 0;
self.keysToDelete['user:localhost:mapviews:global'] = 5;
return callback(err, res, image);
}
);
};
TestClient.prototype.drain = function(callback) {
helper.deleteRedisKeys(this.keysToDelete, callback);
};