summaryrefslogtreecommitdiffstats
path: root/test/stream-compat.js
blob: 71d2410ee3650f8822f6c63c49712ee50a12964a (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
'use strict'

const { test } = require('tap')
const { Client } = require('..')
const { createServer } = require('http')
const { Readable } = require('stream')
const EE = require('events')

test('stream body without destroy', (t) => {
  t.plan(2)

  const server = createServer((req, res) => {
    res.end()
  })
  t.teardown(server.close.bind(server))
  server.listen(0, () => {
    const client = new Client(`http://localhost:${server.address().port}`)
    t.teardown(client.destroy.bind(client))

    const signal = new EE()
    const body = new Readable({ read () {} })
    body.destroy = undefined
    body.on('error', (err) => {
      t.ok(err)
    })
    client.request({
      path: '/',
      method: 'PUT',
      signal,
      body
    }, (err, data) => {
      t.ok(err)
    })
    signal.emit('abort')
  })
})

test('IncomingMessage', (t) => {
  t.plan(2)

  const server = createServer((req, res) => {
    res.end()
  })
  t.teardown(server.close.bind(server))

  server.listen(0, () => {
    const proxyClient = new Client(`http://localhost:${server.address().port}`)
    t.teardown(proxyClient.destroy.bind(proxyClient))

    const proxy = createServer((req, res) => {
      proxyClient.request({
        path: '/',
        method: 'PUT',
        body: req
      }, (err, data) => {
        t.error(err)
        data.body.pipe(res)
      })
    })
    t.teardown(proxy.close.bind(proxy))

    proxy.listen(0, () => {
      const client = new Client(`http://localhost:${proxy.address().port}`)
      t.teardown(client.destroy.bind(client))

      client.request({
        path: '/',
        method: 'PUT',
        body: 'hello world'
      }, (err, data) => {
        t.error(err)
      })
    })
  })
})