简体   繁体   中英

NodeJS testing HTTPS server with supertest

I read about supertest . I managed to test two of my routes:

it('notAuthorized', function (done) {
    request.agent(server)
        .get('/notAuthorized')
        .expect('Content-Type', /html/)
        .expect(200)
        .expect(/Not authorized!/)
        .end(done)
})

it('redirect', function (done) {
    request.agent(server)
        .get('/no-valid-route')
        .expect('Content-Type', /plain/)
        .expect(302)
        .expect('Location', '/notAuthorized')
        .expect(/Moved Temporarily/)
        .end(done)
})


However, the problem starts when I want to access my other pages, those I need to register for. I found this solution for a regular registration:

describe('When logged in', function () {
    before(function (done) {
        // login the user
        agent
            .post('/users/session')
            .field('email', 'foobar@example.com')
            .field('password', 'foobar')
            .end(done)
    })

    // ...
})


In my application I register with a certificate. Can I somehow configure the test with my certificate? Changing my https options doesn't work either:

///////////////////
// https options
var options = {
    // ...
    requestCert: false,
    rejectUnauthorized: false
};


I assume it is because of my middle ware I use in every of my routes:

 exports.isAuthenticated = function(req, res, next){
    if(req.client.authorized) {
        // user authorized
        if(!req.session.user) {
            // set user session
            var cert = req.connection.getPeerCertificate().subject;
        // ..


// profile
app.get("/profile", mw.isAuthenticated, profileController.getProfile);

// user
app.get("/users", mw.isAuthenticated, userController.getUsers);

// chat
app.get("/chat", mw.isAuthenticated, chatController.getChat);


Question:

  • is there anyway I can configure the agent with my certificate?
  • should I maybe overthink the design of using the isAuthenticated middle ware in every route?
  • can I somehow change the cookie object of supertest's agent?

If I could set the req object like the following snippet, I'd probably have a solution.

    req : {
        client : {
            authorized : true
        },
        connection :{
            getPeerCertificate : function(){
                this.subject = 1;
            }
        }
    }

The short answer is that you have to monkey patch supertest's TestAgent class, superagent's Agent class, and superagent's 'request' export from lib/node/index.js.

Superagent is only designed to track the CA associated with a server, and as far as I can tell supertest doesn't even support that. The underlying request is actually made in index.js, and there's no API hook into any of the cert or key options you need to pass.

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