-
Notifications
You must be signed in to change notification settings - Fork 619
feat(protocol-http): implement SRA HttpRequest #4514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import { Fields } from "./Fields"; | ||
import { getHeadersProxy, headersToFields, headerValueToFieldValues } from "./headersProxy"; | ||
|
||
describe("getHeadersProxy", () => { | ||
const mockHeaders = { | ||
foo: "bar", | ||
baz: "qux,quux", | ||
}; | ||
const mockFields = Fields.from([ | ||
{ name: "foo", values: ["bar"] }, | ||
{ name: "baz", values: ["qux", "quux"] }, | ||
]); | ||
|
||
describe("proxy works like a normal Record", () => { | ||
describe("access and mutation", () => { | ||
it("can get and set individual keys", () => { | ||
const headers = getHeadersProxy(new Fields({})); | ||
headers["foo"] = "bar"; | ||
headers.baz = "qux,quux"; | ||
expect(headers["foo"]).toEqual("bar"); | ||
expect(headers.baz).toEqual("qux,quux"); | ||
}); | ||
|
||
it("can be updated using object spread syntax", () => { | ||
let headers = getHeadersProxy(mockFields); | ||
headers = { ...headers, another: "value" }; | ||
expect(headers).toEqual({ ...mockHeaders, another: "value" }); | ||
}); | ||
|
||
it("can delete keys", () => { | ||
const headers = getHeadersProxy(new Fields({ fields: mockFields.getAll() })); | ||
delete headers["foo"]; | ||
delete headers.baz; | ||
expect(headers).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe("iteration", () => { | ||
it("can be iterated over using Object.keys", () => { | ||
const headers = getHeadersProxy(mockFields); | ||
const keys = Object.keys(headers); | ||
expect(keys).toEqual(["foo", "baz"]); | ||
}); | ||
|
||
it("can be iterated over using Object.values", () => { | ||
const headers = getHeadersProxy(mockFields); | ||
const values = Object.values(headers); | ||
expect(values).toEqual(["bar", "qux,quux"]); | ||
}); | ||
|
||
it("can be iterated over using Object.entries", () => { | ||
const headers = getHeadersProxy(mockFields); | ||
const entries = Object.entries(headers); | ||
expect(entries).toEqual([ | ||
["foo", "bar"], | ||
["baz", "qux,quux"], | ||
]); | ||
}); | ||
|
||
it("can be iterated over using `for..in`", () => { | ||
const keys: string[] = []; | ||
const headers = getHeadersProxy(mockFields); | ||
for (const key in headers) { | ||
keys.push(key); | ||
} | ||
expect(keys).toEqual(["foo", "baz"]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("proxies the fields", () => { | ||
it("updates fields when individual keys are set on headers", () => { | ||
const fields = new Fields({}); | ||
const headers = getHeadersProxy(fields); | ||
headers["foo"] = "bar"; | ||
headers.baz = "qux,quux"; | ||
expect(fields).toEqual(mockFields); | ||
}); | ||
|
||
it("updates fields when keys are deleted from headers", () => { | ||
const fields = new Fields({ fields: mockFields.getAll() }); | ||
const headers = getHeadersProxy(fields); | ||
delete headers["foo"]; | ||
delete headers.baz; | ||
expect(fields).toEqual(new Fields({})); | ||
}); | ||
|
||
it("can get values from fields or headers", () => { | ||
const headers = getHeadersProxy(mockFields); | ||
expect(headers["foo"]).toEqual(mockFields.getField("foo")?.values.join(",")); | ||
expect(headers.baz).toEqual(mockFields.getField("baz")?.values.join(",")); | ||
}); | ||
|
||
it("does not proxy class properties of fields", () => { | ||
const fields = new Fields({}); | ||
Object.defineProperty(fields, "foo", { | ||
value: "bar", | ||
enumerable: true, | ||
writable: true, | ||
configurable: true, | ||
}); | ||
const headers = getHeadersProxy(fields); | ||
Object.keys(fields).forEach((key) => { | ||
expect(headers[key]).toBe(undefined); | ||
}); | ||
}); | ||
|
||
it("can use Object prototype methods", () => { | ||
const fields = new Fields({ fields: mockFields.getAll() }); | ||
const headers = getHeadersProxy(fields); | ||
delete headers["foo"]; | ||
Object.defineProperty(headers, "fizz", { | ||
value: "buzz", | ||
enumerable: true, | ||
writable: true, | ||
configurable: true, | ||
}); | ||
expect(headers.hasOwnProperty("foo")).toBe(false); | ||
expect(headers.hasOwnProperty("baz")).toBe(true); | ||
expect(headers.hasOwnProperty("fizz")).toBe(true); | ||
expect(headers["fizz"]).toEqual("buzz"); | ||
expect("fizz" in headers).toBe(true); | ||
expect(headers.hasOwnProperty("encoding")).toBe(false); | ||
expect({ ...headers }).toEqual({ fizz: "buzz", baz: "qux,quux" }); | ||
expect(fields.getField("foo")).not.toBeDefined(); | ||
expect(fields.getField("fizz")?.toString()).toEqual("buzz"); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("headersToFields", () => { | ||
it("ignores null and undefined values", () => { | ||
const headers = { foo: null as any, bar: undefined as any }; | ||
const fields = headersToFields(headers); | ||
expect(fields.getField("foo")).not.toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("headerValueToFieldValues", () => { | ||
it("ignores null and undefined values", () => { | ||
expect(headerValueToFieldValues(undefined as any)).not.toBeDefined(); | ||
expect(headerValueToFieldValues(null as any)).not.toBeDefined(); | ||
}); | ||
it("parses single string value", () => { | ||
const headerValue = "foo"; | ||
expect(headerValueToFieldValues(headerValue)).toEqual(["foo"]); | ||
}); | ||
it("parses comma-separated string value", () => { | ||
const headerValue = "foo,bar"; | ||
expect(headerValueToFieldValues(headerValue)).toEqual(["foo", "bar"]); | ||
}); | ||
it("preserves whitespace", () => { | ||
const headerValue = "foo, bar "; | ||
expect(headerValueToFieldValues(headerValue)).toEqual(["foo", " bar "]); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.