summaryrefslogtreecommitdiffstats
path: root/test/validations.js
blob: d1b34095cfe4ebf115d6854cd049ab8b2c2515ef (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
'use strict'

const t = require('tap')
const { test } = t
const { createServer } = require('http')
const { Client, errors } = require('..')

const server = createServer((req, res) => {
  res.setHeader('content-type', 'text/plain')
  res.end('hello')
  t.fail('server should never be called')
})
t.teardown(server.close.bind(server))

server.listen(0, () => {
  const url = `http://localhost:${server.address().port}`

  test('path', (t) => {
    t.plan(4)

    const client = new Client(url)
    t.teardown(client.close.bind(client))

    client.request({ path: null, method: 'GET' }, (err, res) => {
      t.type(err, errors.InvalidArgumentError)
      t.equal(err.message, 'path must be a string')
    })

    client.request({ path: 'aaa', method: 'GET' }, (err, res) => {
      t.type(err, errors.InvalidArgumentError)
      t.equal(err.message, 'path must be an absolute URL or start with a slash')
    })
  })

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

    const client = new Client(url)
    t.teardown(client.close.bind(client))

    client.request({ path: '/', method: null }, (err, res) => {
      t.type(err, errors.InvalidArgumentError)
      t.equal(err.message, 'method must be a string')
    })
  })

  test('body', (t) => {
    t.plan(4)

    const client = new Client(url)
    t.teardown(client.close.bind(client))

    client.request({ path: '/', method: 'POST', body: 42 }, (err, res) => {
      t.type(err, errors.InvalidArgumentError)
      t.equal(err.message, 'body must be a string, a Buffer, a Readable stream, an iterable, or an async iterable')
    })

    client.request({ path: '/', method: 'POST', body: { hello: 'world' } }, (err, res) => {
      t.type(err, errors.InvalidArgumentError)
      t.equal(err.message, 'body must be a string, a Buffer, a Readable stream, an iterable, or an async iterable')
    })
  })
})