-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest.js
More file actions
151 lines (125 loc) · 4.04 KB
/
Copy pathtest.js
File metadata and controls
151 lines (125 loc) · 4.04 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
import process from 'node:process';
import test from 'node:test';
import assert from 'node:assert/strict';
import nock from 'nock';
import getStream from 'get-stream';
import ghGot from './index.js';
const token = process.env.GITHUB_TOKEN;
test('default', async () => {
const {body} = await ghGot('users/sindresorhus');
assert.strictEqual(body.login, 'sindresorhus');
});
test('full path', async () => {
const {body} = await ghGot('https://api.github.com/users/sindresorhus', {prefixUrl: ''});
assert.strictEqual(body.login, 'sindresorhus');
});
test('accepts options', async () => {
const {body} = await ghGot('users/sindresorhus', {});
assert.strictEqual(body.login, 'sindresorhus');
});
test('accepts options.prefixUrl without trailing slash', async () => {
const {body} = await ghGot('users/sindresorhus', {prefixUrl: 'https://api.github.com'});
assert.strictEqual(body.login, 'sindresorhus');
});
test('dedupes slashes', async () => {
const {body} = await ghGot('/users/sindresorhus', {prefixUrl: 'https://api.github.com/'});
assert.strictEqual(body.login, 'sindresorhus');
});
test('global token option', async () => {
process.env.GITHUB_TOKEN = 'fail';
await assert.rejects(
ghGot.recreate()('users/sindresorhus'),
{
message: 'Bad credentials (401)',
},
);
process.env.GITHUB_TOKEN = token;
});
test('token option (context)', async () => {
await assert.rejects(
ghGot('users/sindresorhus', {context: {token: 'fail'}}),
{
message: 'Bad credentials (401)',
},
);
});
test('token option (direct)', async () => {
await assert.rejects(
ghGot('users/sindresorhus', {token: 'fail'}),
{
message: 'Bad credentials (401)',
},
);
});
test('global endpoint option', async () => {
process.env.GITHUB_ENDPOINT = 'fail';
await assert.rejects(
ghGot.recreate()('users/sindresorhus', {retries: 1}),
error => {
assert.match(error.message, /Invalid URL/v);
return true;
},
);
delete process.env.GITHUB_ENDPOINT;
});
test('endpoint option', async () => {
process.env.GITHUB_ENDPOINT = 'https://api.github.com/';
await assert.rejects(
ghGot.recreate()('users/sindresorhus', {
prefixUrl: 'fail',
retries: 1,
}),
error => {
assert.match(error.message, /Invalid URL/v);
return true;
},
);
delete process.env.GITHUB_ENDPOINT;
});
test('stream interface', async () => {
const string1 = await getStream(ghGot.stream('users/sindresorhus'));
assert.strictEqual(JSON.parse(string1).login, 'sindresorhus');
const string2 = await getStream(ghGot.stream.get('users/sindresorhus'));
assert.strictEqual(JSON.parse(string2).login, 'sindresorhus');
});
test('json body', async () => {
const prefixUrl = 'http://mock-endpoint';
const postBody = {test: [1, 3, 3, 7]};
const reply = {ok: true};
const scope = nock(prefixUrl).post('/test', postBody).reply(200, reply);
const {body} = await ghGot.post('test', {prefixUrl, json: postBody});
assert.deepStrictEqual(body, reply);
assert.ok(scope.isDone());
});
test('custom error', async () => {
await assert.rejects(
ghGot('users/sindresorhus', {context: {token: 'fail'}}),
error => {
assert.strictEqual(error.name, 'GitHubError');
assert.strictEqual(error.message, 'Bad credentials (401)');
return true;
},
);
});
test('.rateLimit response property', async () => {
const {rateLimit} = await ghGot('users/sindresorhus');
assert.strictEqual(typeof rateLimit.limit, 'number');
assert.strictEqual(typeof rateLimit.remaining, 'number');
assert.ok(rateLimit.reset instanceof Date);
});
test('.rateLimit error property', async () => {
try {
await ghGot('users/sindresorhus', {context: {token: 'fail'}});
assert.fail('Should have thrown');
} catch (error) {
// Rate limit headers may not be present on authentication errors
// If they are present, they should be properly formatted
if (error.rateLimit) {
assert.strictEqual(typeof error.rateLimit.limit, 'number');
assert.strictEqual(typeof error.rateLimit.remaining, 'number');
assert.ok(error.rateLimit.reset instanceof Date);
}
// Ensure the error was thrown (test would fail if not)
assert.ok(error);
}
});