-
Notifications
You must be signed in to change notification settings - Fork 18
/
test-get.js
146 lines (131 loc) · 3.03 KB
/
test-get.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
var test = require('tape')
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var tiny = require('./dist.js')
var server
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}))
app.get('/json', (req, res)=> {
res.json({hello: 'there'})
})
app.get('/void', (req, res)=> {
res.json('')
})
app.options('/opts', (req, res) => {
res.setHeader('allow', 'OPTIONS, GET')
res.json('')
})
app.get('/goaway', (req, res) => {
res.setHeader('location', '/')
res.statusCode = 302
res.send()
})
test('startup', t=> {
t.plan(1)
server = app.listen(3001, x=> {
t.ok(true, 'started server')
})
})
test('can get a url', t=> {
t.plan(3)
var url = 'https://brian.io'
tiny.get({url}, function __got(err, result) {
if (err) {
t.fail(err.statusCode, 'failed to get')
}
else {
t.ok(result, 'got a result')
t.ok(result.headers, 'got headers')
t.ok(result.body, 'got body')
console.log(result)
}
})
})
test('can get json', t=> {
t.plan(2)
var url = 'http://localhost:3001/json'
tiny.get({url}, function __json(err, result) {
if (err) {
t.fail(err)
}
else {
t.ok(result, 'got a result')
t.equal(result.body.hello, 'there', 'body is an object')
console.log(err, result)
}
})
})
test('can get and handle "no content"', t=> {
t.plan(2)
var url = 'http://localhost:3001/void'
tiny.get({url}, function __void(err, result) {
if (err) {
t.fail(err)
}
else {
t.ok(result, 'got a result (empty tho)')
t.is(result.body, '')
console.log(result)
}
})
})
test('get fails gracefully', t=> {
t.plan(1)
var url = 'http://nop333.ca'
tiny.get({url}, function __ruhroh(err, result) {
if (err) {
t.ok(err, 'got err as expected')
console.log(err)
}
else {
t.fail(result, 'should not succeed')
}
})
})
test('can head a url', t=> {
t.plan(2)
var url = 'https://www.google.com'
tiny.head({url}, function __got(err, result) {
if (err) {
t.fail(err.statusCode, 'failed to head')
}
else {
t.ok(result, 'got a result')
t.ok(result.headers, 'got headers')
console.log(JSON.stringify(result, null, 2).substring(0,75), '...')
}
})
})
test('can options a url', t=> {
t.plan(2)
var url = 'http://localhost:3001/opts'
tiny.options({url}, function __got(err, result) {
if (err) {
t.fail(err.statusCode, 'failed to options')
}
else {
t.ok(result, 'got a result')
t.ok(result.headers.allow, 'got headers')
console.log(result)
}
})
})
test('can handles redirect', t=> {
t.plan(2)
var url = 'http://localhost:3001/goaway'
tiny.get({url}, function __got(err, result) {
if (err) {
t.fail(err.statusCode, 'failed to handle redirect')
} else {
t.ok(result, 'got a result')
t.ok(result.headers.location, 'got a location header')
console.log(result)
}
})
})
test('shutdown', t=> {
t.plan(1)
server.close()
t.ok(true, 'closed server')
})