【问题标题】:NodeJS testing HTTPS server with supertestNodeJS 用 supertest 测试 HTTPS 服务器
【发布时间】:2013-12-10 07:26:17
【问题描述】:

我读到了supertest。我设法测试了我的两条路线:

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


但是,当我想访问我需要注册的其他页面时,问题就开始了。我找到了这个定期注册的解决方案:

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

    // ...
})


在我的应用程序中,我使用证书注册。我可以用我的证书以某种方式配置测试吗?更改我的 https 选项也不起作用:

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


我认为这是因为我在每条路线中都使用了中间件:

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


问题:

  • 无论如何我可以使用我的证书配置代理吗?
  • 我是否应该过度考虑在每个路由中使用isAuthenticated 中间件的设计?
  • 我能不能改变超测代理的cookie对象?

如果我可以像下面的 sn-p 那样设置 req 对象,我可能会有一个解决方案。

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

【问题讨论】:

  • 你有没有机会解决这个问题。

标签: javascript node.js https certificate supertest


【解决方案1】:

简短的回答是,您必须对 supertest 的 TestAgent 类、superagent 的 Agent 类和 superagent 从 lib/node/index.js 的“请求”导出进行修补。

Superagent 仅用于跟踪与服务器关联的 CA,据我所知 supertest 甚至不支持。底层请求实际上是在 index.js 中发出的,并且您需要传递的任何证书或密钥选项都没有 API 挂钩。

【讨论】:

    猜你喜欢
    • 2014-09-02
    • 2018-03-08
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2023-04-10
    • 1970-01-01
    • 2014-03-21
    相关资源
    最近更新 更多