Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add calling next for Adding next() call to prepareOutput middleware [… #388

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/middleware/prepareOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,20 @@ module.exports = function (options, excludedMap) {
if (options.postProcess) {
if (promise && typeof promise.then === 'function') {
promise.then(() => {
options.postProcess(req, res)
options.postProcess(req, res);
if (options.restify) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Replace ············ with ··············

Suggested change
if (options.restify) {
if (options.restify) {

next();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Replace next(); with ··next()

Suggested change
next();
next()

}
}).catch(errorHandler(req, res, next))
} else {
options.postProcess(req, res)
options.postProcess(req, res);
if (options.restify) {
next();
}
}
} else {
if (options.restify) {
next();
}
}
}
Expand Down
120 changes: 110 additions & 10 deletions test/unit/middleware/prepareOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('prepareOutput', () => {
onError.reset()
outputFn.reset()
outputFnPromise.reset()
postProcess.reset()
next.reset()
})

Expand All @@ -39,23 +40,70 @@ describe('prepareOutput', () => {
sinon.assert.notCalled(next)
})

it('calls outputFn with default options and no post* middleware (async)', () => {
it('calls outputFn with default options and no post* middleware and next when restify option', () => {
let req = {
method: 'GET',
erm: {}
}

let options = {
restify: true,
onError: onError,
outputFn: outputFnPromise
outputFn: outputFn
}

prepareOutput(options)(req, {}, next)

sinon.assert.calledOnce(outputFnPromise)
sinon.assert.calledWithExactly(outputFnPromise, req, {})
sinon.assert.calledOnce(outputFn)
sinon.assert.calledWithExactly(outputFn, req, {})
sinon.assert.notCalled(onError)
sinon.assert.notCalled(next)
sinon.assert.calledOnce(next)
})

it('calls outputFn with default options and no post* middleware (async)', (done) => {
let req = {
method: 'GET',
erm: {}
}

let options = {
onError: onError,
outputFn: outputFnPromise
}

prepareOutput(options)(req, {}, next)

outputFnPromise().then(() => {
sinon.assert.calledTwice(outputFnPromise)
sinon.assert.calledWithExactly(outputFnPromise, req, {})
sinon.assert.notCalled(onError)
sinon.assert.notCalled(next)
done()
})

})

it('calls outputFn with default options and no post* middleware and next when restify option (async)', (done) => {
let req = {
method: 'GET',
erm: {}
}

let options = {
restify: true,
onError: onError,
outputFn: outputFnPromise
}

prepareOutput(options)(req, {}, next)

outputFnPromise().then(() => {
sinon.assert.calledTwice(outputFnPromise)
sinon.assert.calledWithExactly(outputFnPromise, req, {})
sinon.assert.notCalled(onError)
sinon.assert.calledOnce(next)
done()
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codacy has a fix for the issue: Delete ;

Suggested change
});
})

})

it('calls postProcess with default options and no post* middleware', () => {
Expand All @@ -80,25 +128,77 @@ describe('prepareOutput', () => {
sinon.assert.notCalled(next)
})

it('calls postProcess with default options and no post* middleware (async outputFn)', () => {
it('calls postProcess with default options and no post* middleware and next when restify option', () => {
let req = {
method: 'GET',
erm: {}
}

let options = {
restify: true,
onError: onError,
outputFn: outputFnPromise,
outputFn: outputFn,
postProcess: postProcess
}

prepareOutput(options)(req, {}, next)

sinon.assert.calledOnce(outputFnPromise)
sinon.assert.calledWithExactly(outputFnPromise, req, {})
sinon.assert.calledOnce(outputFn)
sinon.assert.calledWithExactly(outputFn, req, {})
sinon.assert.calledOnce(postProcess)
sinon.assert.calledWithExactly(postProcess, req, {})
sinon.assert.notCalled(onError)
sinon.assert.notCalled(next)
sinon.assert.calledOnce(next)
})

it('calls postProcess with default options and no post* middleware (async)', (done) => {
let req = {
method: 'GET',
erm: {}
}

let options = {
onError: onError,
outputFn: outputFnPromise,
postProcess: postProcess
}

prepareOutput(options)(req, {}, next)

outputFnPromise().then(() => {
sinon.assert.calledTwice(outputFnPromise)
sinon.assert.calledWithExactly(outputFnPromise, req, {})
sinon.assert.calledOnce(postProcess)
sinon.assert.calledWithExactly(postProcess, req, {})
sinon.assert.notCalled(onError)
sinon.assert.notCalled(next)
done()
});
})

it('calls postProcess with default options and no post* middleware and next with restify option (async)', (done) => {
let req = {
method: 'GET',
erm: {}
}

let options = {
restify: true,
onError: onError,
outputFn: outputFnPromise,
postProcess: postProcess
}

prepareOutput(options)(req, {}, next)

outputFnPromise().then(() => {
sinon.assert.calledTwice(outputFnPromise)
sinon.assert.calledWithExactly(outputFnPromise, req, {})
sinon.assert.calledOnce(postProcess)
sinon.assert.calledWithExactly(postProcess, req, {})
sinon.assert.notCalled(onError)
sinon.assert.calledOnce(next)
done()
});
})
})