2017-08-22 23:26:29 +02:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var should = require("should");
|
2018-04-24 16:01:49 +02:00
|
|
|
var sinon = require("sinon");
|
2017-08-22 23:26:29 +02:00
|
|
|
var request = require('supertest');
|
|
|
|
var express = require('express');
|
|
|
|
|
|
|
|
var apiUtil = require("../../../red/api/util");
|
|
|
|
|
2018-04-24 16:01:49 +02:00
|
|
|
var log = require("../../../red/util").log; // TODO: separate module
|
|
|
|
var i18n = require("../../../red/util").i18n; // TODO: separate module
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
describe("api/util", function() {
|
|
|
|
describe("errorHandler", function() {
|
|
|
|
var loggedError = null;
|
|
|
|
var loggedEvent = null;
|
|
|
|
var app;
|
|
|
|
before(function() {
|
|
|
|
app = express();
|
2018-04-24 16:01:49 +02:00
|
|
|
sinon.stub(log,'error',function(msg) {loggedError = msg;});
|
|
|
|
sinon.stub(log,'audit',function(event) {loggedEvent = event;});
|
2017-08-22 23:26:29 +02:00
|
|
|
app.get("/tooLarge", function(req,res) {
|
|
|
|
var err = new Error();
|
|
|
|
err.message = "request entity too large";
|
|
|
|
throw err;
|
|
|
|
},apiUtil.errorHandler)
|
|
|
|
app.get("/stack", function(req,res) {
|
|
|
|
var err = new Error();
|
|
|
|
err.message = "stacktrace";
|
|
|
|
throw err;
|
|
|
|
},apiUtil.errorHandler)
|
|
|
|
});
|
2018-04-24 16:01:49 +02:00
|
|
|
after(function() {
|
|
|
|
log.error.restore();
|
|
|
|
log.audit.restore();
|
|
|
|
})
|
2017-08-22 23:26:29 +02:00
|
|
|
beforeEach(function() {
|
|
|
|
loggedError = null;
|
|
|
|
loggedEvent = null;
|
|
|
|
})
|
|
|
|
it("logs an error for request entity too large", function(done) {
|
|
|
|
request(app).get("/tooLarge").expect(400).end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
res.body.should.have.property("error","unexpected_error");
|
|
|
|
res.body.should.have.property("message","Error: request entity too large");
|
|
|
|
|
|
|
|
loggedError.should.have.property("message","request entity too large");
|
|
|
|
|
|
|
|
loggedEvent.should.have.property("event","api.error");
|
|
|
|
loggedEvent.should.have.property("error","unexpected_error");
|
|
|
|
loggedEvent.should.have.property("message","Error: request entity too large");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
it("logs an error plus stack for other errors", function(done) {
|
|
|
|
request(app).get("/stack").expect(400).end(function(err,res) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
res.body.should.have.property("error","unexpected_error");
|
|
|
|
res.body.should.have.property("message","Error: stacktrace");
|
|
|
|
|
|
|
|
/Error: stacktrace\s*at.*util_spec.js/m.test(loggedError).should.be.true();
|
|
|
|
|
|
|
|
loggedEvent.should.have.property("event","api.error");
|
|
|
|
loggedEvent.should.have.property("error","unexpected_error");
|
|
|
|
loggedEvent.should.have.property("message","Error: stacktrace");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('determineLangFromHeaders', function() {
|
2018-04-24 16:01:49 +02:00
|
|
|
var oldDefaultLang;
|
2017-08-22 23:26:29 +02:00
|
|
|
before(function() {
|
2018-04-24 16:01:49 +02:00
|
|
|
oldDefaultLang = i18n.defaultLang;
|
|
|
|
i18n.defaultLang = "en-US";
|
|
|
|
})
|
|
|
|
after(function() {
|
|
|
|
i18n.defaultLang = oldDefaultLang;
|
2017-08-22 23:26:29 +02:00
|
|
|
})
|
|
|
|
it('returns the default lang if non provided', function() {
|
|
|
|
apiUtil.determineLangFromHeaders(null).should.eql("en-US");
|
|
|
|
})
|
|
|
|
it('returns the first language accepted', function() {
|
|
|
|
apiUtil.determineLangFromHeaders(['fr-FR','en-GB']).should.eql("fr-FR");
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|