mru*_*zan 5 mocha.js node.js express chai
我在 node express 框架中创建了一些成功返回的测试用例。但是,有一些 API 调用需要经过身份验证才能进行。我可以通过 ajax 在 reactJs 中调用需要身份验证的 API 并给出成功响应,但是当我在 chai-Http 中设置相同的标头时,它失败并显示invalid token.
var chai = require('chai');
var should = chai.should();
var chaiHttp = require('chai-http');
chai.use(chaiHttp);
var server = require('../app');
describe('routes : index', function() {
var token = '';
beforeEach(function(done) {
done();
});
before(function(done) {
chai.request(server)
.post('/doSignin')
.send({uname:'ruzan@test.com',password:'123123'})
.end(function(err, res) {
res.redirects.length.should.equal(0);
res.status.should.equal(200);
token = res.body.user.token;
res.type.should.equal('application/json');
done();
});
});
afterEach(function(done) {
done();
});
describe('Test FolderController', function() {
it('should list all the folders', function(done) {
console.log(token); // here it print the same token
chai.request(server)
.get('/notes/get-notes')
.set('prg-header' , token)
.end(function(err, res) {
// console.log(res);
res.status.should.equal(200);
done();
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
ReactJs:
$.ajax({
url: '/notes/get-notes',
method: "GET",
dataType: "JSON",
headers: { 'prg-header':loggedUser.token }
}).done( function (data, text) {
console.log(data);
if(data.status.code == 200){
// it come here the data
}
}.bind(this));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4529 次 |
| 最近记录: |