Skip to content

Commit

Permalink
Merge pull request #248 from MyUnisoft/fix-create-headers
Browse files Browse the repository at this point in the history
fix(createHeaders): custom headers overwrite default header
  • Loading branch information
fraxken authored Jun 21, 2024
2 parents 254b32b + 272a29e commit 8c94713
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
106 changes: 53 additions & 53 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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.0",
"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 <[email protected]>",
"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"
}
}
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export function getEncodingCharset(charset = kDefaultEncodingCharset): BufferEnc
* - Authorization
*/
export function createHeaders(options: Partial<Pick<RequestOptions, "headers" | "authorization">>): 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);
}
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/request.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exports[`http.get should throw a 404 Not Found error because the path is not kno
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.56 (Debian) Server at ws-dev.myunisoft.fr Port 443</address>
<address>Apache/2.4.59 (Debian) Server at ws-dev.myunisoft.fr Port 443</address>
</body></html>
"
`;
Expand All @@ -21,7 +21,7 @@ exports[`http.safeGet should throw a 404 Not Found error because the path is not
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.56 (Debian) Server at ws-dev.myunisoft.fr Port 443</address>
<address>Apache/2.4.59 (Debian) Server at ws-dev.myunisoft.fr Port 443</address>
</body></html>
"
`;
2 changes: 1 addition & 1 deletion test/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("http.get", () => {
});

it("should GET json response from node.js health endpoint", async() => {
const { data } = await get<any>("https://yoda.myunisoft.fr:1407/api/v1/ipa/healthz");
const { data } = await get<any>("https://yoda.myunisoft.fr:1337/api/authenticate/healthz");

expect(Object.keys(data).sort()).toMatchObject([
"status", "version", "description", "checks"
Expand Down
68 changes: 24 additions & 44 deletions test/safeRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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')."
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')."
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')."
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')."
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')."
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')."
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')."
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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')."
Expand Down
8 changes: 8 additions & 0 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 8c94713

Please sign in to comment.