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

const { Client } = require('../..')
const { createServer } = require('http')
/* global test, expect */

test('should work in jest', async () => {
  const server = createServer((req, res) => {
    expect(req.url).toBe('/')
    expect(req.method).toBe('POST')
    expect(req.headers.host).toBe(`localhost:${server.address().port}`)
    res.setHeader('Content-Type', 'text/plain')
    res.end('hello')
  })
  await expect(new Promise((resolve, reject) => {
    server.listen(0, () => {
      const client = new Client(`http://localhost:${server.address().port}`)
      client.request({
        path: '/',
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: '{}'
      }, (err, result) => {
        server.close()
        client.close()
        if (err) {
          reject(err)
        } else {
          resolve(result.body.text())
        }
      })
    })
  })).resolves.toBe('hello')
})