-
Notifications
You must be signed in to change notification settings - Fork 1
/
1289.js
65 lines (52 loc) · 1.54 KB
/
1289.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
const cookieParser = require('cookie-parser')
const Nightmare = require('nightmare')
const express = require('express')
const http = require('http')
main().catch(console.error)
function html(body) {
return `<!doctype><html><head><meta charset="UTF-8"> </head><body>${body}</body></html>`
}
async function main() {
const app = express()
const server = await Server(app)
app.use(cookieParser())
app.get('/', (req, res) => {
res.cookie('cookie', 'monster')
res.send(html(`<a href="/dash" target="_blank">Dashboard</a>`))
})
app.get('/alt', (req, res) => {
res.cookie('cookie', 'monster')
res.send(html(`<a href="/dash">Dashboard</a>`))
})
app.get('/dash', (req, res) => {
const cookie = req.cookies['cookie']
res.send(html(`Got cookie: ${JSON.stringify(cookie)}`))
})
const nightmare = Nightmare({ show: true })
await nightmare.goto(server.url + '/')
console.log(await nightmare.cookies.get())
await sleep(1000)
await nightmare.click('a')
console.log(await nightmare.cookies.get())
await sleep(1000)
await nightmare.end()
await server.close()
}
async function Server(handler) {
const server = await new Promise((res, _rej) => {
var s = http.createServer(handler)
s.listen(0, 'localhost', function() {
res(s)
})
})
const address = server.address()
return {
url: `http://${address.address}:${address.port}`,
close: function() {
return new Promise((res, _rej) => server.close(res))
}
}
}
function sleep(ms) {
return new Promise(res => setTimeout(res, ms))
}