I'm developing an application that use mutual SSL authentication and I want to write automated test to evaluate the functionality.
I've already implemented the server and I could test is with Postman. This post is working well.
In my mocha test I wrote this request :
chai.request(getServer())
.post('/users')
.ca(fs.readFileSync(path.join(process.cwd(), 'test', 'ca-crt.pem'), 'utf-8'))
.cert(path.join(process.cwd(), 'test', 'client1-key.pem'), 'utf-8'))
.key(path.join(process.cwd(), 'test', 'client1-crt'), 'utf-8'))
.send(userToCreate)
.end((error, response) => {
if (error !== null) {
reject(`User creation error : ${JSON.stringify(error)}`);
} else if (response.status !== 201) {
reject(`User creation failed : ${JSON.stringify(response.status)}`);
} else {
resolve(response.body);
}
});
But this request doesn't send any certificate to the server :
I've tryed to use an HTTPS agent :
let agent = new Agent({
ca: fs.readFileSync(path.join(process.cwd(), 'test', 'ca-crt.pem'), 'utf-8'),
key: fs.readFileSync(path.join(process.cwd(), 'test', 'client1-key.pem'), 'utf-8'),
cert: fs.readFileSync(path.join(process.cwd(), 'test', 'client1-crt.pem'), 'utf-8')
});
chai.request(getServer())
.post('/users')
.agent(agent)
.send(userToCreate)
.end((error, response) => {
if (error !== null) {
reject(`User creation error : ${JSON.stringify(error)}`);
} else if (response.status !== 201) {
reject(`User creation failed : ${JSON.stringify(response.status)}`);
} else {
resolve(response.body);
}
});
But this request doesn't send any certificate to the server :
Could someone help me please ?
I've finally solved this problem by using superagent directly instead of chai-http. Despite chai-http using superagent, it looks like the implementation missed the methods ca, cert and key. So the syntax beneath solved the problem for me:
superAgent
.post('http:/localhost/users')
.ca(fs.readFileSync(path.join(process.cwd(), 'test', 'ca-crt.pem'), 'utf-8'))
.cert(fs.readFileSync(path.join(process.cwd(), 'test', 'client1-key.pem'), 'utf-8'))
.key(fs.readFileSync(path.join(process.cwd(), 'test', 'client1-crt'), 'utf-8'))
.send(sentBody)
.end((error, response) => {
if (error !== null) {
reject(`User creation error : ${JSON.stringify(error)});
} else if (response.status !== 201) {
reject(`User creation failed : ${JSON.stringify(response.status)});
} else {
resolve(response.body);
}
}
});