我有一个我想测试的API。它使用鹰派认证。我已经使用supertest成功测试了失败代码,但我不确定如何在测试脚本中包含hawk身份验证。我找到并安装了superagent-hawk,它表示它确实可以使用supertest。不幸的是,我对这一切都很陌生,我不确定如何设置它。
这是我的(test.js):
var should = require('should');
var superTest = require('supertest')('mywebsite:3000/');
var addHawk = require('superagent-hawk');
var request = addHawk(superTest);
describe('controlllers', function() {
describe('preregControllers', function() {
describe('GET /mypage', function() {
it('should be response code 200', function(done) {
var creds = {
"id": "testUser",
"key": "testPass",
"algorithm": "sha256"
}
request
.get('mypage')
.hawk(creds)
.set('Accept', 'applications/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
done();
});
});
});
});
});
当我尝试运行mocha test.js
时,我得到以下内容:
mocha test.js
~/Projects/myproject/node_modules/superagent-hawk/index.js:9
: superagent.Request.prototype;
^
TypeError: Cannot read property 'prototype' of undefined
at module.exports (~/Projects/myproject/node_modules/superagent-hawk/index.js:9:43)
at Object.<anonymous> (~/Projects/myproject/test/api/controllers/test.js:4:16)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at /usr/local/lib/node_modules/mocha/lib/mocha.js:219:27
at Array.forEach (native)
at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:216:14)
at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:468:10)
at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:140:18)
at node.js:1001:3
我也尝试过:
var request = require('supertest', 'superagent-hawk')('mywebsite:3000/');
但这给了我以下内容,这并非出人意料:
TypeError: request.get(...).hawk is not a function
我的工作代码如下所示:
var should = require('should');
var response = require('supertest')('mywebsite:3000/');
describe('controlllers', function() {
describe('preregControllers', function() {
describe('GET /mypage', function() {
it('should be response code 401', function(done) {
request
.get('mypage')
.set('Accept', 'applications/json')
.expect('Content-Type', /json/)
.expect(401,{"category":"AUTHORIZATION","context":"Validating user credentials","message":"Unauthorized"})
.end(function(err, res) {
if (err) return done(err);
done();
});
});
});
});
});
答案 0 :(得分:0)
好吧,我想出来了,部分是通过查看superagent-hawk自己的测试/ hawk.js文件,以及玩游戏。我是这样做的:
var should = require('should');
var addHawk = require('superagent-hawk');
var superTest = addHawk(require('supertest'));
var request = superTest('mywebsite:3000/');
describe('controlllers', function() {
describe('preregControllers', function() {
describe('GET /mypage', function() {
it('should be response code 200', function(done) {
var creds = {
"id": "testUser",
"key": "testPass",
"algorithm": "sha256"
}
request
.get('mypage')
.hawk(creds)
.set('Accept', 'applications/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
if (err) return done(err);
done();
});
});
});
});
});