简体   繁体   中英

How to use mutual SSL with test framework Mocha/Chai(chai-http) on a Node.js application

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 :

  • request.socket.authorized = undefined

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 :

  • request.socket.authorized = undefined
  • I get an ERR_INVALID_PROTOCOL exception on the mocha test

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);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM