|
@@ -1,4 +1,5 @@
|
|
|
const { expect } = require('chai')
|
|
const { expect } = require('chai')
|
|
|
|
|
+const sinon = require('sinon')
|
|
|
const MockResponse = require('../helpers/MockResponse')
|
|
const MockResponse = require('../helpers/MockResponse')
|
|
|
const MockRequest = require('../helpers/MockRequest')
|
|
const MockRequest = require('../helpers/MockRequest')
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
const SandboxedModule = require('sandboxed-module')
|
|
@@ -9,8 +10,16 @@ describe('HttpErrorHandler', function() {
|
|
|
this.req = new MockRequest()
|
|
this.req = new MockRequest()
|
|
|
this.res = new MockResponse()
|
|
this.res = new MockResponse()
|
|
|
|
|
|
|
|
|
|
+ this.logger = {
|
|
|
|
|
+ warn() {},
|
|
|
|
|
+ error() {}
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
this.HttpErrorHandler = SandboxedModule.require(modulePath, {
|
|
this.HttpErrorHandler = SandboxedModule.require(modulePath, {
|
|
|
- globals: { console }
|
|
|
|
|
|
|
+ globals: { console },
|
|
|
|
|
+ requires: {
|
|
|
|
|
+ 'logger-sharelatex': this.logger
|
|
|
|
|
+ }
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -201,5 +210,51 @@ describe('HttpErrorHandler', function() {
|
|
|
foo: 'bar'
|
|
foo: 'bar'
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
|
|
+ describe('legacyInternal', function() {
|
|
|
|
|
+ it('returns 500', function() {
|
|
|
|
|
+ this.HttpErrorHandler.legacyInternal(this.req, this.res, new Error())
|
|
|
|
|
+ expect(this.res.statusCode).to.equal(500)
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ it('should send the error to the logger', function() {
|
|
|
|
|
+ const error = new Error('message')
|
|
|
|
|
+ this.logger.error = sinon.stub()
|
|
|
|
|
+ this.HttpErrorHandler.legacyInternal(
|
|
|
|
|
+ this.req,
|
|
|
|
|
+ this.res,
|
|
|
|
|
+ 'message',
|
|
|
|
|
+ error
|
|
|
|
|
+ )
|
|
|
|
|
+ expect(this.logger.error).to.have.been.calledWith(error)
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ it('should print a message when no content-type is included', function() {
|
|
|
|
|
+ this.HttpErrorHandler.legacyInternal(this.req, this.res, new Error())
|
|
|
|
|
+ expect(this.res.body).to.equal('internal server error')
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ it("should render a template when content-type is 'html'", function() {
|
|
|
|
|
+ this.req.accepts = () => 'html'
|
|
|
|
|
+ this.HttpErrorHandler.legacyInternal(this.req, this.res, new Error())
|
|
|
|
|
+ expect(this.res.renderedTemplate).to.equal('general/500')
|
|
|
|
|
+ expect(this.res.renderedVariables).to.deep.equal({
|
|
|
|
|
+ title: 'Server Error'
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ it("should return a json object with a static message when content-type is 'json'", function() {
|
|
|
|
|
+ this.req.accepts = () => 'json'
|
|
|
|
|
+ this.HttpErrorHandler.legacyInternal(
|
|
|
|
|
+ this.req,
|
|
|
|
|
+ this.res,
|
|
|
|
|
+ 'a message',
|
|
|
|
|
+ new Error()
|
|
|
|
|
+ )
|
|
|
|
|
+ expect(JSON.parse(this.res.body)).to.deep.equal({
|
|
|
|
|
+ message: 'a message'
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|