Skip to content

Commit

Permalink
Unit Tests for Netlify Adapter split_headers function (#4108)
Browse files Browse the repository at this point in the history
* 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
Auroratide and Rich-Harris authored Feb 24, 2022
1 parent ae6121e commit 34bd4f5
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 31 deletions.
4 changes: 3 additions & 1 deletion packages/adapter-netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"scripts": {
"dev": "rimraf files && rollup -cw",
"build": "rimraf files && rollup -c",
"test": "uvu src \"(spec\\.js|test[\\\\/]index\\.js)\"",
"lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format",
"format": "npm run check-format -- --write",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
Expand All @@ -41,6 +42,7 @@
"@rollup/plugin-node-resolve": "^13.0.5",
"@sveltejs/kit": "workspace:*",
"rimraf": "^3.0.2",
"rollup": "^2.58.0"
"rollup": "^2.58.0",
"uvu": "^0.5.2"
}
}
31 changes: 1 addition & 30 deletions packages/adapter-netlify/src/handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import './shims';
import { Server } from '0SERVER';
import { split_headers } from './headers';

/**
* @param {import('@sveltejs/kit').SSRManifest} manifest
Expand Down Expand Up @@ -55,33 +56,3 @@ function to_request(event) {

return new Request(rawUrl, init);
}

/**
* Splits headers into two categories: single value and multi value
* @param {Headers} headers
* @returns {{
* headers: Record<string, string>,
* multiValueHeaders: Record<string, string[]>
* }}
*/
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
};
}
29 changes: 29 additions & 0 deletions packages/adapter-netlify/src/headers.js
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
};
}
54 changes: 54 additions & 0 deletions packages/adapter-netlify/src/headers.spec.js
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();
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34bd4f5

Please sign in to comment.