4

supertestについて読みました。2 つのルートをテストすることができました。

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 オブジェクトをどうにか変更できますか?

次のスニペットのように req オブジェクトを設定できれば、おそらく解決策があるでしょう。

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

1 に答える 1

1

簡単に言えば、スーパーテストの TestAgent クラス、スーパーエージェントのエージェント クラス、および lib/node/index.js からのスーパーエージェントの「リクエスト」エクスポートにモンキー パッチを適用する必要があるということです。

Superagent は、サーバーに関連付けられた CA を追跡するようにのみ設計されており、私が知る限り、supertest はそれをサポートしていません。基になるリクエストは実際には index.js で行われ、渡す必要がある証明書またはキー オプションのいずれにも API フックはありません。

于 2014-12-26T18:38:46.087 に答える