Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
enhanced test coverage, fixed setAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Sep 26, 2017
1 parent fd36af2 commit 3b3ea1a
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
5 changes: 2 additions & 3 deletions src/Utils.js
Original file line number Diff line number Diff line change
@@ -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' )
Expand Down
130 changes: 127 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
})

})

0 comments on commit 3b3ea1a

Please sign in to comment.