Windshaft-cartodb/test/unit/error-middleware-test.js

179 lines
4.9 KiB
JavaScript
Raw Normal View History

'use strict';
2019-10-07 17:29:07 +08:00
require('../support/test-helper');
var assert = require('assert');
2019-10-07 17:29:07 +08:00
var errorMiddleware = require('../../lib/api/middlewares/error-middleware');
2019-10-22 01:07:24 +08:00
describe('error-middleware', function () {
it('different formats for postgis plugin error returns 400 as status code', function () {
var expectedStatusCode = 400;
assert.equal(
2019-10-22 01:07:24 +08:00
errorMiddleware.findStatusCode('Postgis Plugin: ERROR: column "missing" does not exist\n'),
expectedStatusCode,
2019-10-22 01:07:24 +08:00
'Error status code for single line does not match'
);
assert.equal(
2019-10-22 01:07:24 +08:00
errorMiddleware.findStatusCode('Postgis Plugin: PSQL error:\nERROR: column "missing" does not exist\n'),
expectedStatusCode,
2019-10-22 01:07:24 +08:00
'Error status code for multiline/PSQL does not match'
);
});
2017-11-28 01:14:02 +08:00
it('should return a header with errors', function (done) {
const error = new Error('error test');
error.label = 'test label';
error.type = 'test type';
error.subtype = 'test subtype';
2017-11-28 01:14:02 +08:00
const errors = [error, error];
2017-11-28 01:14:02 +08:00
const req = {};
const res = {
headers: {},
set (key, value) {
this.headers[key] = value;
},
statusCode: 0,
status (status) {
this.statusCode = status;
},
json () {},
send () {}
};
const errorHeader = {
2017-11-29 01:22:55 +08:00
mainError: {
statusCode: 400,
message: error.message,
name: error.name,
label: error.label,
type: error.type,
2019-10-22 01:07:24 +08:00
subtype: error.subtype
2017-11-29 01:22:55 +08:00
},
moreErrors: [{
message: error.message,
name: error.name,
label: error.label,
type: error.type,
subtype: error.subtype
}]
2017-11-28 01:14:02 +08:00
};
const errorFn = errorMiddleware();
errorFn(errors, req, res);
2017-11-28 01:14:02 +08:00
assert.deepEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
});
done();
});
it('JSONP should return a header with error status code', function (done) {
const error = new Error('error test');
error.label = 'test label';
error.type = 'test type';
error.subtype = 'test subtype';
const errors = [error, error];
const req = {
query: { callback: true }
};
const res = {
headers: {},
set (key, value) {
this.headers[key] = value;
},
statusCode: 0,
status (status) {
this.statusCode = status;
},
jsonp () {},
send () {}
};
const errorHeader = {
2017-11-29 01:22:55 +08:00
mainError: {
statusCode: 400,
message: error.message,
name: error.name,
label: error.label,
type: error.type,
2019-10-22 01:07:24 +08:00
subtype: error.subtype
2017-11-29 01:22:55 +08:00
},
moreErrors: [{
message: error.message,
name: error.name,
label: error.label,
type: error.type,
subtype: error.subtype
}]
};
const errorFn = errorMiddleware();
errorFn(errors, req, res);
assert.deepEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
});
done();
});
2017-12-18 19:35:44 +08:00
it('should escape chars that broke logs regex', function (done) {
const badString = 'error: ( ) = " \" \' * $ & |';
const escapedString = 'error ';
const error = new Error(badString);
error.label = badString;
error.type = badString;
error.subtype = badString;
const errors = [error, error];
2017-12-18 19:35:44 +08:00
const req = {};
const res = {
headers: {},
set (key, value) {
this.headers[key] = value;
},
statusCode: 0,
status (status) {
this.statusCode = status;
},
json () {},
send () {}
};
const errorHeader = {
2017-12-18 19:35:44 +08:00
mainError: {
statusCode: 400,
message: escapedString,
name: error.name,
label: escapedString,
type: escapedString,
2019-10-22 01:07:24 +08:00
subtype: escapedString
2017-12-18 19:35:44 +08:00
},
moreErrors: [{
message: escapedString,
name: error.name,
label: escapedString,
type: escapedString,
subtype: escapedString
}]
};
const errorFn = errorMiddleware();
errorFn(errors, req, res);
assert.deepEqual(res.headers, {
'X-Tiler-Errors': JSON.stringify(errorHeader)
});
done();
});
});