mercredi 6 mai 2015

request not being called

In the following code, apiCaller._get, method request.get is not being called when I run my tests. I am relatively new to node, so am a little lost on why its not being triggered. Do I need to instantiate a server by any chance?

apiCaller

var http = require("http");
var querystring = require("querystring");
var qs = require("qs");
var _ = require("underscore");
var _environ = require("./configs.json").environment;
var request = require("request");

var apiCaller = {};

apiCaller.token = null;

// Lambda does not require the http server and does not run if one if provided.

if (_environ === "develop" || _environ === "nightly") {

    var server = http.createServer(function (req, res) {
    });

    server.listen(8080);
    console.log("Server running");
}

apiCaller._get = function (event, context, callback) {

    // get the parameters for our querytring
    var oauthParams = _.pick(event, "client_id", "client_secret", "grant_type");

    // create the querystring
    var params = querystring.stringify(oauthParams);

    var get_options = {
        uri: event.host + "/some/where/token?" + params,
        headers : {
            'Content-Type': "application/json",
            'Accept': "application/json"
        }
    };

    function _callback (error, response, body) {
        if (error) {
            if ( !context ) {
                console.error("Something went wrong with the api response: %s", error);
                callback(error);
                return;
            }
            context.done(new Error("Something went wrong with the api response."));
        }

        if (!error && response.statusCode === 200) {
            apiCaller.token = JSON.parse(body).access_token;

            // we want to stop the request if token is not correct
            if ( !apiCaller.token ) {
                if ( !context ) {
                    console.error("Something went wrong with the token. Wrong token! Token: %s", apiCaller.token);
                    return;
                }
                console.error("Token: %s", apiCaller.token);
                context.done(new Error("Something went wrong with the token. Wrong token!"));
            }
            callback(null, apiCaller.token);
        }
    }

    request.get(get_options, _callback);
};

tests , which so far only call apiCaller._get

var chai = require('chai');
var sinonChai = require("sinon-chai");
var expect = chai.expect;
var extend = require('lodash').extend;
var sinon = require('sinon');
chai.use(sinonChai);
var nock = require("nock");
var proxyquire = require('proxyquire');

describe("ApiCaller", function () {
    describe("Success calls", function () {
        describe("apiCaller_get", function () {
            var scope, queryStub, reqGetSpy, callbackSpy, config, responseOptions, testedModule, params;

            before(function () {

                config = {
                    "host": "wherever.com",
                    "client_id": "somethinggood",
                    "client_secret": "alongpassword",
                    "grant_type": "client_credentials",
                    "environment": "live"
                }

                params = "client_id=somethinggood&client_secret=alongpassword&grant_type=client_credentials";

                responseOptions = '{access_token: "123456789"}';

                callbackSpy = sinon.spy();
                reqGetStub = sinon.stub();
                reqPostStub = sinon.stub();

                scope = nock(config.host)
                    .get("/some/where/token?" + params)
                    .reply(200, responseOptions);

                testedModule = proxyquire("../apiCaller", {
                    //"request": {
                    //    "get": reqGetSpy
                    //},
                    //"querystring": {
                    //    "stringify": queryStub
                    //}
                });

                testedModule._get( config, null, function (err) {console.log("Called!")});

            });

            it("trigger callback with token", function () {
            });
        });
    });

});

Aucun commentaire:

Enregistrer un commentaire