Skip to content

Commit 088ae45

Browse files
committed
test: adapt getClient test
1 parent 0fbc003 commit 088ae45

File tree

1 file changed

+108
-82
lines changed

1 file changed

+108
-82
lines changed

test/get-client.test.js

Lines changed: 108 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1-
import {join, dirname} from 'node:path';
2-
import {createServer} from 'node:http';
3-
import {createServer as _createServer} from 'node:https';
4-
import {promisify} from 'node:util';
5-
import {fileURLToPath} from 'node:url';
6-
7-
import {readFile} from 'fs-extra';
8-
import test, {serial} from 'ava';
9-
import {inRange} from 'lodash-es';
10-
import {stub, spy} from 'sinon';
11-
import proxyquire from 'proxyquire';
12-
import Proxy from 'proxy';
13-
import serverDestroy from 'server-destroy';
14-
import {Octokit} from '@octokit/rest';
15-
16-
import rateLimit from './helpers/rate-limit.js';
1+
import { join, dirname } from "node:path";
2+
import { createServer } from "node:http";
3+
import { createServer as _createServer } from "node:https";
4+
import { promisify } from "node:util";
5+
import { fileURLToPath } from "node:url";
6+
import { readFile } from "node:fs/promises";
7+
8+
import { inRange } from "lodash-es";
9+
import { Octokit } from "@octokit/rest";
10+
import Proxy from "proxy";
11+
import quibble from "quibble";
12+
import serverDestroy from "server-destroy";
13+
import sinon from "sinon";
14+
import test from "ava";
15+
16+
import * as RATE_LIMIT_MOCK from "./helpers/rate-limit.js";
1717

1818
const __dirname = dirname(fileURLToPath(import.meta.url));
19-
const getClient = proxyquire('../lib/get-client', {'./definitions/rate-limit': rateLimit});
19+
20+
await quibble.esm("../lib/definitions/rate-limit.js", RATE_LIMIT_MOCK);
21+
const getClient = (await import("../lib/get-client.js")).default;
2022

2123
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
2224

