File: test-promise.js

package info (click to toggle)
node-request 2.88.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 960 kB
  • sloc: javascript: 9,687; sh: 46; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,123 bytes parent folder | download | duplicates (3)
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
'use strict'

var http = require('http')
var request = require('../index')
var tape = require('tape')
var Promise = require('bluebird')

var s = http.createServer(function (req, res) {
  res.writeHead(200, {})
  res.end('ok')
})

tape('setup', function (t) {
  s.listen(0, function () {
    s.url = 'http://localhost:' + this.address().port
    t.end()
  })
})

tape('promisify convenience method', function (t) {
  var get = request.get
  var p = Promise.promisify(get, {multiArgs: true})
  p(s.url)
    .then(function (results) {
      var res = results[0]
      t.equal(res.statusCode, 200)
      t.end()
    })
})

tape('promisify request function', function (t) {
  var p = Promise.promisify(request, {multiArgs: true})
  p(s.url)
    .spread(function (res, body) {
      t.equal(res.statusCode, 200)
      t.end()
    })
})

tape('promisify all methods', function (t) {
  Promise.promisifyAll(request, {multiArgs: true})
  request.getAsync(s.url)
    .spread(function (res, body) {
      t.equal(res.statusCode, 200)
      t.end()
    })
})

tape('cleanup', function (t) {
  s.close(function () {
    t.end()
  })
})