Skip to content

Commit

Permalink
WIP - imported from other module
Browse files Browse the repository at this point in the history
  • Loading branch information
leftieFriele committed Oct 29, 2024
1 parent 9d7553c commit da7e61a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
10 changes: 3 additions & 7 deletions lib/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import abslog from 'abslog';
* @property {Number} reset - Circuit breaker: How long, in milliseconds, to wait before a tripped circuit should be reset.
**/

export default class PodiumHttpClient {
export default class HttpClient {
#throwOn400;
#throwOn500;
#breaker;
Expand Down Expand Up @@ -64,7 +64,7 @@ export default class PodiumHttpClient {
if (this.#throwOn400 && statusCode >= 400 && statusCode <= 499) {
// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
const errBody = await body.text();
this.#logger.debug(
this.#logger.trace(
`HTTP ${statusCode} error catched by client. Body: ${errBody}`,
);
throw createError(statusCode);
Expand All @@ -74,7 +74,7 @@ export default class PodiumHttpClient {
// Body must be consumed; https://github.com/nodejs/undici/issues/583#issuecomment-855384858
await body.text();
const errBody = await body.text();
this.#logger.debug(
this.#logger.trace(
`HTTP ${statusCode} error catched by client. Body: ${errBody}`,
);
throw createError(statusCode);
Expand All @@ -92,10 +92,6 @@ export default class PodiumHttpClient {
this.#breaker.fallback(fn);
}

metrics() {
// TODO: Implement...
}

async request(options = {}) {
return await this.#breaker.fire(options);
}
Expand Down
32 changes: 19 additions & 13 deletions tests/http-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import http from 'node:http';

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

let httpServer,
host = 'localhost',
port = 3003;

async function beforeEach() {
httpServer = http.createServer(async (request, response) => {
response.writeHead(200);
response.end();
});
httpServer.listen(port, host, () => Promise.resolve());
}

async function afterEach(client) {
await client.close();
await httpServer.close();
}

test('http-client - basics', async (t) => {
t.beforeEach(async function () {
httpServer = http.createServer(async (request, response) => {
response.writeHead(200);
response.end();
});
httpServer.listen(port, host, () => Promise.resolve());
});
await t.test(
'http-client: returns 200 response when given valid input',
async () => {
await beforeEach();
const url = `http://${host}:${port}`;
const client = new PodiumHttpClient();
const client = new HttpClient();
const response = await client.request({
path: '/',
origin: url,
Expand All @@ -37,8 +39,9 @@ test('http-client - basics', async (t) => {
);

await t.test('does not cause havoc with built in fetch', async () => {
await beforeEach();
const url = `http://${host}:${port}`;
const client = new PodiumHttpClient();
const client = new HttpClient();
await fetch(url);
const response = await client.request({
path: '/',
Expand All @@ -51,9 +54,10 @@ test('http-client - basics', async (t) => {
await afterEach(client);
});

test.skip('http-client: should not invalid port input', async () => {
await test.skip('http-client: should not invalid port input', async () => {
await beforeEach();
const url = `http://${host}:3013`;
const client = new PodiumHttpClient();
const client = new HttpClient();
await client.request({
path: '/',
origin: url,
Expand All @@ -65,13 +69,15 @@ test('http-client - basics', async (t) => {
method: 'GET',
});
assert.strictEqual(response.statusCode, 200);
await afterEach(client);
});
});

test.skip('http-client circuit breaker behaviour', async (t) => {
await t.test('closes on failure threshold', async () => {
await beforeEach();
const url = `http://${host}:3014`;
const client = new PodiumHttpClient({ threshold: 2 });
const client = new HttpClient({ threshold: 2 });
await client.request({
path: '/',
origin: url,
Expand Down

0 comments on commit da7e61a

Please sign in to comment.