1 | 'use strict'
|
2 |
|
3 | const test = require('tap').test
|
4 | const http = require('http')
|
5 | const CollectAndRead = require('./collect-and-read.js')
|
6 | const cmds = []
|
7 |
|
8 | test('cmd - collect - detect server port', function (t) {
|
9 | const cmd = new CollectAndRead({ detectPort: true }, '-e', `
|
10 | const http = require('http')
|
11 | http.createServer(onrequest).listen(0)
|
12 |
|
13 | function onrequest (req, res) {
|
14 | this.close()
|
15 | res.end('from server')
|
16 | }
|
17 | `)
|
18 | cmds.push(cmd)
|
19 |
|
20 | cmd.tool.on('port', function (port) {
|
21 | t.ok(typeof port === 'number')
|
22 | t.ok(port > 0)
|
23 |
|
24 | http.get(`http://127.0.0.1:${port}`, function (res) {
|
25 | const buf = []
|
26 | res.on('data', data => buf.push(data))
|
27 | res.on('end', function () {
|
28 | t.deepEquals(Buffer.concat(buf), Buffer.from('from server'))
|
29 | t.end()
|
30 | })
|
31 | })
|
32 | })
|
33 | })
|
34 |
|
35 | test('cmd - collect - detect server port and cb', function (t) {
|
36 | const cmd = new CollectAndRead({ detectPort: true }, '-e', `
|
37 | const http = require('http')
|
38 | http.createServer(onrequest).listen(0)
|
39 |
|
40 | function onrequest (req, res) {
|
41 | res.end('from server')
|
42 | }
|
43 | `)
|
44 | cmds.push(cmd)
|
45 |
|
46 | cmd.tool.on('port', function (port, proc, cb) {
|
47 | t.ok(typeof port === 'number')
|
48 | t.ok(port > 0)
|
49 |
|
50 | http.get(`http://127.0.0.1:${port}`, function (res) {
|
51 | const buf = []
|
52 | res.on('data', data => buf.push(data))
|
53 | res.on('end', function () {
|
54 | t.deepEquals(Buffer.concat(buf), Buffer.from('from server'))
|
55 | t.end()
|
56 | cb()
|
57 | })
|
58 | })
|
59 | })
|
60 | })
|
61 |
|
62 | process.on('beforeExit', function () {
|
63 | for (const cmd of cmds) {
|
64 | cmd.cleanup()
|
65 | }
|
66 | })
|