hapi
hapi copied to clipboard
how to use server.inject and input validation false
Support plan
- is this issue currently blocking your project? (yes/no): yes
- is this issue affecting a production system? (yes/no): no
Context
- node version: 12.9.0
- module version: 19.1.1
- environment (e.g. node, browser, native): mocha
- used with (e.g. hapi application, another framework, standalone, ...): hapi-auto-route,
- any other relevant information:
How can we help?
trying to setup unit tests for my handler, and it seems like the validate: false option for inject doesn't disable route input validation. documentation suggests that setting this to false should turn off request payload validation. my tests will start working once i comment out the validation in my route options. is there some other options to pass to server.inject to disable input validation ? or is it recommended to always pass a payload with every test case?
route config
const options: RouteOptions = {
tags: ['api'],
auth: false,
response: {
status: {
201: Joi.string().example("created"),
409: Joi.string().example("already exists"),
500: Joi.string().example("server encountered an error")
}
},
validate: {
payload: Joi.object({
firstName: Joi.string().required(),
lastName: Joi.string().required(),
email: Joi.string().required(),
password: Joi.string().required(),
timezone: Joi.string().required(),
}).label('registrationRequest'),
},
handler
};
test case
const postSignUp = async (body?: RegistrationRequestBody) =>
await server.inject({
method: 'POST',
url: '/api/v1/auth/registration',
validate: false,
payload: body
});
it('should throw 500 when server error', async () => {
userExistsStub.rejects(new Error("oops"));
const result = await postSignUp();
expect(result.statusCode).to.equal(500);
});
1) v1 auth
postSignUp
should throw 500 when server error:
AssertionError: expected 400 to equal 500
+ expected - actual
-400
+500
The option is to disable validating the injection options, not the route validation. There is no way to disable route validation.