forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_submissions_spec.js
159 lines (137 loc) · 3.77 KB
/
form_submissions_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
const rp = require('@cypress/request-promise')
const path = require('path')
const bodyParser = require('body-parser')
const multer = require('multer')
const { fs } = require('@packages/server/lib/util/fs')
const systemTests = require('../lib/system-tests').default
const Fixtures = require('../lib/fixtures')
const HTTPS_PORT = 11443
const HTTP_PORT = 11180
describe('e2e form submissions', () => {
return systemTests.setup()
})
const e2ePath = Fixtures.projectPath('e2e')
const pathToLargeImage = Fixtures.path('server/large-img/earth.jpg')
const getFormHtml = (formAttrs, textValue = '') => {
return `\
<html>
<body>
<form method="POST" ${formAttrs}>
<input name="foo" type="text" value="${textValue}"/>
<input name="bar" type="file"/>
<input type="submit"/>
</form>
</body>
</html>\
`
}
const onServer = function (app) {
app.post('/verify-attachment', multer().any(), async (req, res) => {
const file = req.files[0]
const fixturePath = path.resolve(e2ePath, 'cypress', 'fixtures', req.body.foo)
const fixtureBuf = await fs.readFileAsync(fixturePath)
const uploadBuf = file.buffer
const ret = fixtureBuf.compare(uploadBuf)
if (ret === 0) {
return res.send('files match')
}
return res.send(
`\
file did not match. file at ${fixturePath} did not match uploaded buf.
<br/>
<hr/>
buffer compare yielded: ${ret}\
`,
)
})
// all routes below this point will have bodies parsed
app.use(bodyParser.text({
type: '*/*', // parse any content-type
}))
app.get('/', (req, res) => {
return res
.type('html')
.send(getFormHtml('action="/dump-body"'))
})
app.get('/multipart-form-data', (req, res) => {
return res
.type('html')
.send(getFormHtml('action="/dump-body" enctype="multipart/form-data"'))
})
app.get('/multipart-with-attachment', (req, res) => {
return res
.type('html')
.send(getFormHtml('action="/verify-attachment" enctype="multipart/form-data"', req.query.fixturePath))
})
return app.post('/dump-body', (req, res) => {
return res
.type('html')
.send(req.body)
})
}
describe('e2e forms', () => {
context('submissions with jquery XHR POST', () => {
systemTests.setup()
systemTests.it('passing', {
spec: 'form_submission_passing.cy.js',
snapshot: true,
})
systemTests.it('failing', {
spec: 'form_submission_failing.cy.js',
snapshot: true,
expectedExitCode: 1,
onStdout: (stdout) => {
return stdout
.replace(/((?: {6}-)+[^\n]+\n)/gm, '')
}, // remove variable diff
})
})
context('<form> submissions', () => {
systemTests.setup({
settings: {
e2e: {},
env: {
PATH_TO_LARGE_IMAGE: pathToLargeImage,
},
},
servers: [
{
port: HTTPS_PORT,
https: true,
onServer,
},
{
port: HTTP_PORT,
onServer,
},
],
})
before(() => {
// go out and fetch this image if we don't already have it
return fs
.readFileAsync(pathToLargeImage)
.catch({ code: 'ENOENT' }, () => {
// 16MB image, too big to include with git repo
return rp('https://test-page-speed.cypress.io/files/huge-image.jpg')
.then((resp) => {
return fs.outputFileAsync(pathToLargeImage, resp)
})
})
})
systemTests.it('passes with https on localhost', {
config: {
baseUrl: `https://localhost:${HTTPS_PORT}`,
},
spec: 'form_submission_multipart.cy.js',
snapshot: true,
})
systemTests.it('passes with http on localhost', {
config: {
baseUrl: `http://localhost:${HTTP_PORT}`,
e2e: {},
},
spec: 'form_submission_multipart.cy.js',
snapshot: true,
})
})
})