UNPKG

2.18 kBJavaScriptView Raw
1var get = require('../')
2var http = require('http')
3var str = require('string-to-stream')
4var test = require('tape')
5
6test('get.concat (post, stream body, and json option)', function (t) {
7 t.plan(4)
8
9 var server = http.createServer(function (req, res) {
10 res.statusCode = 200
11 req.pipe(res)
12 })
13
14 server.listen(0, function () {
15 var port = server.address().port
16 var opts = {
17 url: 'http://localhost:' + port,
18 body: str('{"a": "b"}'),
19 method: 'POST',
20 json: true
21 }
22 get.concat(opts, function (err, res, data) {
23 t.error(err)
24 t.equal(typeof data, 'object')
25 t.deepEqual(Object.keys(data), ['a'])
26 t.equal(data.a, 'b')
27 server.close()
28 })
29 })
30})
31
32test('get.concat', function (t) {
33 t.plan(4)
34 var server = http.createServer(function (req, res) {
35 res.statusCode = 200
36 res.end('blah blah blah')
37 })
38
39 server.listen(0, function () {
40 var port = server.address().port
41 get.concat('http://localhost:' + port, function (err, res, data) {
42 t.error(err)
43 t.equal(res.statusCode, 200)
44 t.ok(Buffer.isBuffer(data), '`data` is type buffer')
45 t.equal(data.toString(), 'blah blah blah')
46 server.close()
47 })
48 })
49})
50
51test('get.concat json', function (t) {
52 t.plan(3)
53 var server = http.createServer(function (req, res) {
54 res.statusCode = 200
55 res.end('{"message":"response"}')
56 })
57
58 server.listen(0, function () {
59 var port = server.address().port
60 var opts = {
61 url: 'http://localhost:' + port + '/path',
62 json: true
63 }
64 get.concat(opts, function (err, res, data) {
65 t.error(err)
66 t.equal(res.statusCode, 200)
67 t.equal(data.message, 'response')
68 server.close()
69 })
70 })
71})
72
73test('get.concat json error', function (t) {
74 t.plan(1)
75 var server = http.createServer(function (req, res) {
76 res.statusCode = 500
77 res.end('not json')
78 })
79
80 server.listen(0, function () {
81 var port = server.address().port
82 var opts = {
83 url: 'http://localhost:' + port + '/path',
84 json: true
85 }
86 get.concat(opts, function (err, res, data) {
87 t.ok(err instanceof Error)
88 server.close()
89 })
90 })
91})