My current implementation works nicely to return the response. However, looking at the response, the header's host is connecting to a random port on my localhost IP.
Two options that I have tried with supertest:
var app = request('http://localhost:3000'); //not very neat with request being my supertest variable.
var app = request(require('../src/server')); & var app = request.agent(require('../src/server')); where that directory is my exported express app.
The first option connects to the right port, however the body is always undefined because bodyParser is not used and I need it to send encoded data.
The second option seems to include everything my express app does, but the host is always inconsistent!
Manually everything works fine, and the testing implementation seems fine too. Here is my test.js:
'use strict';
var request = require('supertest');
var should = require('chai').should();
var winston = require('winston'); //for logs
var mongoose = require('mongoose');
var app = request.agent(require('../src/server')); //express app
var token = '';
var refreshToken = '';
var clientData = {
id: 'test',
secret: 'test'
};
var basic = {'Authorization': 'Basic ' + new Buffer(clientData.id + ':' + clientData.secret).toString('base64'),
'Content-Type': 'application/json'
};
var bearer = {'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
};
var accessForm = {
username: 'matie',
password : '123',
grant_type: 'password'
};
var refreshForm = {
refreshToken: refreshToken,
grant_type: 'refresh_token'
};
var oauthExchange = function(data, next){
app.post('/oauth/token')
.set(basic) //adds header
.send(data) //sends form data
.end(next); //handles response
}
//Tests for OAuth ---------------------------------------------------------------------------------------
describe('Authentication', function(){
before(function(done) {
mongoose.createConnection('mongodb url');
done();
});
//testing password endpoint
describe('Grant type: Password', function(){
this.timeout(20000); //20 seconds timeout
it('should work and return a refresh token', function(done){
accessForm.scope = 'offline_access';
oauthExchange(accessForm, function(err, res){
//validate response with chai before calling done
console.log(res);
done();
});
});
it('should work and NOT return a refresh token', function(done){
accessForm.scope = 'undefined';
oauthExchange(accessForm, function(err, res){
//validate response with chai before calling done
done();
});
});
});
});
I'm more concerned with the second try with supertest. Why isn't it using the express server that is already running? All tutorials and examples of supertest require the express server but I have never seen one with Passport tokens implementation.
Does this have anything to do with sessions? Please let me know if you need a snapshot of my server script.
http://stackoverflow.com/a/18466430/1974981 The comment in this answer says, I can run the tests without running the server.