-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit Tests for Netlify Adapter split_headers function (#4108)
* Extract split_headers to its own file so it can be unit tested * Unit tests for split_headers * no need for a custom suite Co-authored-by: Rich Harris <hello@rich-harris.dev>
- Loading branch information
1 parent
ae6121e
commit 34bd4f5
Showing
5 changed files
with
89 additions
and
31 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
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,29 @@ | ||
/** | ||
* Splits headers into two categories: single value and multi value | ||
* @param {Headers} headers | ||
* @returns {{ | ||
* headers: Record<string, string>, | ||
* multiValueHeaders: Record<string, string[]> | ||
* }} | ||
*/ | ||
export function split_headers(headers) { | ||
/** @type {Record<string, string>} */ | ||
const h = {}; | ||
|
||
/** @type {Record<string, string[]>} */ | ||
const m = {}; | ||
|
||
headers.forEach((value, key) => { | ||
if (key === 'set-cookie') { | ||
// @ts-expect-error (headers.raw() is non-standard) | ||
m[key] = headers.raw()[key]; | ||
} else { | ||
h[key] = value; | ||
} | ||
}); | ||
|
||
return { | ||
headers: h, | ||
multiValueHeaders: m | ||
}; | ||
} |
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,54 @@ | ||
import '../src/shims.js'; | ||
import { test } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
import { split_headers } from '../src/headers.js'; | ||
|
||
test('empty headers', () => { | ||
const headers = new Headers(); | ||
|
||
const result = split_headers(headers); | ||
|
||
assert.equal(result, { | ||
headers: {}, | ||
multiValueHeaders: {} | ||
}); | ||
}); | ||
|
||
test('single-value headers', () => { | ||
const headers = new Headers(); | ||
headers.append('Location', '/apple'); | ||
headers.append('Content-Type', 'application/json'); | ||
|
||
const result = split_headers(headers); | ||
|
||
assert.equal(result, { | ||
headers: { | ||
// Note: becomes lowercase even if specified as uppercase | ||
location: '/apple', | ||
'content-type': 'application/json' | ||
}, | ||
multiValueHeaders: {} | ||
}); | ||
}); | ||
|
||
test('multi-value headers', () => { | ||
// https://httpwg.org/specs/rfc7231.html#http.date | ||
const wednesday = 'Wed, 23 Feb 2022 21:01:48 GMT'; | ||
const thursday = 'Thu, 24 Feb 2022 21:01:48 GMT'; | ||
|
||
const headers = new Headers(); | ||
headers.append('Set-Cookie', `flavor=sugar; Expires=${wednesday}`); | ||
headers.append('Set-Cookie', `diameter=6cm; Expires=${thursday}`); | ||
|
||
const result = split_headers(headers); | ||
|
||
// it splits at actual cookie boundaries, not the commas in the dates | ||
assert.equal(result, { | ||
headers: {}, | ||
multiValueHeaders: { | ||
'set-cookie': [`flavor=sugar; Expires=${wednesday}`, `diameter=6cm; Expires=${thursday}`] | ||
} | ||
}); | ||
}); | ||
|
||
test.run(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.