Commit bcd238c1 authored by Caleb Weeks's avatar Caleb Weeks

Merge branch 'master' of gitlab.entropy.cc:blacklight/npm-sling-connector into...

Merge branch 'master' of gitlab.entropy.cc:blacklight/npm-sling-connector into issue/2-promisifying-async-functions
parents 70a03f68 241b240b
{
"name": "sling-connector",
"version": "1.0.11",
"version": "1.0.17",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......
{
"name": "sling-connector",
"version": "1.0.11",
"version": "1.0.17",
"description": "Helps connect to Sling API endpoints",
"main": "sling-connector.js",
"scripts": {
"test": "jest --coverage",
"prepush": "npm run test"
"test": "jest --coverage"
},
"jest": {
"coverageReporters": ["text", "text-summary"]
"coverageReporters": [
"text",
"text-summary"
]
},
"author": "Entropy Multimedia, Inc.",
"license": "ISC",
"dependencies": {
"agentkeepalive": "^4.0.2",
"debug": "^3.1.0",
"lodash": "^4.17.10",
"mkdirp": "^0.5.1",
......@@ -20,7 +23,8 @@
"request-debug": "^0.2.0"
},
"devDependencies": {
"jest": "^24.8",
"husky": "^0.14.3"
"@types/jest": "^24.0.15",
"husky": "^0.14.3",
"jest": "^24.8"
}
}
This diff is collapsed.
module.exports = {
"res": {
"statusCode": 200,
"headers": "some headers"
},
"data": {
"exists": false
}
}
\ No newline at end of file
module.exports = require("./exists.get-if-exists.json");
\ No newline at end of file
module.exports = require("./exists.get-if-exists.json");
\ No newline at end of file
module.exports = require("./exists.get-if-exists.json");
\ No newline at end of file
{
"res": {
"statusCode": 200,
"headers": "some headers"
},
"data": {
"something": "here",
"jcr:something": "also here",
"another": {
"deeper": "one"
}
}
module.exports = {
"res": {
"statusCode": 200,
"headers": "some headers"
},
"data": {
"exists": true,
"data" : {
"key" : "value"
}
}
}
\ No newline at end of file
module.exports = {
"res": {
"statusCode": 200,
"headers": "some headers"
},
"data": {
"key" : "value"
}
}
\ No newline at end of file
module.exports = require('./simple.json');
\ No newline at end of file
......@@ -4,7 +4,8 @@ module.exports = {
"headers": "some headers"
},
"data": {
"something": "here",
"key": "value",
"something": "here",
"jcr:something": "also here",
"another": {
"deeper": "one"
......
module.exports = jest.fn(function (reqOptions, reqCallback) {
let testUri = reqOptions.uri;
testUri = testUri.replace("/content/", "/").replace("/etc/", "/");
let uriParts = testUri.split('/');
let fixturesIndex = uriParts.indexOf('__fixtures__');
uriParts.splice(fixturesIndex + 1, 0, 'requests');
const test = require(uriParts.join('/'));
uriParts.splice(fixturesIndex + 1, 0, 'request');
let test;
try{
test = require(uriParts.join('/'));
} catch (e){
test = {
err: `Failed to require ${uriParts.join('/')} ` + e.message,
statusCode: 500
}
}
reqCallback(test.err ? test.err : null, test.res, JSON.stringify(test.data));
})
\ No newline at end of file
})
const SlingConnector = require('../sling-connector');
describe('isValidPath with default invalidPathRegex', () => {
const sc = new SlingConnector({baseUri: "https://localhost:4502"});
test('undefined path', () => {
expect(sc.isValidPath()).toBeFalsy();
});
test('non string path', () => {
expect(sc.isValidPath({})).toBeFalsy();
expect(sc.isValidPath([])).toBeFalsy();
expect(sc.isValidPath(1)).toBeFalsy();
expect(sc.isValidPath(true)).toBeFalsy();
});
test('string path', () => {
expect(sc.isValidPath("/content/some/path")).toBeTruthy();
});
test('invalid path', () => {
expect(sc.isValidPath("/undefined/some/path")).toBeFalsy();
expect(sc.isValidPath("undefined/some/path")).toBeFalsy();
expect(sc.isValidPath("/[object Object]/some/path")).toBeFalsy();
expect(sc.isValidPath(`/${{}}/some/path`)).toBeFalsy();
});
})
describe('isValidPath with custom invalidPathRegex', () => {
const sc = new SlingConnector({baseUri: "https://localhost:4502", invalidPathRegex: /something/});
test('default is now valid path', () => {
expect(sc.isValidPath("/undefined/some/path")).toBeTruthy();
expect(sc.isValidPath("undefined/some/path")).toBeTruthy();
expect(sc.isValidPath("/[object Object]/some/path")).toBeTruthy();
expect(sc.isValidPath(`/${{}}/some/path`)).toBeTruthy();
});
test('invalid path', () => {
expect(sc.isValidPath("/something")).toBeFalsy();
expect(sc.isValidPath("something")).toBeFalsy();
expect(sc.isValidPath("/content/something/path")).toBeFalsy();
});
})
\ No newline at end of file
const SlingConnector = require('../sling-connector');
test('Call cacheableClone from SC with no inMemoryCache', () => {
const sc = new SlingConnector({baseUri: "../__fixtures__"});
const clone = sc.cacheableClone();
Object.keys(clone).forEach( key => {
if(key === "inMemoryCache"){
expect(clone[key]).toEqual({});
}else{
expect(clone[key]).toStrictEqual(sc[key]);
}
});
});
test('Call cacheableClone from SC with inMemoryCache', () => {
const sc = new SlingConnector({baseUri: "../__fixtures__", inMemoryCache: {key: "value"}});
const clone = sc.cacheableClone();
Object.keys(clone).forEach( key => {
if(key === "inMemoryCache"){
expect(clone[key]).toEqual({});
}else{
expect(clone[key]).toStrictEqual(sc[key]);
}
});
});
\ No newline at end of file
const SlingConnector = require('../sling-connector');
const sc = new SlingConnector({baseUri: "../__fixtures__"});
test('Call clone with no overrides', () => {
const clone = sc.clone();
Object.keys(clone).forEach( key => {
expect(clone[key]).toStrictEqual(sc[key]);
});
})
test('Call clone with overrides', () => {
const overrides = {
baseUri: "http://new/",
username: "new",
password: "new",
invalidPathRegex: /^something/
};
const clone = sc.clone(overrides);
Object.keys(clone).forEach( key => {
if(overrides[key]){
expect(clone[key]).toStrictEqual(overrides[key]);
expect(clone[key]).not.toStrictEqual(sc[key]);
} else{
expect(clone[key]).toStrictEqual(sc[key]);
}
});
});
\ No newline at end of file
const SlingConnector = require('../sling-connector');
const {each, includes} = require('lodash');
const {each, includes, isRegExp} = require('lodash');
const optionDefaults = {
timeout: 20000,
......@@ -16,7 +16,11 @@ const optionDefaults = {
foss: "undefined",
queryType: "undefined",
mode: "undefined",
hasDerefCapability: "undefined"
hasDerefCapability: "undefined",
invalidPathRegex : /undefined|\[object Object\]/,
httpRequestAgent : "defined",
httpsRequestAgent : "defined",
inMemoryCache: "undefined"
}
const expectDefaults = (sc, ignoreKeys) => {
......@@ -24,7 +28,11 @@ const expectDefaults = (sc, ignoreKeys) => {
if(!includes(ignoreKeys,key)) {
if(defaultVal === "undefined") {
expect(sc[key]).toBeUndefined();
} else {
} else if(defaultVal === "defined") {
expect(sc[key]).toBeDefined();
} else if(isRegExp(defaultVal)){
expect(sc[key]).toStrictEqual(defaultVal);
}else {
expect(sc[key]).toBe(defaultVal);
}
}
......@@ -57,4 +65,18 @@ test('Initialize a Sling Connector with baseUri: "http://127.0.0.1:4502/" and ti
expect(sc.timeout).toBe(50000);
expect(sc.cacheTTL).toBe(12000);
expectDefaults(sc, ['timeout', 'cacheTTL']);
});
\ No newline at end of file
});
test('Initial SC with custom invalidPathRegex', () => {
const invalidPathRegex = /something/;
const sc = new SlingConnector({baseUri: "http://127.0.0.1:4502/", invalidPathRegex});
expect(sc.invalidPathRegex).toStrictEqual(invalidPathRegex);
expectDefaults(sc, ['invalidPathRegex']);
});
test('Initial SC with custom requestAgent', () => {
const httpRequestAgent = () => {};
const sc = new SlingConnector({baseUri: "http://127.0.0.1:4502/", httpRequestAgent});
expect(sc.httpRequestAgent).toStrictEqual(httpRequestAgent);
expectDefaults(sc, ['httpRequestAgent']);
});
......@@ -60,15 +60,11 @@ test('Call exists that gets an error using promise', done => {
})
})
test('Call exists that gets an error using promise that rejects on error', async done => {
test('Call exists that gets an error using promise that rejects on error', async () => {
try {
let exists = await sc.exists('exists/error', { rejectOnErr: true });
expect(exists).toBeFasly();
await sc.exists('exists/error', { rejectOnErr: true });
} catch (err) {
expect(err).toBeDefined();
expect(err).toBe("404 not found");
} finally {
done();
}
}
})
\ No newline at end of file
const SlingConnector = require('../sling-connector');
const sc = new SlingConnector({baseUri: "../__fixtures__"});
const request = require("request");
beforeEach(() => {
request.mockClear();
});
test('Call get with callback', done => {
sc.get('test1' , (err, data) => {
expect(data['something']).toBe('here');
done();
})
})
test('Call get without callback', async done => {
try{
let data = await sc.get('test1');
expect(data['something']).toBe("here");
}catch(err){
expect(err).toBeDefined();
}finally{
done();
}
})
\ No newline at end of file
describe('callback vs Promise', () => {
test('Call get without callback', async () => {
let data = await sc.get('/content/raw/simple');
expect(data['something']).toBe("here");
});
test('Call get with callback', done => {
sc.get('/content/raw/simple', 0, (err, data) => {
expect(data['something']).toBe('here');
done();
});
});
});
const SlingConnector = require('../sling-connector');
const sc = new SlingConnector({baseUri: "../__fixtures__"});
test('Call getRaw with callback', done => {
sc.getRaw('test1' , (err, data) => {
expect(data).toBe("{\"something\":\"here\",\"_jcr_something\":\"also here\",\"another\":{\"deeper\":\"one\"}}");
done();
})
})
test('Call get without callback', async done => {
try{
let data = await sc.getRaw('test1');
expect(data).toBe("{\"something\":\"here\",\"_jcr_something\":\"also here\",\"another\":{\"deeper\":\"one\"}}");
}catch(err){
expect(err).toBeDefined();
}finally{
done();
}
})
\ No newline at end of file
const request = require("request");
const SlingConnector = require('../sling-connector');
const _ = require("lodash");
const sc = new SlingConnector({baseUri: "../__fixtures__"});
beforeEach(() => {
request.mockClear();
});
describe('callback vs Promise', () => {
test('Call getRaw with callback', done => {
sc.getRaw('/content/raw/simple.json' , (err, data) => {
expect(data).toBe('{"key":"value","something":"here","_jcr_something":"also here","another":{"deeper":"one"}}');
done();
})
})
test('Call get without callback', async () => {
let data = await sc.getRaw('/content/raw/simple.json');
expect(data).toBe('{"key":"value","something":"here","_jcr_something":"also here","another":{"deeper":"one"}}');
})
});
describe("get-if-exists", ()=> {
test("not applied to standard JSON call", (done) => {
sc.getRaw("/content/raw/simple.1.json", (err, data) => {
expect(err).toBeFalsy();
expect(data).toBeTruthy();
expect(typeof data).toBe('string');
done();
});
});
});
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment