-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.js
More file actions
153 lines (114 loc) · 3.79 KB
/
tests.js
File metadata and controls
153 lines (114 loc) · 3.79 KB
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
150
151
152
153
var test = require('tape');
var nock = require('nock');
var Yammer = require('./lib/yammer');
test('access_token is added as authorization header', function(t) {
var yam = new Yammer({ access_token: 'test_token' });
nock('https://www.yammer.dev').get('/test.json')
.reply(200, {});
yam.request({
uri: 'https://www.yammer.dev/test'
}, function (err, body, res) {
t.equal(res.request.headers.authorization, 'Bearer test_token',
'oauth token present in headers');
t.end();
});
});
test('json format is added by default', function(t) {
var yam = new Yammer();
nock('https://www.yammer.dev').get('/test.json')
.reply(200, {});
yam.request({
uri: 'https://www.yammer.dev/test'
}, function (err, body, res) {
t.equal(res.request.uri.path, '/test.json', 'url has json format');
t.end();
});
});
test('json response is parsed automatically', function(t) {
var yam = new Yammer();
nock('https://www.yammer.dev').get('/test.json')
.reply(200, { test: "json" });
yam.request({
uri: 'https://www.yammer.dev/test'
}, function (err, body, res) {
t.deepEqual(body, { test: 'json' }, 'json response is parsed');
t.end();
});
});
test('400 response returns error', function(t) {
var yam = new Yammer();
nock('https://www.yammer.dev/').get('/test.json')
.reply(404, '');
yam.request({
uri: 'https://www.yammer.dev/test'
}, function (err, body, res) {
t.ok(err instanceof Error, 'error on 400 response');
t.end();
});
});
test('500 response returns error', function(t) {
var yam = new Yammer();
nock('https://www.yammer.dev').get('/test.json')
.reply(500, '')
yam.request({
uri: 'htps://www.yammer.dev/test'
}, function (err, body, res) {
t.ok(err instanceof Error, 'error on 500 response');
t.end();
});
});
test('boolean callbacks return false for 404', function(t) {
var yam = new Yammer({
hostname: 'https://www.yammer.dev'
});
t.plan(4);
nock('https://www.yammer.dev').get('/api/v1/subscriptions/to_user/1.json')
.reply(404, '');
yam.checkUserSubscription(1, function (err, body) {
t.equal(body, false, 'checkUserSubscription boolean');
});
nock('https://www.yammer.dev').get('/api/v1/subscriptions/to_tag/1.json')
.reply(404, '');
yam.checkTagSubscription(1, function (err, body) {
t.equal(body, false, 'checkTagSubscription boolean');
});
nock('https://www.yammer.dev').get('/api/v1/subscriptions/to_thread/1.json')
.reply(404, '');
yam.checkThreadSubscription(1, function (err, body) {
t.equal(body, false, 'checkThreadSubscription boolean');
});
nock('https://www.yammer.dev').get('/api/v1/subscriptions/to_topic/1.json')
.reply(404, '');
yam.checkTopicSubscription(1, function (err, body) {
t.equal(body, false, 'checkTopicSubscription boolean');
});
});
test('qs property is passed through as query parameters', function(t) {
var yam = new Yammer();
nock('https://www.yammer.dev').get('/test.json?test_prop=test_value')
.reply(200, '');
yam.request({
uri: 'https://www.yammer.dev/test',
qs: {
test_prop: 'test_value'
}
}, function (err, body, res) {
t.equal(res.request.uri.query, 'test_prop=test_value',
'yam.request sends query parameters');
t.end();
});
});
test('form encoded data is sent', function(t) {
var yam = new Yammer({ hostname: 'https://www.yammer.dev' });
nock('https://www.yammer.dev').post('/api/v1/messages.json')
.reply(200, '');
yam.createMessage({ test_prop: 'test_value' }, function (err, body, res) {
var req = res.request;
t.equal(req.headers['content-type'],
'application/x-www-form-urlencoded; charset=utf-8',
'content-type set correctly');
t.equal(String(req.body), 'test_prop=test_value',
'request body sent');
t.end();
});
});