Commit 2dd9c1c7 authored by Caleb Weeks's avatar Caleb Weeks

adding tests for get-if-exists selector

parent 728f6465
......@@ -147,13 +147,6 @@ SC.get = function(path, depth, callback, options){
if(path[0]=="/") path=path.slice(1);
//deref selector
if(this.hasDerefCapability){
if((this.derefByDefault && !options.dontDeref) || options.deref){
path = `${path}.${selectors.deref}`;
}
}
//depth selector and JSON ext
path = `${path}${depth?("." + depth):""}.json`;
......@@ -218,6 +211,14 @@ SC.getSling = function(path, options = {}, callback){
}
//deref selector
if(this.hasDerefCapability){
if((this.derefByDefault && !options.dontDeref) || options.deref){
//inject as the first selector
path = sc_self.util.addSelector(path, selectors.deref);
}
}
// get if exists selector
const useGetIfExists =
this.hasGetIfExistsCapabililty && //is the capability enabled
......@@ -225,19 +226,21 @@ SC.getSling = function(path, options = {}, callback){
((this.useGetIfExistsByDefault && !options.dontUseGetIfExists) || options.useGetIfExists); //and this request should use
if(useGetIfExists){
//inject as the first selector
path = path.split(".").splice(1, 0, selectors.getIfExists).join(".");
path = sc_self.util.addSelector(path, selectors.getIfExists);
}
var preprocess = this.preprocess(path, {method:"GET", options, callback});
path=preprocess.path;
if(this.inMemoryCache && !options.inMemoryCacheRefresh){
if(sc_self.inMemoryCache && !options.inMemoryCacheRefresh){
// if this path hasn't already been requested, stick the promise verision of it
// in the cache.
inMemoryCache[path] = inMemoryCache[path] || sc_self.getSling(path, {...options, inMemoryCacheRefresh: true});
sc_self.inMemoryCache[path] = sc_self.inMemoryCache[path] || sc_self.getSling(path, {...options, inMemoryCacheRefresh: true});
inMemoryCache.then( data => callback(null, data), err => callback(err) );
sc_self.inMemoryCache[path].then( data => callback(null, data), err => callback(err) );
}else{
if(preprocess.proxy){
......@@ -294,7 +297,7 @@ SC.getSling = function(path, options = {}, callback){
followRedirect: false,
auth:sc_self.makeAuth(),
headers: sc_self.authCookieHeader(),
agent: this.requestAgent,
agent: sc_self.requestAgent,
gzip: true
};
......@@ -585,7 +588,7 @@ SC.getStream = function(path, method, headers){
followRedirect: false,
auth:this.makeAuth(),
headers: this.authCookieHeader(),
agent: this.requestAgent,
agent: sc_self.requestAgent,
gzip: true
})
......@@ -646,7 +649,7 @@ SC.import = function(path, data, options, callback){
followRedirect: false,
auth: this.makeAuth(),
headers: this.authCookieHeader(),
agent: this.requestAgent,
agent: sc_self.requestAgent,
gzip: true
},
function(err, httpResponse, body){
......@@ -691,7 +694,7 @@ SC.post = function(path,params,cb){
followRedirect: false,
auth: this.makeAuth(),
headers: this.authCookieHeader(),
agent: this.requestAgent,
agent: sc_self.requestAgent,
gzip: true
};
......@@ -801,6 +804,13 @@ SC.util.getNonMeta=function(data, keyProperty, iterator){
}
SC.util.addSelector = (path, selector) => {
//inject as the first selector
let pathParts = path.split(".");
pathParts.splice(1, 0, selector);
return pathParts.join(".");
}
//////////////////////////////////////////////////////////////////////////
/**
......
module.exports = require("./exists.get-if-exists.json");
\ No newline at end of file
......@@ -4,6 +4,6 @@ module.exports = {
"headers": "some headers"
},
"data": {
"exists": true
"key" : "value"
}
}
\ No newline at end of file
module.exports = {
"res": {
"statusCode": 200,
"headers": "some headers"
},
"data": {
"exists": true
}
}
\ 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();
});
test('Call getSling with no options', done => {
sc.getSling('test1', (err, data) => {
expect(data['something']).toBe('here');
......@@ -19,8 +24,18 @@ test('Call getSling with leaveMangledNames option', done => {
})
});
test('default requestAgent', () => {
expect(request).toBeCalledWith
test('default requestAgent', async () => {
await sc.getSling('test1');
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].agent).toBeTruthy();
});
test('custom requestAgent', async () => {
const customRequestAgent = new Object();
const localSc = sc.clone({requestAgent: customRequestAgent})
await localSc.getSling('test1');
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].agent).toStrictEqual(customRequestAgent);
});
test('Call getSling with invalid path', () => {
......@@ -31,10 +46,107 @@ test('Call getSling with invalid path', () => {
});
// test("Call getSling with same params with inMemoryCache", async () => {
test("Call getSling with same params without inMemoryCache", async () => {
const result1 = await sc.getSling('test1');
const result2 = await sc.getSling('test1');
const result3 = await sc.getSling('test1');
expect(request).toBeCalledTimes(3);
expect(result1).toStrictEqual(result2);
expect(result2).toStrictEqual(result3);
});
test("Call getSling with same params with inMemoryCache", async () => {
let cacheableSc = sc.cacheableClone();
const result1 = await cacheableSc.getSling('test1');
const result2 = await cacheableSc.getSling('test1');
const result3 = await cacheableSc.getSling('test1');
expect(request).toBeCalledTimes(1);
expect(result1).toStrictEqual(result2);
expect(result2).toStrictEqual(result3);
});
test("Call getSling with get-if-exists (default) with no other selectors", async () => {
let clonedSc = sc.clone({useGetIfExistsByDefault: true, hasGetIfExistsCapabililty: true});
const validExistingResult = await clonedSc.getSling('getIfExists/exists.json');
expect(validExistingResult).toHaveProperty("key");
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].uri).toMatch(/^.*\.get-if-exists.json$/);
});
test("Call getSling with get-if-exists (default) with number selector", async () => {
let clonedSc = sc.clone({useGetIfExistsByDefault: true, hasGetIfExistsCapabililty: true});
const validExistingResult = await clonedSc.getSling('getIfExists/exists.1.json');
expect(validExistingResult).toHaveProperty("key");
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].uri).toMatch(/^.*\.get-if-exists.1.json$/);
});
test("Call getSling with get-if-exists (default) with text selector", async () => {
let clonedSc = sc.clone({useGetIfExistsByDefault: true, hasGetIfExistsCapabililty: true});
const validExistingResult = await clonedSc.getSling('getIfExists/exists.infinity.json');
expect(validExistingResult).toHaveProperty("key");
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].uri).toMatch(/^.*\.get-if-exists.infinity.json$/);
});
test("Call getSling with get-if-exists (default) with text and deref selector", async () => {
let clonedSc = sc.clone({useGetIfExistsByDefault: true, hasGetIfExistsCapabililty: true, hasDerefCapability: true});
const validExistingResult = await clonedSc.getSling('getIfExists/exists.infinity.json', {deref: true});
// });
expect(validExistingResult).toHaveProperty("key");
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].uri).toMatch(/^.*\.get-if-exists.deref.infinity.json$/);
});
test("Call getSling with get-if-exists (option) with text and deref selector", async () => {
let clonedSc = sc.clone({hasGetIfExistsCapabililty: true, hasDerefCapability: true});
const validExistingResult = await clonedSc.getSling('getIfExists/exists.infinity.json', {deref: true, useGetIfExists: true});
expect(validExistingResult).toHaveProperty("key");
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].uri).toMatch(/^.*\.get-if-exists.deref.infinity.json$/);
});
test("Call getSling with get-if-exists (option) against non-existing", async () => {
let clonedSc = sc.clone({hasGetIfExistsCapabililty: true});
try{
await clonedSc.getSling('getIfExists/doesnt-exist.json', {useGetIfExists: true})
} catch (e){
expect(e).toBeInstanceOf(Error);
expect(e.message).toMatch("The requested resource doesn't exist");
}
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].uri).toMatch(/^.*\.get-if-exists.json$/);
});
test("Call getSling with get-if-exists (option) with invalid response", async () => {
let clonedSc = sc.clone({hasGetIfExistsCapabililty: true});
try{
await clonedSc.getSling('getIfExists/invalid-res.json', {useGetIfExists: true})
} catch (e){
expect(e).toBeInstanceOf(Error);
expect(e.message).toMatch("The requested resource doesn't exist");
}
expect(request).toBeCalled();
expect(_.first(request.mock.calls)[0].uri).toMatch(/^.*\.get-if-exists.json$/);
});
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