这对我有用:
...
var user = request.agent({ca: cert});
...
完整示例:
var expect = require('chai').expect;
var should = require('should');
var request= require('superagent');
var fs = require('fs');
var cert = fs.readFileSync('sslcert/server.crt', 'utf8');
var validUser = { username: 'test@test.com', password: 'secret111' };
describe('User', function() {
// provide certificate as agent parameter
var user = request.agent({ca: cert});
it("/login", function(done) {
user
.get('https://localhost:3000/login')
.end(function(err, res) {
if(err) throw err;
// HTTP status should be 200
res.status.should.equal(200);
user
.post('https://localhost:3000/login')
.send(validUser)
.end(function(err, res) {
if(err) throw err;
// HTTP status should be 200
res.status.should.equal(200);
done();
// user will manage its own cookies
// res.redirects contains an Array of redirects
});
});
});
it("/", function(done) {
user
.get('https://localhost:3000/')
.end(function(err, res) {
if(err) throw err;
// HTTP status should be 200
res.status.should.equal(200);
done();
});
});
it("/logout", function(done) {
user
.get('https://localhost:3000/logout')
.end(function(err, res) {
if(err) throw err;
// HTTP status should be 200
res.status.should.equal(200);
done();
});
});
});