Skip to content

Commit

Permalink
test cache, add tests for microservices to test route onRequest/onRes…
Browse files Browse the repository at this point in the history
…ponse
  • Loading branch information
killmenot committed Dec 8, 2017
1 parent 5065053 commit 7d7adac
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 3 deletions.
41 changes: 39 additions & 2 deletions spec/integration/microservices/primary/qewd.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,50 @@ const config = {
method: 'GET',
destination: 'all_stores'
},
{
path: '/api/store/request/stocklist',
method: 'GET',
onRequest: (args, send, finished) => {
finished({
message: {
text: 'Hello from onRequest handler'
}
});
}
},
{
path: '/api/store/:destination/stocklist',
method: 'GET'
method: 'GET',
onResponse: (args) => {
if (args.message.query.onResponse === 'intercept') {
args.responseObj.message.text = 'The response was intercepted by onResponse handler.';
}

if (args.message.query.onResponse === 'handle') {
args.responseObj.message.text = 'The response was handled by onResponse handler.';
args.handleResponse(args.responseObj);

return true;
}
}
},
{
path: '/api/store/all/category/:category/stocklist',
method: 'GET',
destination: 'all_stores',
onResponse: (args) => {
args.responseObj.message.text = 'The response was handled by onResponse handler.';
args.handleResponse(args.responseObj);

return true;
}
},
{
path: '/api/store/:destination/category/:category/stocklist',
method: 'GET'
method: 'GET',
onResponse: (args) => {
args.responseObj.message.text = 'The response was intercepted by onResponse handler.';
}
}
]
}
Expand Down
3 changes: 3 additions & 0 deletions spec/integration/microservices/stock-list-ms/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ module.exports = {
'/api/store/:destination/stocklist': {
GET: getStockList
},
'/api/store/all/category/:category/stocklist': {
GET: getStockListByCategory
},
'/api/store/:destination/category/:category/stocklist': {
GET: getStockListByCategory
}
Expand Down
101 changes: 101 additions & 0 deletions spec/integration/microservices/testrunner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ describe('integration/qewd/microservices:', () => {
});
});

describe('GET /api/store/request/stocklist', () => {
it('should send request and be intercepted by route onRequest', (done) => {
request.
get('/api/store/request/stocklist').
set('authorization', `Bearer ${token}`).
expect(200).
expect(res => {
expect(res.body).toEqual({
text: 'Hello from onRequest handler'
});
}).
end(err => err ? done.fail(err) : done());
});
});

describe('GET /api/store/:destination/stocklist', () => {
it('should send request to dynamic destination', (done) => {
request.
Expand All @@ -146,6 +161,42 @@ describe('integration/qewd/microservices:', () => {
end(err => err ? done.fail(err) : done());
});

it('should send request and be intercepted by route onResponse (single destination)', (done) => {
request.
get('/api/store/store1/stocklist?onResponse=intercept').
set('authorization', `Bearer ${token}`).
expect(200).
expect(res => {
expect(res.body).toEqual({
store: 'store1',
ip: '127.0.0.1:8082',
stock: 'stock list here...',
text: 'The response was intercepted by onResponse handler.',
token: jasmine.any(String)
});
expect(utils.isJWT(res.body.token)).toBeTruthy();
}).
end(err => err ? done.fail(err) : done());
});

it('should send request and be handled by route onResponse (single destination)', (done) => {
request.
get('/api/store/store1/stocklist?onResponse=handle').
set('authorization', `Bearer ${token}`).
expect(200).
expect(res => {
expect(res.body).toEqual({
store: 'store1',
ip: '127.0.0.1:8082',
stock: 'stock list here...',
text: 'The response was handled by onResponse handler.',
token: jasmine.any(String)
});
expect(utils.isJWT(res.body.token)).toBeTruthy();
}).
end(err => err ? done.fail(err) : done());
});

it('should be able to do request with non existing destination', (done) => {
request.
get('/api/store/store3/stocklist').
Expand All @@ -161,4 +212,54 @@ describe('integration/qewd/microservices:', () => {
end(err => err ? done.fail(err) : done());
});
});

describe('GET /api/store/all/category/:category/stocklist', () => {
it('should send request to multiple destination and be handled by route onResponse (multiple destination)', (done) => {
request.
get('/api/store/all/category/toys/stocklist').
set('authorization', `Bearer ${token}`).
expect(200).
expect(res => {
expect(res.body).toEqual({
results: {
store1: {
ip: '127.0.0.1:8082',
category: 'toys',
stock: 'stock list for toys here...'
},
store2: {
ip: '127.0.0.1:8082',
category: 'toys',
stock: 'stock list for toys here...'
}
},
text: 'The response was handled by onResponse handler.',
token: jasmine.any(String)
});
expect(utils.isJWT(res.body.token)).toBeTruthy();
}).
end(err => err ? done.fail(err) : done());
});
});

describe('GET /api/store/:destination/category/:category/stocklist', () => {
it('should send request and be intercepted by route onResponse (multiple destination)', (done) => {
request.
get('/api/store/store1/category/games/stocklist').
set('authorization', `Bearer ${token}`).
expect(200).
expect(res => {
expect(res.body).toEqual({
store: 'store1',
ip: '127.0.0.1:8082',
category: 'games',
stock: 'stock list for games here...',
text: 'The response was intercepted by onResponse handler.',
token: jasmine.any(String)
});
expect(utils.isJWT(res.body.token)).toBeTruthy();
}).
end(err => err ? done.fail(err) : done());
});
});
});
10 changes: 9 additions & 1 deletion spec/integration/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ module.exports = {

db: () => {
switch (process.env.DATABASE) {
case 'cache': return {type: 'cache'};
case 'cache': return {
type: 'cache',
params: {
path: process.env.CACHE_MGR_PATH,
username: process.env.CACHE_USERNAME,
password: process.env.CACHE_PASSWORD,
namespace: process.env.CACHE_NAMESPACE
}
};
case 'gtm': return {type: 'gtm'};
default: return {type: 'redis'};
}
Expand Down

0 comments on commit 7d7adac

Please sign in to comment.