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

set default when bodyParser is true #258

Merged
merged 2 commits into from
Jul 17, 2021
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules/
coverage/
npm-debug.log
stats.json
yarn-error.log
yarn-error.log
.idea
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ module.exports = {
route.callOptions = opts.callOptions;

// Create body parsers as middlewares
if (opts.bodyParsers == null) {
if (opts.bodyParsers == null || opts.bodyParsers === true) {
// Set default JSON body-parser
opts.bodyParsers = {
json: true
Expand Down
38 changes: 38 additions & 0 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,44 @@ describe("Test body-parsers", () => {
beforeAll(() => {
});

describe("JSON parser should be set to default", () => {
it("When its value is null / undefined", () => {
[broker, service, server] = setup({
routes: [{}]
});

expect(Array.isArray(service.routes) && service.routes.length === 1).toBeTruthy();
const middlewares = service.routes[0].middlewares;
expect(Array.isArray(middlewares) && middlewares.length === 1).toBeTruthy();
expect(typeof middlewares[0] === "function" && middlewares[0].name === "jsonParser").toBeTruthy();
});

it("When its value is true", () => {
[broker, service, server] = setup({
routes: [{
"bodyParsers": true
}]
});

expect(Array.isArray(service.routes) && service.routes.length === 1).toBeTruthy();
const middlewares = service.routes[0].middlewares;
expect(Array.isArray(middlewares) && middlewares.length === 1).toBeTruthy();
expect(typeof middlewares[0] === "function" && middlewares[0].name === "jsonParser").toBeTruthy();
});

it("But not truly", () => {
[broker, service, server] = setup({
routes: [{
"bodyParsers": 1
}]
});

expect(Array.isArray(service.routes) && service.routes.length === 1).toBeTruthy();
const middlewares = service.routes[0].middlewares;
expect(Array.isArray(middlewares) && middlewares.length === 0).toBeTruthy();
});
});

it("POST /api/test.gretter without bodyParsers", () => {
[broker, service, server] = setup({
routes: [{
Expand Down