From 3b3ea1a4c3535f9fd41b4b34895057003ba120e6 Mon Sep 17 00:00:00 2001 From: Taylor Downs Date: Tue, 26 Sep 2017 16:35:26 -0300 Subject: [PATCH] enhanced test coverage, fixed setAuth --- lib/Utils.js | 4 +- package.json | 2 +- src/Utils.js | 5 +- test/index.js | 130 ++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 132 insertions(+), 9 deletions(-) diff --git a/lib/Utils.js b/lib/Utils.js index ab8e4e4..5162c3f 100644 --- a/lib/Utils.js +++ b/lib/Utils.js @@ -8,11 +8,11 @@ exports.setAuth = setAuth; exports.assembleError = assembleError; exports.tryJson = tryJson; function setUrl(configuration, path) { - if (!configuration) return path;else if (configuration.baseUrl) return configuration.baseUrl + path;else return path; + if (configuration && configuration.baseUrl) return configuration.baseUrl + path;else return path; } function setAuth(configuration, manualAuth) { - if (manualAuth) return manualAuth;else if (configuration.username) return { + if (manualAuth) return manualAuth;else if (configuration && configuration.username) return { 'username': configuration.username, 'password': configuration.password, 'sendImmediately': configuration.authType != 'digest' diff --git a/package.json b/package.json index 288f885..20aab17 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "language-http", - "version": "1.0.4", + "version": "1.0.5", "description": "An HTTP request language package for use with Open Function", "main": "lib/index.js", "scripts": { diff --git a/src/Utils.js b/src/Utils.js index dab0e27..0710542 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -1,12 +1,11 @@ export function setUrl(configuration, path) { - if ( !configuration ) return path - else if ( configuration.baseUrl ) return configuration.baseUrl + path + if ( configuration && configuration.baseUrl ) return configuration.baseUrl + path else return path } export function setAuth(configuration, manualAuth) { if ( manualAuth ) return manualAuth - else if ( configuration.username ) return { + else if ( configuration && configuration.username ) return { 'username': configuration.username, 'password': configuration.password, 'sendImmediately': ( configuration.authType != 'digest' ) diff --git a/test/index.js b/test/index.js index ba0bc85..66bffeb 100644 --- a/test/index.js +++ b/test/index.js @@ -2,9 +2,31 @@ import Adaptor from '../src'; import { expect } from 'chai'; import nock from 'nock'; -const { execute, get } = Adaptor; +const { execute, get, post, put, patch, del } = Adaptor; -describe("execute", () => { +function stdGet(state) { + return execute( + get("https://www.example.com/api/fake", {}) + )(state) + .then((nextState) => { + const { data, references } = nextState; + expect(data).to.eql({ httpStatus: 'OK', message: 'the response' }) + expect(references).to.eql([{ "triggering": "event" }]) + }) +} + +function clientReq(method, state) { + return execute( + method("https://www.example.com/api/fake", {}) + )(state) + .then((nextState) => { + const { data, references } = nextState; + expect(data).to.eql({ httpStatus: 'OK', message: 'the response' }) + expect(references).to.eql([{"a":1}]) + }) +} + +describe("The execute() function", () => { it("executes each operation in sequence", (done) => { let state = {} @@ -41,10 +63,11 @@ describe("execute", () => { }) }) -describe("get", () => { +describe("The get() function", () => { before(() => { nock('https://www.example.com') + .persist() .get('/api/fake') .reply(200, { httpStatus:'OK', @@ -75,4 +98,105 @@ describe("get", () => { }) + it("works without a baseUrl", () => { + let state = { + configuration: { + username: "hello", + password: "there" + }, + data: { "triggering": "event" } + }; + return stdGet(state) + }) + + it("works with an empty set of credentials", () => { + let state = { + configuration: {}, + data: { "triggering": "event" } + }; + return stdGet(state) + }) + + it("works with no credentials (null)", () => { + let state = { + configuration: null, + data: { + "triggering": "event" + } + }; + return stdGet(state) + }) + +}) + + +describe("The client", () => { + + before(() => { + nock('https://www.example.com') + .get('/api/fake') + .reply(200, { + httpStatus:'OK', + message: 'the response' + }); + + nock('https://www.example.com') + .post('/api/fake') + .reply(200, { + httpStatus:'OK', + message: 'the response' + }); + + nock('https://www.example.com') + .put('/api/fake') + .reply(200, { + httpStatus:'OK', + message: 'the response' + }); + + nock('https://www.example.com') + .patch('/api/fake') + .reply(200, { + httpStatus:'OK', + message: 'the response' + }); + + nock('https://www.example.com') + .delete('/api/fake') + .reply(200, { + httpStatus:'OK', + message: 'the response' + }); + }) + + const stdState = { + "configuration": null, + "data": {"a": 1} + } + + it("works with GET", () => { + let state = stdState; + clientReq(get, state); + }) + + it("works with POST", () => { + let state = stdState; + clientReq(post, state); + }) + + it("works with PATCH", () => { + let state = stdState; + clientReq(patch, state); + }) + + it("works with POST", () => { + let state = stdState; + clientReq(put, state); + }) + + it("works with POST", () => { + let state = stdState; + clientReq(del, state); + }) + })