forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_polyfill_spec.js
163 lines (151 loc) · 4.01 KB
/
fetch_polyfill_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const systemTests = require('../lib/system-tests').default
const { stripIndent } = require('common-tags')
const bodyParser = require('body-parser')
const onServer = function (app) {
app.use(bodyParser.json({ extended: false }))
app.get('/get-json', (req, res) => {
return res.json([1, 2, 3])
})
app.get('/get-text', (req, res) => {
return res.send('pong')
})
app.post('/add', (req, res) => {
if (req.body.method !== 'add') {
throw new Error('wrong body method')
}
return res.json({ answer: req.body.a + req.body.b })
})
// page posts a JSON object
app.get('/addition', (req, res) => {
return res.send(stripIndent`
<body>
<div id="result"></div>
<script>
const data = {
method: 'add',
a: 2,
b: 15
}
fetch('/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then((response) => {
if (!response) {
throw new Error('no response')
}
if (!response.ok) {
throw new Error('response is not ok')
}
return response.json()
}).then(j => {
// either answer from the server
// or from the network stub
if (j.answer !== 17 && j.answer !== 193) {
throw new Error('wrong answer')
}
document.getElementById('result').innerText = 'answer: ' + j.answer
})
</script>
</body>
`)
})
// page fetches JSON array
app.get('/first', (req, res) => {
return res.send(stripIndent`
<body>
<script>
fetch('/get-json')
.then((response) => {
if (!response) {
throw new Error('no response')
}
if (!response.ok) {
throw new Error('response is not ok')
}
return response.json()
})
.then((list) => {
if (list.length !== 3) {
throw new Error('Wrong number of items')
}
if (list[0] !== 1) {
throw new Error('Wrong first item')
}
if (list[1] !== 2) {
throw new Error('Wrong second item')
}
if (list[2] !== 3) {
throw new Error('Wrong third item')
}
})
</script>
<a href="/second">second</a>
</body>
`)
})
// page fetches text
app.get('/second', (req, res) => {
return res.send(stripIndent`
<body>
<div id="result"></div>
<script>
fetch('/get-text')
.then((response) => {
if (!response) {
throw new Error('no response')
}
if (!response.ok) {
throw new Error('response is not ok')
}
if (response.status !== 200) {
throw new Error('response status not 200')
}
return response.text()
})
.then((text) => {
// allow response from the server
// or stub response
if (text !== 'pong' && text !== 'mock pong') {
throw new Error('Wrong text response')
}
document.getElementById('result').innerText = 'text: ' + text
})
</script>
</body>
`)
})
}
describe('e2e fetch polyfill', () => {
systemTests.setup({
servers: {
port: 1818,
onServer,
},
})
systemTests.it('passes', {
browser: '!webkit', // TODO(webkit): fix+unskip
spec: 'fetch.cy.js',
snapshot: false,
config: {
experimentalFetchPolyfill: true,
},
})
})
describe('e2e no fetch polyfill', () => {
systemTests.setup({
servers: {
port: 1818,
onServer,
},
})
systemTests.it('passes', {
spec: 'fetch_no_polyfill.cy.js',
snapshot: false,
config: {
experimentalFetchPolyfill: false,
},
})
})