I have an API that I am trying to test. It uses hawk authentication. I have successfully test failure codes using supertest, but I'm not sure how to include the hawk authentication within test script. I have found and installed superagent-hawk, which states it does work with supertest. Unfortunately, I am fairly new to all this and am unsure how to set it up.
Here's what I have (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();
});
});
});
});
});
When I try to run mocha test.js
, I get the following:
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
I've also tried:
var request = require('supertest', 'superagent-hawk')('mywebsite:3000/');
but that gave me the following, which was not unexpected:
TypeError: request.get(...).hawk is not a function
My working code looks like the following:
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();
});
});
});
});
});
Okay, I figured it out, partially by looking at superagent-hawk's own tests/hawk.js file, and by playing around. Here's how I did it:
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();
});
});
});
});
});