23-
serial('Use a http proxy', async (t) => {
25+
test.serial("Use a http proxy", async (t) => {
2426
const server = createServer();
2527
await promisify(server.listen).bind(server)();
2628
const serverPort = server.address().port;
@@ -30,35 +32,35 @@ serial('Use a http proxy', async (t) => {
3032
const proxyPort = proxy.address().port;
3133
serverDestroy(proxy);
3234

33-
const proxyHandler = spy();
34-
const serverHandler = spy((request, response) => {
35+
const proxyHandler = sinon.spy();
36+
const serverHandler = sinon.spy((request, response) => {
3537
response.end();
3638
});
37-
proxy.on('request', proxyHandler);
38-
server.on('request', serverHandler);
39+
proxy.on("request", proxyHandler);
40+
server.on("request", serverHandler);
3941

4042
const github = getClient({
41-
githubToken: 'github_token',
43+
githubToken: "github_token",
4244
githubUrl: `http://localhost:${serverPort}`,
43-
githubApiPathPrefix: '',
45+
githubApiPathPrefix: "",
4446
proxy: `http://localhost:${proxyPort}`,
4547
});
4648

47-
await github.repos.get({repo: 'repo', owner: 'owner'});
49+
await github.repos.get({ repo: "repo", owner: "owner" });
4850

49-
t.is(proxyHandler.args[0][0].headers.accept, 'application/vnd.github.v3+json');
50-
t.is(serverHandler.args[0][0].headers.accept, 'application/vnd.github.v3+json');
51+
t.is(proxyHandler.args[0][0].headers.accept, "application/vnd.github.v3+json");
52+
t.is(serverHandler.args[0][0].headers.accept, "application/vnd.github.v3+json");
5153
t.regex(serverHandler.args[0][0].headers.via, /proxy/);
52-
t.truthy(serverHandler.args[0][0].headers['x-forwarded-for']);
54+
t.truthy(serverHandler.args[0][0].headers["x-forwarded-for"]);
5355

5456
await promisify(proxy.destroy).bind(proxy)();
5557
await promisify(server.destroy).bind(server)();
5658
});
5759

58-
serial('Use a https proxy', async (t) => {
60+
test.serial("Use a https proxy", async (t) => {
5961
const server = _createServer({
60-
key: await readFile(join(__dirname, '/fixtures/ssl/ssl-cert-snakeoil.key')),
61-
cert: await readFile(join(__dirname, '/fixtures/ssl/ssl-cert-snakeoil.pem')),
62+
key: await readFile(join(__dirname, "/fixtures/ssl/ssl-cert-snakeoil.key")),
63+
cert: await readFile(join(__dirname, "/fixtures/ssl/ssl-cert-snakeoil.pem")),
6264
});
6365
await promisify(server.listen).bind(server)();
6466
const serverPort = server.address().port;
@@ -68,66 +70,73 @@ serial('Use a https proxy', async (t) => {
6870
const proxyPort = proxy.address().port;
6971
serverDestroy(proxy);
7072

71-
const proxyHandler = spy();
72-
const serverHandler = spy((request, response) => {
73+
const proxyHandler = sinon.spy();
74+
const serverHandler = sinon.spy((request, response) => {
7375
response.end();
7476
});
75-
proxy.on('connect', proxyHandler);
76-
server.on('request', serverHandler);
77+
proxy.on("connect", proxyHandler);
78+
server.on("request", serverHandler);
7779

7880
const github = getClient({
79-
githubToken: 'github_token',
81+
githubToken: "github_token",
8082
githubUrl: `https://localhost:${serverPort}`,
81-
githubApiPathPrefix: '',
82-
proxy: {host: 'localhost', port: proxyPort, headers: {foo: 'bar'}},
83+
githubApiPathPrefix: "",
84+
proxy: { host: "localhost", port: proxyPort, headers: { foo: "bar" } },
8385
});
8486

85-
await github.repos.get({repo: 'repo', owner: 'owner'});
87+
await github.repos.get({ repo: "repo", owner: "owner" });
8688

8789
t.is(proxyHandler.args[0][0].url, `localhost:${serverPort}`);
88-
t.is(proxyHandler.args[0][0].headers.foo, 'bar');
89-
t.is(serverHandler.args[0][0].headers.accept, 'application/vnd.github.v3+json');
90+
t.is(proxyHandler.args[0][0].headers.foo, "bar");
91+
t.is(serverHandler.args[0][0].headers.accept, "application/vnd.github.v3+json");
9092

9193
await promisify(proxy.destroy).bind(proxy)();
9294
await promisify(server.destroy).bind(server)();
9395
});
9496

95-
serial('Do not use a proxy if set to false', async (t) => {
97+
test.serial("Do not use a proxy if set to false", async (t) => {
9698
const server = createServer();
9799
await promisify(server.listen).bind(server)();
98100
const serverPort = server.address().port;
99101
serverDestroy(server);
100102

101-
const serverHandler = spy((request, response) => {
103+
const serverHandler = sinon.spy((request, response) => {
102104
response.end();
103105
});
104-
server.on('request', serverHandler);
106+
server.on("request", serverHandler);
105107

106108
const github = getClient({
107-
githubToken: 'github_token',
109+
githubToken: "github_token",
108110
githubUrl: `http://localhost:${serverPort}`,
109-
githubApiPathPrefix: '',
111+
githubApiPathPrefix: "",
110112
proxy: false,
111113
});
112114

113-
await github.repos.get({repo: 'repo', owner: 'owner'});
115+
await github.repos.get({ repo: "repo", owner: "owner" });
114116

115-
t.is(serverHandler.args[0][0].headers.accept, 'application/vnd.github.v3+json');
117+
t.is(serverHandler.args[0][0].headers.accept, "application/vnd.github.v3+json");
116118
t.falsy(serverHandler.args[0][0].headers.via);
117-
t.falsy(serverHandler.args[0][0].headers['x-forwarded-for']);
119+
t.falsy(serverHandler.args[0][0].headers["x-forwarded-for"]);
118120

119121
await promisify(server.destroy).bind(server)();
120122
});
121123

122-
test('Use the global throttler for all endpoints', async (t) => {
124+
test.serial("Use the global throttler for all endpoints", async (t) => {
123125
const rate = 150;
124126

125127
const octokit = new Octokit();
126-
octokit.hook.wrap('request', () => Date.now());
127-
const github = proxyquire('../lib/get-client', {
128-
'@octokit/rest': {Octokit: stub().returns(octokit)},
129-
'./definitions/rate-limit': {RATE_LIMITS: {search: 1, core: 1}, GLOBAL_RATE_LIMIT: rate},
130-
})({githubToken: 'token'});
128+
octokit.hook.wrap("request", () => Date.now());
129+
130+
await quibble.reset()
131+
await quibble.esm("../lib/definitions/rate-limit.js", {
132+
RATE_LIMITS: { search: 1, core: 1 },
133+
GLOBAL_RATE_LIMIT: rate,
134+
RETRY_CONF: {retries: 3, factor: 1, minTimeout: 1, maxTimeout: 1}
135+
});
136+
await quibble.esm("@octokit/rest", { Octokit: sinon.stub().returns(octokit) });
137+
const getClient = (await import("../lib/get-client.js")).default;
138+
139+
const github = getClient({ githubToken: "token" });
131140

132141
/* eslint-disable unicorn/prevent-abbreviations */
133142

@@ -152,16 +161,23 @@ test('Use the global throttler for all endpoints', async (t) => {
152161
/* eslint-enable unicorn/prevent-abbreviations */
153162
});
154163

155-
test('Use the same throttler for endpoints in the same rate limit group', async (t) => {
164+
test.serial("Use the same throttler for endpoints in the same rate limit group", async (t) => {
156165
const searchRate = 300;
157166
const coreRate = 150;
158167

159168
const octokit = new Octokit();
160-
octokit.hook.wrap('request', () => Date.now());
161-
const github = proxyquire('../lib/get-client', {
162-
'@octokit/rest': {Octokit: stub().returns(octokit)},
163-
'./definitions/rate-limit': {RATE_LIMITS: {search: searchRate, core: coreRate}, GLOBAL_RATE_LIMIT: 1},
164-
})({githubToken: 'token'});
169+
octokit.hook.wrap("request", () => Date.now());
170+
171+
await quibble.reset()
172+
await quibble.esm("../lib/definitions/rate-limit.js", {
173+
RATE_LIMITS: { search: searchRate, core: coreRate },
174+
GLOBAL_RATE_LIMIT: 1,
175+
RETRY_CONF: {retries: 3, factor: 1, minTimeout: 1, maxTimeout: 1}
176+
});
177+
await quibble.esm("@octokit/rest", { Octokit: sinon.stub().returns(octokit) });
178+
const getClient = (await import("../lib/get-client.js")).default;
179+
180+
const github = getClient({ githubToken: "token" });
165181

166182
/* eslint-disable unicorn/prevent-abbreviations */
167183

@@ -187,16 +203,23 @@ test('Use the same throttler for endpoints in the same rate limit group', async
187203
/* eslint-enable unicorn/prevent-abbreviations */
188204
});
189205

190-
test('Use different throttler for read and write endpoints', async (t) => {
206+
test.serial("Use different throttler for read and write endpoints", async (t) => {
191207
const writeRate = 300;
192208
const readRate = 150;
193209

194210
const octokit = new Octokit();
195-
octokit.hook.wrap('request', () => Date.now());
196-
const github = proxyquire('../lib/get-client', {
197-
'@octokit/rest': {Octokit: stub().returns(octokit)},
198-
'./definitions/rate-limit': {RATE_LIMITS: {core: {write: writeRate, read: readRate}}, GLOBAL_RATE_LIMIT: 1},
199-
})({githubToken: 'token'});
211+
octokit.hook.wrap("request", () => Date.now());
212+
213+
await quibble.reset()
214+
await quibble.esm("../lib/definitions/rate-limit.js", {
215+
RATE_LIMITS: { core: { write: writeRate, read: readRate } },
216+
GLOBAL_RATE_LIMIT: 1,
217+
RETRY_CONF: {retries: 3, factor: 1, minTimeout: 1, maxTimeout: 1}
218+
});
219+
await quibble.esm("@octokit/rest", { Octokit: sinon.stub().returns(octokit) });
220+
const getClient = (await import("../lib/get-client.js")).default;
221+
222+
const github = getClient({ githubToken: "token" });
200223

201224
const a = await github.repos.get();
202225
const b = await github.repos.get();
@@ -209,30 +232,33 @@ test('Use different throttler for read and write endpoints', async (t) => {
209232
t.true(inRange(d - c, writeRate - 50, writeRate + 50));
210233
});
211234

212-
test('Use the same throttler when retrying', async (t) => {
235+
test.serial("Use the same throttler when retrying", async (t) => {
213236
const coreRate = 200;
214-
const request = stub().callsFake(async () => {
237+
const request = sinon.stub().callsFake(async () => {
215238
const error = new Error();
216239
error.time = Date.now();
217240
error.status = 404;
218241
throw error;
219242
});
220243
const octokit = new Octokit();
221-
octokit.hook.wrap('request', request);
222-
const github = proxyquire('../lib/get-client', {
223-
'@octokit/rest': {Octokit: stub().returns(octokit)},
224-
'./definitions/rate-limit': {
225-
RETRY_CONF: {retries: 3, factor: 1, minTimeout: 1},
226-
RATE_LIMITS: {core: coreRate},
227-
GLOBAL_RATE_LIMIT: 1,
228-
},
229-
})({githubToken: 'token'});
244+
octokit.hook.wrap("request", request);
245+
246+
await quibble.reset()
247+
await quibble.esm("../lib/definitions/rate-limit.js", {
248+
RATE_LIMITS: { core: coreRate },
249+
GLOBAL_RATE_LIMIT: 1,
250+
RETRY_CONF: { retries: 3, factor: 1, minTimeout: 1 },
251+
});
252+
await quibble.esm("@octokit/rest", { Octokit: sinon.stub().returns(octokit) });
253+
const getClient = (await import("../lib/get-client.js")).default;
254+
255+
const github = getClient({ githubToken: "token" });
230256

231257
await t.throwsAsync(github.repos.createRelease());
232-
const {time: a} = await t.throwsAsync(request.getCall(0).returnValue);
233-
const {time: b} = await t.throwsAsync(request.getCall(1).returnValue);
234-
const {time: c} = await t.throwsAsync(request.getCall(2).returnValue);
235-
const {time: d} = await t.throwsAsync(request.getCall(3).returnValue);
258+
const { time: a } = await t.throwsAsync(request.getCall(0).returnValue);
259+
const { time: b } = await t.throwsAsync(request.getCall(1).returnValue);
260+
const { time: c } = await t.throwsAsync(request.getCall(2).returnValue);
261+
const { time: d } = await t.throwsAsync(request.getCall(3).returnValue);
236262

237263
// Each retry should be done after `coreRate` ms
238264
t.true(inRange(b - a, coreRate - 50, coreRate + 50));

0 commit comments

Comments
 (0)