UNPKG

4.73 kBJavaScriptView Raw
1var concat = require('simple-concat')
2var get = require('../')
3var http = require('http')
4var querystring = require('querystring')
5var str = require('string-to-stream')
6var test = require('tape')
7
8test('post (text body)', function (t) {
9 t.plan(5)
10
11 var server = http.createServer(function (req, res) {
12 t.equal(req.method, 'POST')
13 res.statusCode = 200
14 req.pipe(res)
15 })
16
17 server.listen(0, function () {
18 var port = server.address().port
19 var opts = {
20 url: 'http://localhost:' + port,
21 body: 'this is the body'
22 }
23 get.post(opts, function (err, res) {
24 t.error(err)
25 t.equal(res.statusCode, 200)
26 concat(res, function (err, data) {
27 t.error(err)
28 t.equal(data.toString(), 'this is the body')
29 server.close()
30 })
31 })
32 })
33})
34
35test('post (utf-8 text body)', function (t) {
36 t.plan(5)
37
38 var server = http.createServer(function (req, res) {
39 t.equal(req.method, 'POST')
40 res.statusCode = 200
41 req.pipe(res)
42 })
43
44 server.listen(0, function () {
45 var port = server.address().port
46 var opts = {
47 url: 'http://localhost:' + port,
48 body: 'jedan dva tri četiri'
49 }
50 get.post(opts, function (err, res) {
51 t.error(err)
52 t.equal(res.statusCode, 200)
53 concat(res, function (err, data) {
54 t.error(err)
55 t.equal(data.toString(), 'jedan dva tri četiri')
56 server.close()
57 })
58 })
59 })
60})
61
62test('post (buffer body)', function (t) {
63 t.plan(5)
64
65 var server = http.createServer(function (req, res) {
66 t.equal(req.method, 'POST')
67 res.statusCode = 200
68 req.pipe(res)
69 })
70
71 server.listen(0, function () {
72 var port = server.address().port
73 var opts = {
74 url: 'http://localhost:' + port,
75 body: Buffer.from('this is the body')
76 }
77 get.post(opts, function (err, res) {
78 t.error(err)
79 t.equal(res.statusCode, 200)
80 concat(res, function (err, data) {
81 t.error(err)
82 t.equal(data.toString(), 'this is the body')
83 server.close()
84 })
85 })
86 })
87})
88
89test('post (stream body)', function (t) {
90 t.plan(6)
91
92 var server = http.createServer(function (req, res) {
93 t.equal(req.method, 'POST')
94 res.statusCode = 200
95 t.notOk(req.headers['content-length'])
96 req.pipe(res)
97 })
98
99 server.listen(0, function () {
100 var port = server.address().port
101 var opts = {
102 url: 'http://localhost:' + port,
103 body: str('this is the body')
104 }
105 get.post(opts, function (err, res) {
106 t.error(err)
107 t.equal(res.statusCode, 200)
108 concat(res, function (err, data) {
109 t.error(err)
110 t.equal(data.toString(), 'this is the body')
111 server.close()
112 })
113 })
114 })
115})
116
117test('post (json body)', function (t) {
118 t.plan(5)
119
120 var server = http.createServer(function (req, res) {
121 t.equal(req.method, 'POST')
122 t.equal(req.headers['content-type'], 'application/json')
123 res.statusCode = 200
124 req.pipe(res)
125 })
126
127 server.listen(0, function () {
128 var port = server.address().port
129 var opts = {
130 method: 'POST',
131 url: 'http://localhost:' + port,
132 body: {
133 message: 'this is the body'
134 },
135 json: true
136 }
137 get.concat(opts, function (err, res, data) {
138 t.error(err)
139 t.equal(res.statusCode, 200)
140 t.equal(data.message, 'this is the body')
141 server.close()
142 })
143 })
144})
145
146test('post (form, object)', function (t) {
147 t.plan(5)
148
149 var formData = {
150 foo: 'bar'
151 }
152
153 var server = http.createServer(function (req, res) {
154 t.equal(req.method, 'POST')
155 t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded')
156 res.statusCode = 200
157 req.pipe(res)
158 })
159
160 server.listen(0, function () {
161 var port = server.address().port
162 var opts = {
163 method: 'POST',
164 url: 'http://localhost:' + port,
165 form: formData
166 }
167 get.concat(opts, function (err, res, data) {
168 t.error(err)
169 t.equal(res.statusCode, 200)
170 t.deepEqual(querystring.parse(data.toString()), formData)
171 server.close()
172 })
173 })
174})
175
176test('post (form, querystring)', function (t) {
177 t.plan(5)
178
179 var formData = 'foo=bar'
180
181 var server = http.createServer(function (req, res) {
182 t.equal(req.method, 'POST')
183 t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded')
184 res.statusCode = 200
185 req.pipe(res)
186 })
187
188 server.listen(0, function () {
189 var port = server.address().port
190 var opts = {
191 method: 'POST',
192 url: 'http://localhost:' + port,
193 form: formData
194 }
195 get.concat(opts, function (err, res, data) {
196 t.error(err)
197 t.equal(res.statusCode, 200)
198 t.equal(data.toString(), formData)
199 server.close()
200 })
201 })
202})