-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.js
149 lines (138 loc) · 4.32 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var test = require('tape')
var nets = require('./')
var bin
var binUrl = 'http://requestb.in'
var headers = {
'x-requested-with': 'nets',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
}
test('GET a new requestbin for this test run', function (t) {
nets({url: binUrl + '/api/v1/bins', method: 'POST', json: true, headers: headers}, function (err, resp, newBin) {
t.notOk(err, 'no err')
t.ok(newBin.name, 'bin has a name')
bin = newBin
t.end()
})
})
test('GET url property', function (t) {
nets({url: binUrl + '/' + bin.name, headers: headers}, function (err, resp, body) {
t.ok(Buffer.isBuffer(body), 'response data is a Buffer by default')
t.notOk(err, 'no err')
t.equal(resp.statusCode, 200, '200 OK')
getRequests(function (err, resp, reqs) {
t.notOk(err, 'no err')
t.equal(reqs.length, 1)
var req = reqs[0]
t.equal(req.path, '/' + bin.name, 'path matches')
t.end()
})
})
})
test('GET uri property', function (t) {
nets({uri: binUrl + '/' + bin.name, headers: headers}, function (err, resp, body) {
t.notOk(err, 'no err')
t.equal(resp.statusCode, 200, '200 OK')
getRequests(function (err, resp, reqs) {
t.notOk(err, 'no err')
t.equal(reqs.length, 2)
var req = reqs[0]
t.equal(req.path, '/' + bin.name, 'path matches')
t.end()
})
})
})
test('GET w/ custom header', function (t) {
headers['X-Hello'] = 'hello!'
nets({url: binUrl + '/' + bin.name, headers: headers}, function (err, resp, body) {
delete headers['X-Hello']
t.notOk(err, 'no err')
t.equal(resp.statusCode, 200, '200 OK')
getRequests(function (err, resp, reqs) {
t.notOk(err, 'no err')
t.equal(reqs.length, 3)
var req = reqs[0]
t.equal(req.path, '/' + bin.name, 'path matches')
t.equal(req.headers['X-Hello'], 'hello!', 'header matches')
t.end()
})
})
})
test('POST', function (t) {
nets({url: binUrl + '/' + bin.name, headers: headers, method: 'POST', body: 'hello'}, function (err, resp, body) {
t.notOk(err, 'no err')
t.equal(resp.statusCode, 200, '200 OK')
getRequests(function (err, resp, reqs) {
t.notOk(err, 'no err')
t.equal(reqs.length, 4)
var req = reqs[0]
t.equal(req.path, '/' + bin.name, 'path matches')
t.equal(req.body, 'hello', 'body matches')
t.equal(req.method, 'POST', 'POST')
t.end()
})
})
})
test('PUT', function (t) {
nets({url: binUrl + '/' + bin.name, headers: headers, method: 'PUT', body: 'hello put'}, function (err, resp, body) {
t.notOk(err, 'no err')
t.equal(resp.statusCode, 200, '200 OK')
getRequests(function (err, resp, reqs) {
t.notOk(err, 'no err')
t.equal(reqs.length, 5)
var req = reqs[0]
t.equal(req.path, '/' + bin.name, 'path matches')
t.equal(req.body, 'hello put', 'body matches')
t.equal(req.method, 'PUT', 'PUT')
t.end()
})
})
})
test('DELETE', function (t) {
nets({url: binUrl + '/' + bin.name, headers: headers, method: 'DELETE'}, function (err, resp, body) {
t.notOk(err, 'no err')
t.equal(resp.statusCode, 200, '200 OK')
getRequests(function (err, resp, reqs) {
t.notOk(err, 'no err')
t.equal(reqs.length, 6)
var req = reqs[0]
t.equal(req.path, '/' + bin.name, 'path matches')
t.equal(req.method, 'DELETE')
t.end()
})
})
})
test('returns value', function (t) {
t.ok(nets({url: 'http://localhost'}, function () {}), 'nets has return value')
t.end()
})
function getRequests (cb) {
nets({
url: binUrl + '/api/v1/bins/' + bin.name + '/requests',
json: true,
headers: headers
}, function (err, resp, body) {
cb(err, resp, body)
})
}
// {
// "method": 'GET',
// "path": "/zsdmvmzs",
// "form_data": {},
// "body": "",
// "time": 1400675602.691776,
// "content_length": 0,
// "remote_addr": "192.168.101.13, 172.16.231.24, 138.62.0.21",
// "headers": {
// "Via": "1.1 73-46:3128 (squid/2.7.STABLE6), 1.0 cache_server:3128 (squid/2.6.STABLE21)",
// "X-Request-Id": "23c1e383-23a8-497d-b9e9-c7c17935e162",
// "X-Bluecoat-Via": "a640f636fca622c2",
// "Host": "requestb.in",
// "Cache-Control": "max-age=259200",
// "Connection": "close"
// },
// "id": "1bqzf2",
// "content_type": "",
// "query_string": {}
// }