Skip to content
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

fix(js-runtime): make Headers class case insensitive #227

Merged
merged 2 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-teachers-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/js-runtime': patch
---

Make Headers class case insensitive
Empty file modified packages/cli/npm/run.js
100644 → 100755
Empty file.
26 changes: 14 additions & 12 deletions packages/js-runtime/src/__tests__/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ describe('Headers', () => {
it('should instanciate with init as object', () => {
const headers = new Headers({ 'Content-Type': 'image/jpeg', 'X-My-Custom-Header': 'Zeke are cool' });
expect(Array.from(headers.entries())).toEqual([
['Content-Type', 'image/jpeg'],
['X-My-Custom-Header', 'Zeke are cool'],
['content-type', 'image/jpeg'],
['x-my-custom-header', 'Zeke are cool'],
]);
});

Expand All @@ -65,24 +65,24 @@ describe('Headers', () => {
it('should append', () => {
const headers = new Headers();
headers.append('a', 'b');
headers.append('c', 'd');
headers.append('C', 'd');
expect(headers.get('a')).toEqual('b');
expect(headers.get('c')).toEqual('d');
});

it('should delete', () => {
const headers = new Headers({
a: 'b',
A: 'b',
c: 'd',
});
headers.delete('a');
expect(headers.get('a')).toBeUndefined();
expect(headers.get('A')).toBeUndefined();
});

it('should return entries', () => {
const headers = new Headers({
a: 'b',
c: 'd',
C: 'd',
});
expect(Array.from(headers.entries())).toEqual([
['a', 'b'],
Expand All @@ -103,7 +103,7 @@ describe('Headers', () => {
it('should has', () => {
const headers = new Headers({
a: 'b',
c: 'd',
C: 'd',
});
expect(headers.has('a')).toBeTruthy();
expect(headers.has('c')).toBeTruthy();
Expand All @@ -113,7 +113,7 @@ describe('Headers', () => {
it('should return keys', () => {
const headers = new Headers({
a: 'b',
c: 'd',
C: 'd',
});
expect(Array.from(headers.keys())).toEqual(['a', 'c']);
});
Expand All @@ -122,20 +122,22 @@ describe('Headers', () => {
it('should set without init', () => {
const headers = new Headers();
headers.set('a', 'b');
headers.set('c', 'd');
headers.set('C', 'd');
expect(headers.get('a')).toEqual('b');
expect(headers.get('c')).toEqual('d');
});

it('should set with init', () => {
const headers = new Headers({
a: 'b',
c: 'd',
C: 'd',
});
headers.set('a', 'e');
headers.set('c', 'f');
headers.set('B', 'f');
headers.set('c', 'g');
expect(headers.get('a')).toEqual('e');
expect(headers.get('c')).toEqual('f');
expect(headers.get('b')).toEqual('f');
expect(headers.get('C')).toEqual('g');
});
});

Expand Down
6 changes: 6 additions & 0 deletions packages/js-runtime/src/runtime/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class Headers {
}

private addValue(name: string, value: string) {
name = name.toLowerCase();
const values = this.headers.get(name);

if (values) {
Expand All @@ -29,10 +30,12 @@ export class Headers {
}

append(name: string, value: string) {
name = name.toLowerCase();
this.addValue(name, value);
}

delete(name: string) {
name = name.toLowerCase();
this.headers.delete(name);
}

Expand All @@ -45,10 +48,12 @@ export class Headers {
}

get(name: string): string | undefined {
name = name.toLowerCase();
return this.headers.get(name)?.[0];
}

has(name: string): boolean {
name = name.toLowerCase();
return this.headers.has(name);
}

Expand All @@ -57,6 +62,7 @@ export class Headers {
}

set(name: string, value: string) {
name = name.toLowerCase();
this.headers.set(name, [value]);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/runtime/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ async fn return_headers() {
));

let mut headers = HashMap::new();
headers.insert("Content-Type".into(), "text/html".into());
headers.insert("X-Test".into(), "test".into());
headers.insert("content-type".into(), "text/html".into());
headers.insert("x-test".into(), "test".into());

let (tx, rx) = flume::unbounded();
isolate.run(Request::default(), tx).await;
Expand Down Expand Up @@ -222,8 +222,8 @@ async fn return_headers_from_headers_api() {
));

let mut headers = HashMap::new();
headers.insert("Content-Type".into(), "text/html".into());
headers.insert("X-Test".into(), "test".into());
headers.insert("content-type".into(), "text/html".into());
headers.insert("x-test".into(), "test".into());

let (tx, rx) = flume::unbounded();
isolate.run(Request::default(), tx).await;
Expand Down