From 71f5a4f382002b64aebd3b89b0d30f070bcb51d2 Mon Sep 17 00:00:00 2001 From: SofianD Date: Tue, 18 Jun 2024 17:56:02 +0200 Subject: [PATCH 1/4] fix(createHeaders): custom headers overwrite default header test(jestSnapchot): update expected body test: check if 'createHeaders' overwrite the 'user-agent' header --- src/utils.ts | 3 ++- test/__snapshots__/request.spec.ts.snap | 4 ++-- test/utils.spec.ts | 8 ++++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index cb89f37..633b94c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -39,7 +39,8 @@ export function getEncodingCharset(charset = kDefaultEncodingCharset): BufferEnc * - Authorization */ export function createHeaders(options: Partial>): IncomingHttpHeaders { - const headers = Object.assign(options.headers ?? {}, DEFAULT_HEADER); + const headers = Object.assign({ ...DEFAULT_HEADER }, options.headers ?? {}); + if (options.authorization) { headers.Authorization = createAuthorizationHeader(options.authorization); } diff --git a/test/__snapshots__/request.spec.ts.snap b/test/__snapshots__/request.spec.ts.snap index 15436eb..c1bdd33 100644 --- a/test/__snapshots__/request.spec.ts.snap +++ b/test/__snapshots__/request.spec.ts.snap @@ -8,7 +8,7 @@ exports[`http.get should throw a 404 Not Found error because the path is not kno

Not Found

The requested URL was not found on this server.


-
Apache/2.4.56 (Debian) Server at ws-dev.myunisoft.fr Port 443
+
Apache/2.4.59 (Debian) Server at ws-dev.myunisoft.fr Port 443
" `; @@ -21,7 +21,7 @@ exports[`http.safeGet should throw a 404 Not Found error because the path is not

Not Found

The requested URL was not found on this server.


-
Apache/2.4.56 (Debian) Server at ws-dev.myunisoft.fr Port 443
+
Apache/2.4.59 (Debian) Server at ws-dev.myunisoft.fr Port 443
" `; diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 5c25a10..c57f640 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -57,6 +57,14 @@ describe("createHeaders", () => { expect(result).toEqual({ foo: "bar", "user-agent": "httpie" }); }); + it("should overwrite the 'user-agent' header", () => { + const result = Utils.createHeaders({ + headers: { "user-agent": "myUserAgent" } + }); + + expect(result).toEqual({ "user-agent": "myUserAgent" }); + }); + it("should add authorization header (and override original property)", () => { const result = Utils.createHeaders({ headers: { From 7bf8cd79039e0dee23ed760d423a0a3f218292fe Mon Sep 17 00:00:00 2001 From: fraxken Date: Fri, 21 Jun 2024 23:21:59 +0200 Subject: [PATCH 2/4] fix: update @openally/result to v1.2.1 --- package.json | 2 +- test/safeRequest.spec.ts | 68 ++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 45 deletions(-) diff --git a/package.json b/package.json index 3b47c2d..beee20c 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "typescript": "^5.3.3" }, "dependencies": { - "@openally/result": "^1.2.0", + "@openally/result": "^1.2.1", "content-type": "^1.0.5", "lru-cache": "^10.1.0", "statuses": "^2.0.1", diff --git a/test/safeRequest.spec.ts b/test/safeRequest.spec.ts index 027bec2..2dc36d0 100644 --- a/test/safeRequest.spec.ts +++ b/test/safeRequest.spec.ts @@ -48,8 +48,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHTTPError(error)).toBeTruthy(); expect(error.statusCode).toBe(statusCode); expect(error.data).toBe(payload.toString()); @@ -99,8 +98,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHTTPError(error)).toBeTruthy(); expect(error.statusCode).toBe(statusCode); expect(error.data).toBe(payload.toString()); @@ -150,8 +148,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHTTPError(error)).toBeTruthy(); expect(error.statusCode).toBe(statusCode); expect(error.data).toBe(payload.toString()); @@ -201,8 +198,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHTTPError(error)).toBeTruthy(); expect(error.statusCode).toBe(statusCode); expect(error.data).toBe(payload.toString()); @@ -508,8 +504,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHttpieError(error)).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to parse the response body (reason: 'invalid media type')." @@ -539,8 +534,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toStrictEqual(payload); expect(error.headers).toMatchObject(headers); @@ -739,8 +733,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHttpieError(error)).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to parse the response body (reason: 'invalid media type')." @@ -770,8 +763,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toStrictEqual(payload); expect(error.headers).toMatchObject(headers); @@ -970,8 +962,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHttpieError(error)).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to parse the response body (reason: 'invalid media type')." @@ -1001,8 +992,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toStrictEqual(payload); expect(error.headers).toMatchObject(headers); @@ -1201,8 +1191,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(isHttpieError(error)).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to parse the response body (reason: 'invalid media type')." @@ -1232,8 +1221,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toStrictEqual(payload); expect(error.headers).toMatchObject(headers); @@ -1369,8 +1357,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toMatchObject(payload); expect(error.headers).toMatchObject(headers); @@ -1398,9 +1385,8 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; - expect(err.cause).toBeTruthy(); + catch (error) { + expect(error).toBeTruthy(); expect(error.reason).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to decompress the response body (reason: 'incorrect header check')." @@ -1535,8 +1521,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toMatchObject(payload); expect(error.headers).toMatchObject(headers); @@ -1564,9 +1549,8 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; - expect(err.cause).toBeTruthy(); + catch (error) { + expect(error).toBeTruthy(); expect(error.reason).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to decompress the response body (reason: 'incorrect header check')." @@ -1701,8 +1685,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toMatchObject(payload); expect(error.headers).toMatchObject(headers); @@ -1730,9 +1713,8 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; - expect(err.cause).toBeTruthy(); + catch (error) { + expect(error).toBeTruthy(); expect(error.reason).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to decompress the response body (reason: 'incorrect header check')." @@ -1867,8 +1849,7 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; + catch (error) { expect(error.message).toStrictEqual("Unsupported encoding 'unknown'."); expect(error.buffer).toMatchObject(payload); expect(error.headers).toMatchObject(headers); @@ -1896,9 +1877,8 @@ describe("Httpie.safeRequest", () => { expect(result.ok).toBeFalsy(); result.unwrap(); } - catch (err) { - const error = err.cause; - expect(err.cause).toBeTruthy(); + catch (error) { + expect(error).toBeTruthy(); expect(error.reason).toBeTruthy(); expect(error.message).toStrictEqual( "An unexpected error occurred when trying to decompress the response body (reason: 'incorrect header check')." From b1bec6b47d1526e026e26784cca345a1b69a5667 Mon Sep 17 00:00:00 2001 From: fraxken Date: Fri, 21 Jun 2024 23:23:30 +0200 Subject: [PATCH 3/4] ci: drop support for Node.js 18 --- .github/workflows/node.js.yml | 2 +- README.md | 2 +- package.json | 106 +++++++++++++++++----------------- 3 files changed, 55 insertions(+), 55 deletions(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index d1228f5..ed42954 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -19,7 +19,7 @@ jobs: strategy: matrix: - node-version: [18.x, 20.x] + node-version: [20.x] # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ steps: diff --git a/README.md b/README.md index e5259e5..28e421b 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Light with seriously maintained dependencies: ![](./docs/images/nodesecure.PNG) ## 🚧 Requirements -- [Node.js](https://nodejs.org/en/) version 18 or higher +- [Node.js](https://nodejs.org/en/) version 20 or higher ## 🚀 Getting Started diff --git a/package.json b/package.json index beee20c..435664e 100644 --- a/package.json +++ b/package.json @@ -1,53 +1,53 @@ -{ - "name": "@myunisoft/httpie", - "version": "4.0.1", - "description": "MyUnisoft Node.js HTTP client that use Undici client", - "main": "dist/index.js", - "types": "dist/index.d.ts", - "scripts": { - "build": "tsc", - "prepublishOnly": "npm run build", - "test": "jest" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/MyUnisoft/httpie.git" - }, - "engines": { - "node": ">=18" - }, - "keywords": [], - "files": [ - "dist" - ], - "publishConfig": { - "@myunisoft:registry": "https://registry.npmjs.org/" - }, - "author": "GENTILHOMME Thomas ", - "license": "MIT", - "bugs": { - "url": "https://github.com/MyUnisoft/httpie/issues" - }, - "homepage": "https://github.com/MyUnisoft/httpie#readme", - "devDependencies": { - "@nodesecure/eslint-config": "^1.9.0", - "@types/content-type": "^1.1.8", - "@types/jest": "^29.5.11", - "@types/lru-cache": "^7.10.10", - "@types/node": "^20.10.4", - "@types/statuses": "^2.0.4", - "dotenv": "^16.3.1", - "fastify": "^4.24.3", - "jest": "^29.7.0", - "p-ratelimit": "^1.0.1", - "ts-jest": "^29.1.1", - "typescript": "^5.3.3" - }, - "dependencies": { - "@openally/result": "^1.2.1", - "content-type": "^1.0.5", - "lru-cache": "^10.1.0", - "statuses": "^2.0.1", - "undici": "^6.9.0" - } -} +{ + "name": "@myunisoft/httpie", + "version": "4.0.1", + "description": "MyUnisoft Node.js HTTP client that use Undici client", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc", + "prepublishOnly": "npm run build", + "test": "jest" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/MyUnisoft/httpie.git" + }, + "engines": { + "node": ">=20" + }, + "keywords": [], + "files": [ + "dist" + ], + "publishConfig": { + "@myunisoft:registry": "https://registry.npmjs.org/" + }, + "author": "GENTILHOMME Thomas ", + "license": "MIT", + "bugs": { + "url": "https://github.com/MyUnisoft/httpie/issues" + }, + "homepage": "https://github.com/MyUnisoft/httpie#readme", + "devDependencies": { + "@nodesecure/eslint-config": "^1.9.0", + "@types/content-type": "^1.1.8", + "@types/jest": "^29.5.11", + "@types/lru-cache": "^7.10.10", + "@types/node": "^20.10.4", + "@types/statuses": "^2.0.4", + "dotenv": "^16.3.1", + "fastify": "^4.24.3", + "jest": "^29.7.0", + "p-ratelimit": "^1.0.1", + "ts-jest": "^29.1.1", + "typescript": "^5.3.3" + }, + "dependencies": { + "@openally/result": "^1.2.1", + "content-type": "^1.0.5", + "lru-cache": "^10.1.0", + "statuses": "^2.0.1", + "undici": "^6.9.0" + } +} From 272a29ebbd611c87a7f64d40fcaf8f3c17ecb496 Mon Sep 17 00:00:00 2001 From: fraxken Date: Fri, 21 Jun 2024 23:25:10 +0200 Subject: [PATCH 4/4] fix: use API with valide SSL certificate --- test/request.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/request.spec.ts b/test/request.spec.ts index 2a6f7c3..0ab9697 100644 --- a/test/request.spec.ts +++ b/test/request.spec.ts @@ -74,7 +74,7 @@ describe("http.get", () => { }); it("should GET json response from node.js health endpoint", async() => { - const { data } = await get("https://yoda.myunisoft.fr:1407/api/v1/ipa/healthz"); + const { data } = await get("https://yoda.myunisoft.fr:1337/api/authenticate/healthz"); expect(Object.keys(data).sort()).toMatchObject([ "status", "version", "description", "checks"