Skip to content
Closed
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
9 changes: 5 additions & 4 deletions packages/integrations/netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"test": "pnpm run test-fn && pnpm run test-static && pnpm run test:dev",
"test-fn": "astro-scripts test \"test/functions/*.test.js\"",
"test:dev": "astro-scripts test \"test/development/*.test.js\"",
"test-static": "astro-scripts test \"test/static/*.test.js\"",
"test:hosted": "astro-scripts test \"test/hosted/*.test.js\""
"test-fn": "astro-scripts test \"test/functions/*.test.ts\"",
"test:dev": "astro-scripts test \"test/development/*.test.ts\"",
"test-static": "astro-scripts test \"test/static/*.test.ts\"",
"test:hosted": "astro-scripts test \"test/hosted/*.test.js\"",
"typecheck:tests": "tsc --build tsconfig.test.json"
},
"dependencies": {
"@astrojs/internal-helpers": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import assert from 'node:assert/strict';
import { after, afterEach, before, describe, it } from 'node:test';

import * as cheerio from 'cheerio';
import { loadFixture } from '../../../../astro/test/test-utils.js';
import { type DevServer, type Fixture, loadFixture } from '../../../../astro/test/test-utils.js';
import netlifyAdapter from '../../dist/index.js';

describe('Netlify primitives', () => {
describe('Development', () => {
/** @type {import('../../../../astro/test/test-utils').Fixture} */
let fixture;
let devServer;
let fixture: Fixture;
let devServer: DevServer;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/primitives/', import.meta.url),
Expand Down Expand Up @@ -65,9 +64,11 @@ describe('Netlify primitives', () => {
it('loads images in development', async () => {
const imgResponse = await fixture.fetch('/astronaut');
const $img = cheerio.load(await imgResponse.text());
const images = $img('img').map((_i, el) => {
return $img(el).attr('src');
});
const images = $img('img')
.map((_i, el) => {
return $img(el).attr('src');
})
.toArray();

for (const imgSrc of images) {
assert(imgSrc.startsWith('/.netlify/images'));
Expand All @@ -89,9 +90,11 @@ describe('Netlify primitives', () => {
try {
const imgResponse = await cdnDisabledFixture.fetch('/astronaut');
const $img = cheerio.load(await imgResponse.text());
const images = $img('img').map((_i, el) => {
return $img(el).attr('src');
});
const images = $img('img')
.map((_i, el) => {
return $img(el).attr('src');
})
.toArray();

for (const imgSrc of images) {
assert(
Expand All @@ -101,7 +104,7 @@ describe('Netlify primitives', () => {
}
} finally {
await cdnDisabledServer.stop();
process.env.DISABLE_IMAGE_CDN = undefined;
delete process.env.DISABLE_IMAGE_CDN;
}
});
});
Expand Down
65 changes: 0 additions & 65 deletions packages/integrations/netlify/test/functions/cookies.test.js

This file was deleted.

59 changes: 59 additions & 0 deletions packages/integrations/netlify/test/functions/cookies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { type Fixture, loadFixture } from '../../../../astro/test/test-utils.js';

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

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.href);
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.href);
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.href);
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);
});
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { type Fixture, loadFixture } from '../../../../astro/test/test-utils.js';

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

describe('middlewareMode: classic', () => {
let fixture: 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,
);
});

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);
});

after(async () => {
delete process.env.EDGE_MIDDLEWARE;
await fixture.clean();
});
});

describe('middlewareMode: edge', () => {
let fixture: 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.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 () => {
delete process.env.EDGE_MIDDLEWARE;
await fixture.clean();
});
});
});
Loading
Loading