diff --git a/lib/http-client.js b/lib/http-client.js index a34156a..dd2dbdd 100644 --- a/lib/http-client.js +++ b/lib/http-client.js @@ -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; @@ -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); @@ -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); @@ -92,10 +92,6 @@ export default class PodiumHttpClient { this.#breaker.fallback(fn); } - metrics() { - // TODO: Implement... - } - async request(options = {}) { return await this.#breaker.fire(options); } diff --git a/tests/http-client.test.js b/tests/http-client.test.js index 6237ccf..1ad1d87 100644 --- a/tests/http-client.test.js +++ b/tests/http-client.test.js @@ -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, @@ -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: '/', @@ -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, @@ -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,