summaryrefslogtreecommitdiffstats
path: root/test/client-post.js
blob: 363b43ca34dd2f7ab6b430ba2f0efd898f58fc2e (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
'use strict'

const { test } = require('tap')
const { Client } = require('..')
const { createServer } = require('http')
const { Blob } = require('buffer')

test('request post blob', { skip: !Blob }, (t) => {
  t.plan(4)

  const server = createServer(async (req, res) => {
    t.equal(req.headers['content-type'], 'application/json')
    let str = ''
    for await (const chunk of req) {
      str += chunk
    }
    t.equal(str, 'asd')
    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))

    client.request({
      path: '/',
      method: 'GET',
      body: new Blob(['asd'], {
        type: 'application/json'
      })
    }, (err, data) => {
      t.error(err)
      data.body.resume().on('end', () => {
        t.pass()
      })
    })
  })
})

test('request post arrayBuffer', { skip: !Blob }, (t) => {
  t.plan(3)

  const server = createServer(async (req, res) => {
    let str = ''
    for await (const chunk of req) {
      str += chunk
    }
    t.equal(str, 'asd')
    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 buf = Buffer.from('asd')
    const dst = new ArrayBuffer(buf.byteLength)
    buf.copy(new Uint8Array(dst))

    client.request({
      path: '/',
      method: 'GET',
      body: dst
    }, (err, data) => {
      t.error(err)
      data.body.resume().on('end', () => {
        t.pass()
      })
    })
  })
})