-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
github-api-provider.integration.js
123 lines (108 loc) · 3.44 KB
/
github-api-provider.integration.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
import { expect } from 'chai'
import config from 'config'
import { fetch } from '../../core/base-service/got.js'
import GithubApiProvider from './github-api-provider.js'
describe('Github API provider', function () {
const baseUrl = process.env.GITHUB_URL || 'https://api.github.com'
const reserveFraction = 0.333
let token
before(function () {
token = config.util.toObject().private.gh_token
if (!token) {
throw Error('The integration tests require a gh_token to be set')
}
})
let githubApiProvider
context('with no auth', function () {
before(function () {
githubApiProvider = new GithubApiProvider({
baseUrl,
authType: GithubApiProvider.AUTH_TYPES.NO_AUTH,
})
})
it('should be able to run 10 requests', async function () {
this.timeout('20s')
for (let i = 0; i < 10; ++i) {
const { res } = await githubApiProvider.fetch(
fetch,
'/repos/rust-lang/rust',
{},
)
expect(res.statusCode).to.equal(200)
}
})
})
context('with global token', function () {
before(function () {
githubApiProvider = new GithubApiProvider({
baseUrl,
authType: GithubApiProvider.AUTH_TYPES.GLOBAL_TOKEN,
globalToken: token,
reserveFraction,
})
})
it('should be able to run 10 requests', async function () {
this.timeout('20s')
for (let i = 0; i < 10; ++i) {
const { res } = await githubApiProvider.fetch(
fetch,
'/repos/rust-lang/rust',
{},
)
expect(res.statusCode).to.equal(200)
}
})
})
context('with token pool', function () {
let githubApiProvider
before(function () {
githubApiProvider = new GithubApiProvider({
baseUrl,
authType: GithubApiProvider.AUTH_TYPES.TOKEN_POOL,
reserveFraction,
})
githubApiProvider.addToken(token)
})
const headers = []
async function performOneRequest() {
const { res } = await githubApiProvider.fetch(
fetch,
'/repos/rust-lang/rust',
{},
)
expect(res.statusCode).to.equal(200)
headers.push(res.headers)
}
before('should be able to run 10 requests', async function () {
this.timeout('20s')
for (let i = 0; i < 10; ++i) {
await performOneRequest()
}
})
it('should decrement the limit remaining with each request', function () {
for (let i = 1; i < headers.length; ++i) {
const current = headers[i]
const previous = headers[i - 1]
expect(+current['x-ratelimit-remaining']).to.be.lessThan(
+previous['x-ratelimit-remaining'],
)
}
})
// Is this test failing? See https://github.com/badges/shields/pull/4590#issuecomment-708551801
it('should update the token with the final limit remaining and reset time', function () {
const lastHeaders = headers.slice(-1)[0]
const reserve = reserveFraction * +lastHeaders['x-ratelimit-limit']
const usesRemaining = +lastHeaders['x-ratelimit-remaining'] - reserve
const nextReset = +lastHeaders['x-ratelimit-reset']
const tokens = []
githubApiProvider.standardTokens.forEach(t => {
tokens.push(t)
})
// Confidence check.
expect(tokens).to.have.lengthOf(1)
const [token] = tokens
expect(token.usesRemaining).to.equal(usesRemaining)
expect(token.nextReset).to.equal(nextReset)
})
})
})