forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_spec.js
74 lines (61 loc) · 1.7 KB
/
cache_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require('express')
const fs = require('fs')
const path = require('path')
const Fixtures = require('../lib/fixtures')
const systemTests = require('../lib/system-tests').default
const replacerRe = /(<h1>)\w+(<\/h1>)/
const e2ePath = Fixtures.projectPath('e2e')
let requestsForCache = 0
const onServer = function (app) {
app.use(express.static(e2ePath, {
// force caching to happen
maxAge: 3600000,
}))
app.post('/write/:text', (req, res) => {
const file = path.join(e2ePath, 'index.html')
return fs.readFile(file, 'utf8', (err, str) => {
// replace the word between <h1>...</h1>
str = str.replace(replacerRe, `$1${req.params.text}$2`)
return fs.writeFile(file, str, (err) => {
return res.sendStatus(200)
})
})
})
app.get('/cached', (req, res) => {
requestsForCache += 1
return res
.set('cache-control', 'public, max-age=3600')
.send('this response will be disk cached')
})
}
describe('e2e cache', () => {
systemTests.setup({
servers: {
port: 1515,
onServer,
},
})
it('passes', function () {
return systemTests.exec(this, {
spec: 'cache.cy.js',
snapshot: true,
})
})
it('clears cache when browser is spawned', function () {
return systemTests.exec(this, {
spec: 'cache_clearing.cy.js',
})
.then(() => {
// only 1 request should have gone out
expect(requestsForCache).to.eq(1)
return systemTests.exec(this, {
spec: 'cache_clearing.cy.js',
})
.then(() => {
// and after the cache is cleaned before
// opening the browser, it'll make a new request
expect(requestsForCache).to.eq(2)
})
})
})
})