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

Unit Tests for Netlify Adapter split_headers function #4108

Merged
merged 3 commits into from
Feb 24, 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
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.