-
Notifications
You must be signed in to change notification settings - Fork 46
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
Eviction tests using cache manager #146
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
195 changes: 195 additions & 0 deletions
195
packages/core/test/static-client-caching-eviction.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright (c) 2019, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
"use strict"; | ||
|
||
import chai from "chai"; | ||
import chaiAsPromised from "chai-as-promised"; | ||
import nock from "nock"; | ||
|
||
const expect = chai.expect; | ||
|
||
before(() => { | ||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
}); | ||
|
||
import { BaseClient } from "../src/base/client"; | ||
import { _get } from "../src/base/staticClient"; | ||
|
||
const RESPONSE_DATA = { mock: "data" }; | ||
const RESPONSE_DATA_MODIFIED = { mock: "data_modified" }; | ||
|
||
describe("base client evict cache tests", () => { | ||
afterEach(nock.cleanAll); | ||
|
||
it("cache manager caches asset on HTTP 200 response", () => { | ||
const client = new BaseClient({ baseUri: "https://somewhere" }); | ||
const scope = nock("https://somewhere") | ||
.get("/validate-fresh") | ||
.reply(200, RESPONSE_DATA, { ETag: "etag" }); | ||
|
||
// make first request and get response from server | ||
return _get({ | ||
client: client, | ||
path: "/validate-fresh" | ||
}).then(() => { | ||
//ensure all http calls are done | ||
expect(nock.isDone()).to.be.true; | ||
|
||
// check cacheManager has the content | ||
// construct request object with url | ||
const request = { url: "https://somewhere/validate-fresh" }; | ||
// options object with cacheManager | ||
const opts = { | ||
cacheManager: client.clientConfig.cacheManager | ||
}; | ||
|
||
return client.clientConfig.cacheManager.match(request, opts).then(res => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
return res.json().then(resData => { | ||
// ensure response data is cached | ||
expect(resData).to.eql(RESPONSE_DATA); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it("cache manager does not cache asset on no-store response header", () => { | ||
const client = new BaseClient({ baseUri: "https://somewhere" }); | ||
const scope = nock("https://somewhere") | ||
.get("/validate-no-store") | ||
.reply(200, RESPONSE_DATA, { "Cache-Control": "no-store" }); | ||
|
||
// make first request and get response from server | ||
return _get({ | ||
client: client, | ||
path: "/validate-no-store" | ||
}).then(() => { | ||
//ensure all http calls are done | ||
expect(nock.isDone()).to.be.true; | ||
|
||
// check cacheManager has the content | ||
// construct request object with url | ||
const request = { url: "https://somewhere/validate-no-store" }; | ||
// options object with cacheManager | ||
const opts = { | ||
cacheManager: client.clientConfig.cacheManager | ||
}; | ||
|
||
return client.clientConfig.cacheManager.match(request, opts).then(res => { | ||
expect(res).to.be.undefined; | ||
}); | ||
}); | ||
}); | ||
|
||
it("cache manager updates asset headers on HTTP 304 response", () => { | ||
const client = new BaseClient({ baseUri: "https://somewhere" }); | ||
const scope = nock("https://somewhere") | ||
.get("/validate-304-update") | ||
.reply(200, RESPONSE_DATA, { ETag: "etag" }); | ||
|
||
// make first request and get response from server | ||
return _get({ | ||
client: client, | ||
path: "/validate-304-update" | ||
}).then(() => { | ||
//ensure all http calls are done | ||
expect(nock.isDone()).to.be.true; | ||
|
||
//define new etag on fresh content. | ||
scope.get("/validate-304-update").reply( | ||
304, | ||
function() { | ||
//verify if sdk adds if-none-match header | ||
expect(this.req.headers["if-none-match"][0]).to.eql("etag"); | ||
}, | ||
{ ETag: "etag_modified", newHeader: "a new header" } | ||
); | ||
return _get({ | ||
client: client, | ||
path: "/validate-304-update" | ||
}).then(() => { | ||
//ensure all http calls are done | ||
expect(nock.isDone()).to.be.true; | ||
// check cacheManager has the content | ||
// construct request object with url | ||
const request = { url: "https://somewhere/validate-304-update" }; | ||
// options object with cacheManager | ||
const opts = { | ||
cacheManager: client.clientConfig.cacheManager | ||
}; | ||
return client.clientConfig.cacheManager | ||
.match(request, opts) | ||
.then(res => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
expect(res.headers.get("etag")).to.eql("etag_modified"); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
expect(res.headers.get("newHeader")).to.eql("a new header"); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it("cache manager evicts cached asset from cache on fresh content", () => { | ||
const client = new BaseClient({ baseUri: "https://somewhere" }); | ||
const scope = nock("https://somewhere") | ||
.get("/evict-modified") | ||
.reply(200, RESPONSE_DATA, { ETag: "etag" }); | ||
|
||
// make first request and get response from server | ||
return _get({ | ||
client: client, | ||
path: "/evict-modified" | ||
}).then(() => { | ||
//ensure all http calls are done | ||
expect(nock.isDone()).to.be.true; | ||
|
||
// check cacheManager has the content | ||
// construct request object with url | ||
const request = { url: "https://somewhere/evict-modified" }; | ||
// options object with cacheManager | ||
const opts = { | ||
cacheManager: client.clientConfig.cacheManager | ||
}; | ||
|
||
return client.clientConfig.cacheManager.match(request, opts).then(res => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
return res.json().then(resData => { | ||
// ensure response data is cached | ||
expect(resData).to.eql(RESPONSE_DATA); | ||
//define fresh response | ||
scope.get("/evict-modified").reply(200, function() { | ||
//verify if sdk adds if-none-match header | ||
expect(this.req.headers["if-none-match"][0]).to.eql("etag"); | ||
return RESPONSE_DATA_MODIFIED; | ||
}); | ||
return _get({ | ||
client: client, | ||
path: "/evict-modified" | ||
}).then(() => { | ||
//ensure all http calls are done | ||
expect(nock.isDone()).to.be.true; | ||
// check cacheManager has the fresh content | ||
return client.clientConfig.cacheManager | ||
.match(request, opts) | ||
.then(res => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
return res.json().then(resData => { | ||
expect(resData).to.eql(RESPONSE_DATA_MODIFIED); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
2020?
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.
Will take care of it next PR...