Skip to content
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
108 changes: 51 additions & 57 deletions packages/integrations/netlify/test/functions/cookies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,58 @@ import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { loadFixture } from '../../../../astro/test/test-utils.js';

describe(
'Cookies',
() => {
let fixture;
describe('Cookies', { timeout: 120000 }, () => {
let fixture;

before(async () => {
fixture = await loadFixture({ root: new URL('./fixtures/cookies/', import.meta.url) });
await fixture.build();
});
before(async () => {
fixture = await loadFixture({ root: new URL('./fixtures/cookies/', import.meta.url) });
await fixture.build();
});

it('Can set multiple', async () => {
const entryURL = new URL(
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const resp = await handler(
new Request('http://example.com/login', { method: 'POST', body: '{}' }),
{},
);
assert.equal(resp.status, 301);
assert.equal(resp.headers.get('location'), '/');
assert.deepEqual(resp.headers.getSetCookie(), ['foo=foo; HttpOnly', 'bar=bar; HttpOnly']);
});
it('Can set multiple', async () => {
const entryURL = new URL(
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const resp = await handler(
new Request('http://example.com/login', { method: 'POST', body: '{}' }),
{},
);
assert.equal(resp.status, 301);
assert.equal(resp.headers.get('location'), '/');
assert.deepEqual(resp.headers.getSetCookie(), ['foo=foo; HttpOnly', 'bar=bar; HttpOnly']);
});

it('Can set partitioned cookie', async () => {
const entryURL = new URL(
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const resp = await handler(new Request('http://example.com/partitioned'), {});
assert.equal(resp.status, 200);
const cookie = resp.headers.getSetCookie()[0];
assert.ok(cookie.includes('Partitioned'), 'Cookie should include Partitioned attribute');
});
it('Can set partitioned cookie', async () => {
const entryURL = new URL(
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const resp = await handler(new Request('http://example.com/partitioned'), {});
assert.equal(resp.status, 200);
const cookie = resp.headers.getSetCookie()[0];
assert.ok(cookie.includes('Partitioned'), 'Cookie should include Partitioned attribute');
});

it('renders dynamic 404 page', async () => {
const entryURL = new URL(
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const resp = await handler(
new Request('http://example.com/nonexistant-page', {
headers: {
'x-test': 'bar',
},
}),
{},
);
assert.equal(resp.status, 404);
const text = await resp.text();
assert.equal(text.includes('This is my custom 404 page'), true);
assert.equal(text.includes('x-test: bar'), true);
});
},
{
timeout: 120000,
},
);
it('renders dynamic 404 page', async () => {
const entryURL = new URL(
'./fixtures/cookies/.netlify/v1/functions/ssr/ssr.mjs',
import.meta.url,
);
const { default: handler } = await import(entryURL);
const resp = await handler(
new Request('http://example.com/nonexistant-page', {
headers: {
'x-test': 'bar',
},
}),
{},
);
assert.equal(resp.status, 404);
const text = await resp.text();
assert.equal(text.includes('This is my custom 404 page'), true);
assert.equal(text.includes('x-test: bar'), true);
});
});
102 changes: 48 additions & 54 deletions packages/integrations/netlify/test/functions/edge-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,59 @@ import * as assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { loadFixture } from '../../../../astro/test/test-utils.js';

describe(
'Middleware',
() => {
const root = new URL('./fixtures/middleware/', import.meta.url);

describe('middlewareMode: classic', () => {
let fixture;
before(async () => {
process.env.EDGE_MIDDLEWARE = 'false';
fixture = await loadFixture({ root });
await fixture.build();
});

it('emits no edge function', async () => {
assert.equal(
fixture.pathExists('../.netlify/v1/edge-functions/middleware/middleware.mjs'),
false,
);
});
describe('Middleware', { timeout: 120000 }, () => {
const root = new URL('./fixtures/middleware/', import.meta.url);

describe('middlewareMode: classic', () => {
let fixture;
before(async () => {
process.env.EDGE_MIDDLEWARE = 'false';
fixture = await loadFixture({ root });
await fixture.build();
});

it('applies middleware to static files at build-time', async () => {
// prerendered page has middleware applied at build time
const prerenderedPage = await fixture.readFile('prerender/index.html');
assert.equal(prerenderedPage.includes('<title>Middleware</title>'), true);
});
it('emits no edge function', async () => {
assert.equal(
fixture.pathExists('../.netlify/v1/edge-functions/middleware/middleware.mjs'),
false,
);
});

after(async () => {
process.env.EDGE_MIDDLEWARE = undefined;
await fixture.clean();
});
it('applies middleware to static files at build-time', async () => {
// prerendered page has middleware applied at build time
const prerenderedPage = await fixture.readFile('prerender/index.html');
assert.equal(prerenderedPage.includes('<title>Middleware</title>'), true);
});

describe('middlewareMode: edge', () => {
let fixture;
before(async () => {
process.env.EDGE_MIDDLEWARE = 'true';
fixture = await loadFixture({ root });
await fixture.build();
});
after(async () => {
process.env.EDGE_MIDDLEWARE = undefined;
await fixture.clean();
});
});

describe('middlewareMode: edge', () => {
let fixture;
before(async () => {
process.env.EDGE_MIDDLEWARE = 'true';
fixture = await loadFixture({ root });
await fixture.build();
});

it('emits an edge function', async () => {
const contents = await fixture.readFile(
'../.netlify/v1/edge-functions/middleware/middleware.mjs',
);
assert.equal(contents.includes('"Hello world"'), false);
});
it('emits an edge function', async () => {
const contents = await fixture.readFile(
'../.netlify/v1/edge-functions/middleware/middleware.mjs',
);
assert.equal(contents.includes('"Hello world"'), false);
});

it.skip('does not apply middleware during prerendering', async () => {
const prerenderedPage = await fixture.readFile('prerender/index.html');
assert.equal(prerenderedPage.includes('<title></title>'), true);
});
it.skip('does not apply middleware during prerendering', async () => {
const prerenderedPage = await fixture.readFile('prerender/index.html');
assert.equal(prerenderedPage.includes('<title></title>'), true);
});

after(async () => {
process.env.EDGE_MIDDLEWARE = undefined;
await fixture.clean();
});
after(async () => {
process.env.EDGE_MIDDLEWARE = undefined;
await fixture.clean();
});
},
{
timeout: 120000,
},
);
});
});
Loading
Loading