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