Skip to content

Commit

Permalink
fixed cache bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fehguy committed Jun 6, 2018
1 parent 0bcbe10 commit de7d8d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 5 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,15 @@ module.exports.cacheWith = (cache) => {

module.exports.fromCache = () => {
return new Promise((resolve, reject) => {
if(container.router) {
resolve(container.router);
if(container.swambda) {
resolve(container.swambda);
}
else {
reject();
}
});
};

module.exports.cache = (router) => {
if(container) {
container.router = router;
}
};

const Swambda = module.exports.Swambda = class Swambda {
constructor(prefix) {
this.prefix = prefix;
Expand Down Expand Up @@ -110,8 +104,8 @@ Swambda.prototype.cors = function (extra) {

Swambda.prototype.load = function (identifier) {
return new Promise((resolve, reject) => {
if(container.router) {
resolve(container.router);
if(container.swambda) {
resolve(container.swambda);
return;
}
const self = this;
Expand All @@ -125,7 +119,7 @@ Swambda.prototype.load = function (identifier) {
router.compilePaths(self.spec.paths);
self.router = router;

container.router = router;
container.swambda = self;
resolve(self);
})
.catch((err) => {
Expand Down
12 changes: 10 additions & 2 deletions test/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("routing", () => {
});
});

it("routes an valid query parameter", (done) => {
it("uses the cache", (done) => {
let cache = {};
swambda.cacheWith(cache);
const event = {
Expand All @@ -40,7 +40,15 @@ describe("routing", () => {
.then((response) => {
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toEqual([{foo: "bar"}]);
done();
swambda.fromCache()
.then((res) => {
res.process(event)
.then((response) => {
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toEqual([{foo: "bar"}]);
done();
})
});
})
.catch(err => {
done(err);
Expand Down

0 comments on commit de7d8d8

Please sign in to comment.