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

Shouldn't mutate original data in res.end #51

Open
wants to merge 1 commit into
base: master
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
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ const resolveRequestModelSchema = (req) => {
};

const sendData = (res, data, encoding) => {
// 'res.end' requires a Buffer or String so if it's not one, create a String
if (!(data instanceof Buffer) && !_.isString(data)) {
data = JSON.stringify(data);
}
res.end(data, encoding);
};

Expand Down Expand Up @@ -201,7 +197,7 @@ const validateResponse = (req, res, next) => {
debug(` Response validation errors: \n${util.inspect(validator.errors)}`);
if (options.responseValidationFn) {
options.responseValidationFn(req, val, validator.errors);
sendData(res, val, encoding);
sendData(res, data, encoding);
} else {
const err = {
message: `Response schema validation failed for ${req.method}${req.originalUrl}`,
Expand All @@ -213,7 +209,7 @@ const validateResponse = (req, res, next) => {
}
} else {
debug('Response validation success');
sendData(res, val, encoding);
sendData(res, data, encoding);
}
}
};
Expand Down
39 changes: 39 additions & 0 deletions test/pet-store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,45 @@ describe('pet store', () => {
})
.end(done);
});

it('should process valid string response', (done) => {
const router = Router();
router.get('/firstname/:username', (req, res) => {
res.json('firstname1');
});
const app = createServer(router, optsValidateAll);

request(app)
.get('/firstname/user1')
.expect(200)
.end(done);
});

it('fails with 500 response code due to invalid response string return', (done) => {
const router = Router();
router.get('/firstname/:username', (req, res) => {
res.json({});
});
const app = createServer(router, optsValidateAll);

request(app)
.get('/firstname/user1')
.expect(500)
.end(done);
});

it('should process valid boolean response', (done) => {
const router = Router();
router.get('/isValidUser/:username', (req, res) => {
res.json(false);
});
const app = createServer(router, optsValidateAll);

request(app)
.get('/isValidUser/user1')
.expect(200)
.end(done);
});
});

describe('supports overriding ajv settings', () => {
Expand Down
60 changes: 60 additions & 0 deletions test/swagger-schemas/pet-store.json
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,66 @@
}
}
}
},
"/firstname/{username}": {
"get": {
"tags": [
"user"
],
"summary": "Get user first name by user name",
"description": "",
"operationId": "getUserByName",
"produces": [
"application/json"
],
"parameters": [
{
"name": "username",
"in": "path",
"description": "The name that needs to be fetched. Use user1 for testing. ",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "string"
}
}
}
}
},
"/isValidUser/{username}": {
"get": {
"tags": [
"user"
],
"summary": "Checks if it is a valid username",
"description": "",
"operationId": "getUserByName",
"produces": [
"application/json"
],
"parameters": [
{
"name": "username",
"in": "path",
"description": "The name that needs to be fetched. Use user1 for testing. ",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "boolean"
}
}
}
}
}
},
"securityDefinitions": {
Expand Down