From 97d778634dd5bf11cfd9e9b3f21fa11a3dd21153 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 14 Nov 2014 11:05:30 -0400 Subject: [PATCH] Implement Authorization header support in server module --- lib/server/server.coffee | 66 ++++++++++++++++++++++------------- lib/server/server.spec.coffee | 35 +++++++++++++++++++ 2 files changed, 77 insertions(+), 24 deletions(-) diff --git a/lib/server/server.coffee b/lib/server/server.coffee index 009fa762..9aa9e34b 100644 --- a/lib/server/server.coffee +++ b/lib/server/server.coffee @@ -1,38 +1,56 @@ request = require('request') urljoin = require('url-join') +async = require('async') config = require('../config') +token = require('../token/token') -exports.request = (method = 'GET', uri, json, callback) -> +exports.request = (method = 'GET', url, json, callback) -> method = method.toUpperCase() - uri = urljoin(config.baseUrl, uri) + url = urljoin(config.baseUrl, url) - request { - uri - method - json - }, (error, response, body) -> - try - response.body = JSON.parse(response.body) + async.waterfall [ - if response?.statusCode >= 400 - error = new Error(response.body) + (callback) -> + token.getToken(callback) - return callback?.call(null, error, response, body) + (savedToken, callback) -> + requestOptions = { + url + method + json + } -exports.get = (uri, callback) -> - return exports.request('GET', uri, null, callback) + if savedToken? + requestOptions.headers = + 'Authorization': "Bearer #{savedToken}" -exports.head = (uri, callback) -> - return exports.request('HEAD', uri, null, callback) + request(requestOptions, callback) -exports.delete = (uri, callback) -> - return exports.request('DELETE', uri, null, callback) + (response, body, callback) -> + try + response.body = JSON.parse(response.body) -exports.post = (uri, json, callback) -> - return exports.request('POST', uri, json, callback) + if response?.statusCode >= 400 + error = new Error(response.body) -exports.put = (uri, json, callback) -> - return exports.request('PUT', uri, json, callback) + return callback(error, response, body) -exports.patch = (uri, json, callback) -> - return exports.request('PATCH', uri, json, callback) + ], callback + +exports.get = (url, callback) -> + return exports.request('GET', url, null, callback) + +exports.head = (url, callback) -> + return exports.request('HEAD', url, null, callback) + +exports.delete = (url, callback) -> + return exports.request('DELETE', url, null, callback) + +exports.post = (url, json, callback) -> + return exports.request('POST', url, json, callback) + +exports.put = (url, json, callback) -> + return exports.request('PUT', url, json, callback) + +exports.patch = (url, json, callback) -> + return exports.request('PATCH', url, json, callback) diff --git a/lib/server/server.spec.coffee b/lib/server/server.spec.coffee index 87a30d23..14296d8e 100644 --- a/lib/server/server.spec.coffee +++ b/lib/server/server.spec.coffee @@ -2,6 +2,8 @@ expect = require('chai').expect nock = require('nock') server = require('./server') config = require('../config') +token = require('../token/token') +johnDoeFixture = require('../../tests/fixtures/johndoe.json') TEST_URI = config.baseUrl @@ -120,3 +122,36 @@ describe 'Server:', -> describe '#patch()', -> it('should be a facade to request()', checkRequestTypeWithBody('PATCH', { hello: 'world' })) + + describe 'given there is a token', -> + + beforeEach (done) -> + token.saveToken(johnDoeFixture.token, done) + + describe '#request()', -> + + it 'should send the Authorization header', (done) -> + + server.request 'GET', URI.ok, null, (error, response) -> + authorizationHeader = response.request.headers.Authorization + + expect(error).to.not.exist + expect(authorizationHeader).to.exist + expect(authorizationHeader).to.equal("Bearer #{johnDoeFixture.token}") + done() + + describe 'given there is not a token', -> + + beforeEach (done) -> + token.clearToken(done) + + describe '#request()', -> + + it 'should not send the Authorization header', (done) -> + server.request 'GET', URI.ok, null, (error, response) -> + authorizationHeader = response.request.headers.Authorization + + expect(error).to.not.exist + expect(authorizationHeader).to.not.exist + done() +