summaryrefslogtreecommitdiffstats
path: root/fastify-busboy/test/dicer-endfinish.test.js
blob: 4718076b23db846403acaacf582850848e0e950d (plain)
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
'use strict'

const Dicer = require('../deps/dicer/lib/Dicer')
const { test } = require('tap')

test('dicer-endfinish', t => {
  t.plan(1)

  t.test('should properly handle finish', t => {
    t.plan(4)

    const CRLF = '\r\n'
    const boundary = 'boundary'

    const writeSep = '--' + boundary

    const writePart = [
      writeSep,
      'Content-Type:   text/plain',
      'Content-Length: 0'
    ].join(CRLF) +
      CRLF + CRLF +
      'some data' + CRLF

    const writeEnd = '--' + CRLF

    let firedEnd = false
    let firedFinish = false

    const dicer = new Dicer({ boundary })
    dicer.on('part', partListener)
    dicer.on('finish', finishListener)
    dicer.write(writePart + writeSep)

    function partListener (partReadStream) {
      partReadStream.on('data', function () { })
      partReadStream.on('end', partEndListener)
    }
    function partEndListener () {
      firedEnd = true
      setImmediate(afterEnd)
    }
    function afterEnd () {
      dicer.end(writeEnd)
      setImmediate(afterWrite)
    }
    function finishListener () {
      t.ok(firedEnd, 'end before finishing')
      firedFinish = true
      test2()
    }
    function afterWrite () {
      t.ok(firedFinish, 'Failed to finish')
    }

    let isPausePush = true

    let firedPauseCallback = false
    let firedPauseFinish = false

    let dicer2 = null

    function test2 () {
      dicer2 = new Dicer({ boundary })
      dicer2.on('part', pausePartListener)
      dicer2.on('finish', pauseFinish)
      dicer2.write(writePart + writeSep, 'utf8', pausePartCallback)
      setImmediate(pauseAfterWrite)
    }
    function pausePartListener (partReadStream) {
      partReadStream.on('data', function () { })
      partReadStream.on('end', function () { })
      const realPush = partReadStream.push
      partReadStream.push = function fakePush () {
        realPush.apply(partReadStream, arguments)
        if (!isPausePush) { return true }
        isPausePush = false
        return false
      }
    }
    function pauseAfterWrite () {
      dicer2.end(writeEnd)
      setImmediate(pauseAfterEnd)
    }
    function pauseAfterEnd () {
      t.ok(firedPauseCallback, 'Called callback after pause')
      t.ok(firedPauseFinish, 'Finish after pause')
    }
    function pauseFinish () {
      firedPauseFinish = true
    }
    function pausePartCallback () {
      firedPauseCallback = true
    }
  })
})