1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/test/unit/@node-red/editor-api/lib/util_spec.js

111 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

/**
* 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");
var request = require('supertest');
var express = require('express');
2018-08-20 17:17:24 +02:00
var NR_TEST_UTILS = require("nr-test-utils");
2018-04-24 16:01:49 +02:00
2018-08-20 17:17:24 +02:00
var apiUtil = NR_TEST_UTILS.require("@node-red/editor-api/lib/util");
2018-04-24 16:01:49 +02:00
2018-08-20 17:17:24 +02:00
var log = NR_TEST_UTILS.require("@node-red/util").log;
var i18n = NR_TEST_UTILS.require("@node-red/util").i18n;
2018-04-24 16:01:49 +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;});
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();
})
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;
before(function() {
2018-04-24 16:01:49 +02:00
oldDefaultLang = i18n.defaultLang;
i18n.defaultLang = "en-US";
})
after(function() {
i18n.defaultLang = oldDefaultLang;
})
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");
})
})
});