forked from mafintosh/docker-remote-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
74 lines (63 loc) · 1.63 KB
/
test.js
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
var tape = require('tape')
var concat = require('concat-stream')
var http = require('http')
var docker = require('./')
var server = http.createServer(function (req, res) {
if (req.method === 'POST') return req.pipe(res)
if (req.url === '/error') {
res.statusCode = 500
res.end('error message')
return
}
res.end(JSON.stringify({
method: req.method,
path: req.url
}))
})
server.listen(0, function () {
server.unref()
var request = docker({ssl: false, host: server.address().port})
tape('get json', function (t) {
request.get('/', {json: true, qs: {foo: 'bar'}}, function (err, body) {
t.ok(!err)
t.same(body.method, 'GET')
t.same(body.path, '/?foo=bar')
t.end()
})
})
tape('get stream', function (t) {
request.get('/', function (err, response) {
t.ok(!err)
response.pipe(concat(function (buf) {
t.same(buf.toString(), '{"method":"GET","path":"/"}')
t.end()
}))
})
})
tape('post stream', function (t) {
var post = request.post('/', function (err, response) {
t.ok(!err)
response.pipe(concat(function (buf) {
t.same(buf.toString(), 'hello world')
t.end()
}))
})
post.write('hello')
post.end(' world')
})
tape('post json', function (t) {
request.post('/', {json: {method: 'GET', path: '/'}}, function (err, body) {
t.ok(!err)
t.same(body.method, 'GET')
t.same(body.path, '/')
t.end()
})
})
tape('get error', function (t) {
request.get('/error', function (err) {
t.ok(err)
t.same(err.message, 'error message')
t.end()
})
})
})