Skip to content

Commit

Permalink
fix: change LF to CRLF symbol in getAllResponseHeaders facade's method (
Browse files Browse the repository at this point in the history
#163)

* fix: change LF to CRLF symbol in getAllResponseHeaders facade's method

* fix: resolve prettier errors

* fix: resolve prettier errors

* fix: resolve prettier errors

---------

Co-authored-by: Aleksey Ivasyuta <[email protected]>
  • Loading branch information
avivasyuta and Aleksey Ivasyuta authored May 6, 2023
1 parent b8cf047 commit 8853473
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
58 changes: 36 additions & 22 deletions src/misc/headers.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
//helper
const convert = function (h, dest) {
let name;
const CRLF = "\r\n";

const objectToString = function (headersObj) {
const entries = Object.entries(headersObj);

const headers = entries.map(([name, value]) => {
return `${name.toLowerCase()}: ${value}`;
});

return headers.join(CRLF);
};

const stringToObject = function (headersString, dest) {
const headers = headersString.split(CRLF);
if (dest == null) {
dest = {};
}
switch (typeof h) {
case "object":
var headers = [];
for (let k in h) {
const v = h[k];
name = k.toLowerCase();
headers.push(`${name}:\t${v}`);
}
return headers.join("\n") + "\n";
case "string":
headers = h.split("\n");
for (let header of Array.from(headers)) {
if (/([^:]+):\s*(.+)/.test(header)) {
name = RegExp.$1 != null ? RegExp.$1.toLowerCase() : undefined;
const value = RegExp.$2;
if (dest[name] == null) {
dest[name] = value;
}
}

for (let header of headers) {
if (/([^:]+):\s*(.+)/.test(header)) {
const name = RegExp.$1 != null ? RegExp.$1.toLowerCase() : undefined;
const value = RegExp.$2;
if (dest[name] == null) {
dest[name] = value;
}
return dest;
}
}

return dest;
};

const convert = function (headers, dest) {
switch (typeof headers) {
case "object": {
return objectToString(headers);
}
case "string": {
return stringToObject(headers, dest);
}
}

return [];
};

Expand Down
35 changes: 35 additions & 0 deletions tests/convert.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test, expect } from "@playwright/test";
import headers from "../src/misc/headers";

const CRLF = "\r\n";

const headersObj = {
"cache-control": "no-cache",
"content-encoding": "gzip",
"content-type": "multipart/mixed",
};

const headersString = `cache-control: no-cache${CRLF}content-encoding: gzip${CRLF}content-type: multipart/mixed`;

test.describe("convert helper", function () {
test("object to string", () => {
const result = headers.convert(headersObj);
expect(result).toEqual(headersString);
});

test("string to object", () => {
const result = headers.convert(headersString);
expect(result).toEqual(headersObj);
});

test("add result to dest when object", () => {
const result = {};
headers.convert(headersString, result);
expect(result).toEqual(headersObj);
});

test("converts header name to lowercase", () => {
const result = headers.convert({ Foo: "Bar" });
expect(result).toEqual("foo: Bar");
});
});
3 changes: 2 additions & 1 deletion tests/modify-headers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { test, expect } from "@playwright/test";

test("response should include Foo Header", async ({ page }) => {
await page.goto("http://127.0.0.1:8080/example/modify-headers.html");

const text = await page.locator("id=res").innerText();
expect(text).toContain("foo:\tBar");
expect(text).toContain("foo: Bar");
});

0 comments on commit 8853473

Please sign in to comment.