-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: cache middleware - OKTA-280090 #218
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,7 +71,7 @@ class Http { | |
|
||
let retriedOnAuthError = false; | ||
const execute = () => { | ||
const promise = this.prepareRequest(request) | ||
const getRequestPromise = () => this.prepareRequest(request) | ||
.then(() => this.requestExecutor.fetch(request)) | ||
.then(Http.errorFilter) | ||
.catch(error => { | ||
|
@@ -86,11 +86,11 @@ class Http { | |
}); | ||
|
||
if (!this.cacheMiddleware) { | ||
return promise; | ||
return getRequestPromise(); | ||
} | ||
|
||
const ctx = { | ||
uri, // TODO: remove unused property. req.url should be the key. | ||
uri, // TODO: remove unused property. req.url should be the key. OKTA-351525 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, I think it is a case of a duplicate property. We can choose which value should be used as the cache key. For some reason, which I don't recall, it was chosen to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's kept for backward compatibility, as the users are able to provide other cache options that depend on the I created OKTA-351525 to remove it in the next major version update. |
||
isCollection: context.isCollection, | ||
resources: context.resources, | ||
req: request, | ||
|
@@ -101,7 +101,7 @@ class Http { | |
return; | ||
} | ||
|
||
return promise.then(res => ctx.res = res); | ||
return getRequestPromise().then(res => ctx.res = res); | ||
}) | ||
.then(() => ctx.res); | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,22 +24,19 @@ async function next(ctx, body = '{}') { | |
}; | ||
} | ||
|
||
// TODO: use request.url per fetch spec: https://developer.mozilla.org/en-US/docs/Web/API/Request | ||
// https://oktainc.atlassian.net/browse/OKTA-280090 | ||
|
||
describe('Default cache middleware', () => { | ||
it('caches GET items with a \'self\' link', async () => { | ||
const cacheStore = new MemoryStore(); | ||
const ctx = { | ||
req: { | ||
method: 'get', | ||
uri: 'http://example.com/item' | ||
url: 'http://example.com/item' | ||
}, | ||
cacheStore | ||
}; | ||
const body = JSON.stringify(_.set({}, '_links.self.href', 'http://example.com/item')); | ||
await middleware(ctx, () => next(ctx, body)); | ||
expect(await cacheStore.get(ctx.req.uri)).to.equal(body); | ||
expect(await cacheStore.get(ctx.req.url)).to.equal(body); | ||
// make sure the middleware doesn't flush the stream | ||
expect(await ctx.res.text()).to.equal(body); | ||
}); | ||
|
@@ -49,14 +46,16 @@ describe('Default cache middleware', () => { | |
const ctx = { | ||
req: { | ||
method: 'get', | ||
uri: 'http://example.com/item' | ||
url: 'http://example.com/item' | ||
}, | ||
cacheStore | ||
}; | ||
const body = JSON.stringify(_.set({}, '_links.self.href', 'http://example.com/item')); | ||
await cacheStore.set(ctx.req.uri, body); | ||
const bodyObj = _.set({}, '_links.self.href', 'http://example.com/item'); | ||
const bodyStr = JSON.stringify(bodyObj); | ||
await cacheStore.set(ctx.req.url, bodyStr); | ||
await middleware(ctx, () => next(ctx)); | ||
expect(await ctx.res.text()).to.equal(body); | ||
expect(await ctx.res.json()).to.eql(bodyObj); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eql()? I couldn't find this matcher in the jest docs, what is it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tests under |
||
expect(await ctx.res.text()).to.equal(bodyStr); | ||
}); | ||
|
||
it('doesn\'t cache GET items without a \'self\' link', async () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note:
ctx.req.uri
was a typo and so the value was always undefined.ctx.req.url
holds a string value for the original uri of the request.