-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix (core,streams): support ResponseInit variants (#1766)
Co-authored-by: Sarah Shader <[email protected]>
- Loading branch information
Showing
6 changed files
with
135 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'ai': patch | ||
--- | ||
|
||
fix (core,streams): support ResponseInit variants |
This file contains 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 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,53 @@ | ||
import { expect, it } from 'vitest'; | ||
import { prepareResponseHeaders } from './prepare-response-headers'; | ||
|
||
it('should set Content-Type header if not present', () => { | ||
const headers = prepareResponseHeaders( | ||
{}, | ||
{ contentType: 'application/json' }, | ||
); | ||
|
||
expect(headers.get('Content-Type')).toBe('application/json'); | ||
}); | ||
|
||
it('should not overwrite existing Content-Type header', () => { | ||
const headers = prepareResponseHeaders( | ||
{ headers: { 'Content-Type': 'text/html' } }, | ||
{ contentType: 'application/json' }, | ||
); | ||
|
||
expect(headers.get('Content-Type')).toBe('text/html'); | ||
}); | ||
|
||
it('should handle undefined init', () => { | ||
const headers = prepareResponseHeaders(undefined, { | ||
contentType: 'application/json', | ||
}); | ||
|
||
expect(headers.get('Content-Type')).toBe('application/json'); | ||
}); | ||
|
||
it('should handle init headers as Headers object', () => { | ||
const headers = prepareResponseHeaders( | ||
{ headers: new Headers({ init: 'foo' }) }, | ||
{ contentType: 'application/json' }, | ||
); | ||
|
||
expect(headers.get('init')).toBe('foo'); | ||
expect(headers.get('Content-Type')).toBe('application/json'); | ||
}); | ||
|
||
it('should handle Response object headers', () => { | ||
const initHeaders = { init: 'foo' }; | ||
const response = new Response(null, { | ||
headers: { ...initHeaders, extra: 'bar' }, | ||
}); | ||
|
||
const headers = prepareResponseHeaders(response, { | ||
contentType: 'application/json', | ||
}); | ||
|
||
expect(headers.get('init')).toBe('foo'); | ||
expect(headers.get('extra')).toBe('bar'); | ||
expect(headers.get('Content-Type')).toBe('application/json'); | ||
}); |
This file contains 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,12 @@ | ||
export function prepareResponseHeaders( | ||
init: ResponseInit | undefined, | ||
{ contentType }: { contentType: string }, | ||
) { | ||
const headers = new Headers(init?.headers ?? {}); | ||
|
||
if (!headers.has('Content-Type')) { | ||
headers.set('Content-Type', contentType); | ||
} | ||
|
||
return headers; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,62 +1,60 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { expect, it } from 'vitest'; | ||
import { splitArray } from './split-array'; | ||
|
||
describe('splitArray', () => { | ||
it('should split an array into chunks of the specified size', () => { | ||
const array = [1, 2, 3, 4, 5]; | ||
const size = 2; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([[1, 2], [3, 4], [5]]); | ||
}); | ||
|
||
it('should return an empty array when the input array is empty', () => { | ||
const array: number[] = []; | ||
const size = 2; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should return the original array when the chunk size is greater than the array length', () => { | ||
const array = [1, 2, 3]; | ||
const size = 5; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([[1, 2, 3]]); | ||
}); | ||
|
||
it('should return the original array when the chunk size is equal to the array length', () => { | ||
const array = [1, 2, 3]; | ||
const size = 3; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([[1, 2, 3]]); | ||
}); | ||
|
||
it('should handle chunk size of 1 correctly', () => { | ||
const array = [1, 2, 3]; | ||
const size = 1; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([[1], [2], [3]]); | ||
}); | ||
|
||
it('should throw an error for chunk size of 0', () => { | ||
const array = [1, 2, 3]; | ||
const size = 0; | ||
expect(() => splitArray(array, size)).toThrow( | ||
'chunkSize must be greater than 0', | ||
); | ||
}); | ||
|
||
it('should throw an error for negative chunk size', () => { | ||
const array = [1, 2, 3]; | ||
const size = -1; | ||
expect(() => splitArray(array, size)).toThrow( | ||
'chunkSize must be greater than 0', | ||
); | ||
}); | ||
|
||
it('should handle non-integer chunk size by flooring the size', () => { | ||
const array = [1, 2, 3, 4, 5]; | ||
const size = 2.5; | ||
const result = splitArray(array, Math.floor(size)); | ||
expect(result).toEqual([[1, 2], [3, 4], [5]]); | ||
}); | ||
it('should split an array into chunks of the specified size', () => { | ||
const array = [1, 2, 3, 4, 5]; | ||
const size = 2; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([[1, 2], [3, 4], [5]]); | ||
}); | ||
|
||
it('should return an empty array when the input array is empty', () => { | ||
const array: number[] = []; | ||
const size = 2; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('should return the original array when the chunk size is greater than the array length', () => { | ||
const array = [1, 2, 3]; | ||
const size = 5; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([[1, 2, 3]]); | ||
}); | ||
|
||
it('should return the original array when the chunk size is equal to the array length', () => { | ||
const array = [1, 2, 3]; | ||
const size = 3; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([[1, 2, 3]]); | ||
}); | ||
|
||
it('should handle chunk size of 1 correctly', () => { | ||
const array = [1, 2, 3]; | ||
const size = 1; | ||
const result = splitArray(array, size); | ||
expect(result).toEqual([[1], [2], [3]]); | ||
}); | ||
|
||
it('should throw an error for chunk size of 0', () => { | ||
const array = [1, 2, 3]; | ||
const size = 0; | ||
expect(() => splitArray(array, size)).toThrow( | ||
'chunkSize must be greater than 0', | ||
); | ||
}); | ||
|
||
it('should throw an error for negative chunk size', () => { | ||
const array = [1, 2, 3]; | ||
const size = -1; | ||
expect(() => splitArray(array, size)).toThrow( | ||
'chunkSize must be greater than 0', | ||
); | ||
}); | ||
|
||
it('should handle non-integer chunk size by flooring the size', () => { | ||
const array = [1, 2, 3, 4, 5]; | ||
const size = 2.5; | ||
const result = splitArray(array, Math.floor(size)); | ||
expect(result).toEqual([[1, 2], [3, 4], [5]]); | ||
}); |
This file contains 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