File size: 2,875 Bytes
9307755
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict'
/* globals it */
const bent = require('../')
const tsame = require('tsame')
const assert = require('assert')
const zlib = require('zlib')
const ttype = (e, str) => same(e.constructor.name, str)
const qs = require('querystring')

const test = it
const same = (x, y) => assert.ok(tsame(x, y))

test('Invalid encoding', done => {
  try {
    bent('blah')
  } catch (e) {
    ttype(e, 'Error')
    same(e.message, 'Unknown encoding, blah')
    done()
  }
})

test('double method', done => {
  try {
    bent('GET', 'PUT')
  } catch (e) {
    ttype(e, 'Error')
    same(e.message, 'Can\'t set method to PUT, already set to GET.')
    done()
  }
})

test('double headers', done => {
  try {
    bent({}, {})
  } catch (e) {
    ttype(e, 'Error')
    same(e.message, 'Cannot set headers twice.')
    done()
  }
})

test('unknown protocol', async () => {
  try {
    const request = bent()
    await request('ftp://host.com')
    throw new Error('Should have already failed')
  } catch (e) {
    ttype(e, 'Error')
    same(e.message, 'Unknown protocol, ftp:')
  }
})

test('Invalid type', done => {
  try {
    bent(true)
  } catch (e) {
    ttype(e, 'Error')
    same(e.message, 'Unknown type: boolean')
    done()
  }
})

test('Invalid body', async () => {
  const r = bent('PUT')
  try {
    await r('http://localhost:3000', true)
    throw new Error('Should have failed')
  } catch (e) {
    ttype(e, 'Error')
    same(e.message, 'Unknown body type.')
  }
})

test('Invalid json', async () => {
  const r = bent('GET', 'json')
  try {
    await r('https://echo-server.mikeal.now.sh/src/echo.js?body=[asdf]')
    throw new Error('Should have failed')
  } catch (e) {
    assert.ok(e.message.startsWith('Unexpected token a in JSON'))
  }
})

const getError = async () => {
  const r = bent(201)
  try {
    await r('https://echo-server.mikeal.now.sh/src/echo.js?body="asdf"')
    throw new Error('Should have failed')
  } catch (e) {
    ttype(e, 'StatusError')
    return e
  }
}

test('error decodings', async () => {
  let e = await getError()
  same(await e.text(), '"asdf"')
  e = await getError()
  same(await e.json(), 'asdf')
})

if (!process.browser) {
  test('Z_BUF_ERROR error', async () => {
    const request = bent('json')
    try {
      await request('https://echo-server.mikeal.now.sh/src/echo.js?headers=content-encoding%3Agzip%2Ccontent-type%3Aapplication%2Fjson')
    } catch (e) {
      ttype(e, 'Error')
      return e
    }
  })
  test('gzip json compresssion SyntaxError', async () => {
    const request = bent('json')
    const base64 = zlib.gzipSync('ok').toString('base64')
    const headers = 'content-encoding:gzip,content-type:application/json'
    try {
      await request(`https://echo-server.mikeal.now.sh/src/echo.js?${qs.stringify({ base64, headers })}`)
    } catch (e) {
      ttype(e, 'SyntaxError')
      return e
    }
  })
}