我如何让 superagent-hawk 与 supertest 一起工作

How do I get superagent-hawk to work with supertest

我有一个 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();
          });
      });
    });
  });
});

好吧,我想通了,部分是通过查看 superagent-hawk 自己的 tests/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();
          });
      });
    });
  });
});