Skip to content

Commit

Permalink
fix(js-runtime): make Headers class case insensitive (lagonapp#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyco130 committed Nov 2, 2022
1 parent 0404f3f commit 0efab5a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-teachers-jog.md
@@ -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
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
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

0 comments on commit 0efab5a

Please sign in to comment.