-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
45 lines (40 loc) · 948 Bytes
/
example.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
const nock = require('nock')
const expect = require('./')
const theon = expect(require('theon'))
// Set up mock
nock('http://my.api.com')
.get('/api/users/123')
.set('Content-Type', 'application/json')
.reply(200, [{
id: '123',
username: 'foo'
}])
const client = theon('http://my.api.com')
const users = client
.basePath('/api')
.set('Version', '1.0')
.collection('users')
.basePath('/users')
.resource('get')
.path('/:id')
// Render the API client
const api = users.render()
api
.users
.get()
.param('id', 123)
// Attach an observer for the current request at API client level
.expect(200)
.expect('Content-Type', 'application/json')
.expect([{ id: '123', username: 'foo' }])
.expect(res => {
if (res.error) {
throw new Error('Invalid request')
}
})
.end((err, res) => {
if (err) {
return console.error('Expectation failed:', err)
}
console.log('Success!')
})