Skip to content

Commit

Permalink
server: fixes #1124, clear the disk cache whenever launching the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mann committed Feb 14, 2018
1 parent 4af9c98 commit e38ada8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 9 deletions.
12 changes: 8 additions & 4 deletions packages/server/lib/browsers/chrome.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,23 @@ module.exports = {
args = newArgs
.then =>
Promise.all([
## ensure that we have a chrome profile dir
utils.ensureProfile(browserName)
## ensure that we have a clean cache dir
## before launching the browser every time
utils.ensureCleanCache(browserName)

@_writeExtension(options.proxyUrl, options.socketIoRoute)
])
.spread (dir, dest) ->
.spread (cacheDir, dest) ->
## normalize the --load-extensions argument by
## massaging what the user passed into our own
args = _normalizeArgExtensions(dest, args)

userDir = utils.getProfileDir(browserName)

## this overrides any previous user-data-dir args
## by being the last one
args.push("--user-data-dir=#{dir}")
args.push("--user-data-dir=#{userDir}")
args.push("--disk-cache-dir=#{cacheDir}")

debug("launch in chrome: %s, %s", url, args)

Expand Down
14 changes: 12 additions & 2 deletions packages/server/lib/browsers/electron.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,22 @@ module.exports = {
if ua = options.userAgent
@_setUserAgent(win.webContents, ua)

if ps = options.proxyServer
@_setProxy(win.webContents, ps)
setProxy = =>
if ps = options.proxyServer
@_setProxy(win.webContents, ps)

Promise.join(
setProxy(),
@_clearCache(win.webContents)
)
.then ->
win.loadURL(url)
.return(win)

_clearCache: (webContents) ->
new Promise (resolve) ->
webContents.session.clearCache(resolve)

_setUserAgent: (webContents, userAgent) ->
## set both because why not
webContents.setUserAgent(userAgent)
Expand Down
13 changes: 10 additions & 3 deletions packages/server/lib/browsers/utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ fs = Promise.promisifyAll(fs)
profiles = appData.path("browsers")

module.exports = {
ensureProfile: (name) ->
p = path.join(profiles, name)
getProfileDir: (name) ->
path.join(profiles, name)

fs.ensureDirAsync(p).return(p)
ensureCleanCache: (name) ->
p = path.join(profiles, name, "CypressCache")

fs
.removeAsync(p)
.then ->
fs.ensureDirAsync(p)
.return(p)

copyExtension: (src, dest) ->
fs.copyAsync(src, dest)
Expand Down
27 changes: 27 additions & 0 deletions packages/server/test/e2e/cache_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ replacerRe = /(<h1>)\w+(<\/h1>)/

e2ePath = Fixtures.projectPath("e2e")

requestsForCache = 0

onServer = (app) ->
app.post "/write/:text", (req, res) ->
file = path.join(e2ePath, "index.html")
Expand All @@ -19,6 +21,13 @@ onServer = (app) ->
fs.writeFile file, str, (err) ->
res.sendStatus(200)

app.get "/cached", (req, res) ->
requestsForCache += 1

res
.set("cache-control", "public, max-age=3600")
.send("this response will be disk cached")

describe "e2e cache", ->
e2e.setup({
servers: {
Expand All @@ -37,3 +46,21 @@ describe "e2e cache", ->
snapshot: true
expectedExitCode: 0
})

it "clears cache when browser is spawned", ->
e2e.exec(@, {
spec: "cache_clearing_spec.coffee"
expectedExitCode: 0
})
.then =>
## only 1 request should have gone out
expect(requestsForCache).to.eq(1)

e2e.exec(@, {
spec: "cache_clearing_spec.coffee"
expectedExitCode: 0
})
.then ->
## and after the cache is cleaned before
## opening the browser, it'll make a new request
expect(requestsForCache).to.eq(2)
4 changes: 4 additions & 0 deletions packages/server/test/scripts/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const glob = Promise.promisify(require('glob'))

const options = minimist(process.argv.slice(2))

if (options.browser) {
process.env.BROWSER = options.browser
}

const started = new Date()

let numFailed = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
req = (win) ->
new Promise (resolve, reject) ->
rand = Math.random()

xhr = new win.XMLHttpRequest()
xhr.open("GET", "http://localhost:1515/cached/")
xhr.onload = -> resolve(win)
xhr.onerror = reject
xhr.send()

it "makes cached request", ->
cy
.visit("http://localhost:1515")
.then(req) ## this creates the disk cache
.then(req) ## this should not hit our server
1 change: 1 addition & 0 deletions packages/server/test/unit/browsers/electron_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe "lib/browsers/electron", ->
context "._launch", ->
beforeEach ->
@sandbox.stub(menu, "set")
@sandbox.stub(electron, "_clearCache").resolves()
@sandbox.stub(electron, "_setProxy").resolves()
@sandbox.stub(electron, "_setUserAgent")

Expand Down

0 comments on commit e38ada8

Please sign in to comment.