Skip to content

Commit 10b2301

Browse files
committed
WIP - imported from other module
1 parent 3001e5f commit 10b2301

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

lib/http-client.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import abslog from 'abslog';
1717
* @property {Number} reset - Circuit breaker: How long, in milliseconds, to wait before a tripped circuit should be reset.
1818
**/
1919

20-
export default class PodiumHttpClient {
20+
export default class HttpClient {
2121
#throwOn400;
2222
#throwOn500;
2323
#breaker;
@@ -64,7 +64,7 @@ export default class PodiumHttpClient {
6464
if (this.#throwOn400 && statusCode >= 400 && statusCode <= 499) {
6565
// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
6666
const errBody = await body.text();
67-
this.#logger.debug(
67+
this.#logger.trace(
6868
`HTTP ${statusCode} error catched by client. Body: ${errBody}`,
6969
);
7070
throw createError(statusCode);
@@ -74,7 +74,7 @@ export default class PodiumHttpClient {
7474
// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
7575
await body.text();
7676
const errBody = await body.text();
77-
this.#logger.debug(
77+
this.#logger.trace(
7878
`HTTP ${statusCode} error catched by client. Body: ${errBody}`,
7979
);
8080
throw createError(statusCode);
@@ -92,10 +92,6 @@ export default class PodiumHttpClient {
9292
this.#breaker.fallback(fn);
9393
}
9494

95-
metrics() {
96-
// TODO: Implement...
97-
}
98-
9995
async request(options = {}) {
10096
return await this.#breaker.fire(options);
10197
}

tests/http-client.test.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@ import test from 'node:test';
22
import assert from 'node:assert/strict';
33
import http from 'node:http';
44

5-
import PodiumHttpClient from '../lib/http-client.js';
5+
import HttpClient from '../lib/http-client.js';
66

77
let httpServer,
88
host = 'localhost',
99
port = 3003;
1010

11+
async function beforeEach() {
12+
httpServer = http.createServer(async (request, response) => {
13+
response.writeHead(200);
14+
response.end();
15+
});
16+
httpServer.listen(port, host, () => Promise.resolve());
17+
}
18+
1119
async function afterEach(client) {
1220
await client.close();
1321
await httpServer.close();
1422
}
1523

1624
test('http-client - basics', async (t) => {
17-
t.beforeEach(async function () {
18-
httpServer = http.createServer(async (request, response) => {
19-
response.writeHead(200);
20-
response.end();
21-
});
22-
httpServer.listen(port, host, () => Promise.resolve());
23-
});
2425
await t.test(
2526
'http-client: returns 200 response when given valid input',
2627
async () => {
28+
await beforeEach();
2729
const url = `http://${host}:${port}`;
28-
const client = new PodiumHttpClient();
30+
const client = new HttpClient();
2931
const response = await client.request({
3032
path: '/',
3133
origin: url,
@@ -37,8 +39,9 @@ test('http-client - basics', async (t) => {
3739
);
3840

3941
await t.test('does not cause havoc with built in fetch', async () => {
42+
await beforeEach();
4043
const url = `http://${host}:${port}`;
41-
const client = new PodiumHttpClient();
44+
const client = new HttpClient();
4245
await fetch(url);
4346
const response = await client.request({
4447
path: '/',
@@ -51,9 +54,10 @@ test('http-client - basics', async (t) => {
5154
await afterEach(client);
5255
});
5356

54-
test.skip('http-client: should not invalid port input', async () => {
57+
await test.skip('http-client: should not invalid port input', async () => {
58+
await beforeEach();
5559
const url = `http://${host}:3013`;
56-
const client = new PodiumHttpClient();
60+
const client = new HttpClient();
5761
await client.request({
5862
path: '/',
5963
origin: url,
@@ -65,13 +69,15 @@ test('http-client - basics', async (t) => {
6569
method: 'GET',
6670
});
6771
assert.strictEqual(response.statusCode, 200);
72+
await afterEach(client);
6873
});
6974
});
7075

7176
test.skip('http-client circuit breaker behaviour', async (t) => {
7277
await t.test('closes on failure threshold', async () => {
78+
await beforeEach();
7379
const url = `http://${host}:3014`;
74-
const client = new PodiumHttpClient({ threshold: 2 });
80+
const client = new HttpClient({ threshold: 2 });
7581
await client.request({
7682
path: '/',
7783
origin: url,

0 commit comments

Comments
 (0)