Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
altano committed Jan 21, 2025
1 parent 5bbcc96 commit 52eec79
Show file tree
Hide file tree
Showing 14 changed files with 237 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'astro/config';

export default defineConfig({
output: "static",
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/middleware-non-html-ssg",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineMiddleware, sequence } from 'astro:middleware';
import { promises as fs } from 'node:fs';

const first = defineMiddleware(async (context, next) => {
if (context.request.url.includes('/placeholder.png')) {
const imgURL = new URL('../../non-html-pages/src/images/placeholder.png', import.meta.url);
const buffer = await fs.readFile(imgURL);
return new Response(buffer.buffer, {
headers: {
"Content-Type": "image/png",
"Content-Disposition": `inline; filename="placeholder.png"`,
},
});
} else if (context.request.url.includes('/rename-me.json')) {
const content = JSON.stringify({name: "alan"})
return new Response(content, {
headers: {
"Content-Type": "application/json",
"Content-Disposition": `inline; filename="data.json"`,
},
});
}

return next();
});

export const onRequest = sequence(first);
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'astro/config';
import node from "@astrojs/node";

export default defineConfig({
output: "server",
adapter: node({
mode: "standalone"
})
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/middleware-non-html-ssr",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "^8.3.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineMiddleware, sequence } from 'astro:middleware';
import { promises as fs } from 'node:fs';

const first = defineMiddleware(async (context, next) => {
if (context.request.url.includes('/placeholder.png')) {
const buffer = await fs.readFile('./test/fixtures/non-html-pages/src/images/placeholder.png');
return new Response(buffer.buffer, {
headers: {
"Content-Type": "image/png",
"Content-Disposition": `inline; filename="placeholder.png"`,
},
});
} else if (context.request.url.includes('/rename-me.json')) {
const content = JSON.stringify({name: "alan"})
return new Response(content, {
headers: {
"Content-Type": "application/json",
"Content-Disposition": `inline; filename="data.json"`,
},
});
}

return next();
});

export const onRequest = sequence(
first
);
Empty file.
Empty file.
1 change: 1 addition & 0 deletions packages/astro/test/non-html-pages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { before, describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';

describe('Non-HTML Pages', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ process.env.ASTRO_TELEMETRY_DISABLED = true;
* @typedef {import('../src/core/dev/dev').DevServer} DevServer
* @typedef {import('../src/types/public/config.js').AstroInlineConfig & { root?: string | URL }} AstroInlineConfig
* @typedef {import('../src/types/public/config.js').AstroConfig} AstroConfig
* @typedef {import('../src/types/public/config.js').RouteData} RouteData
* @typedef {import('../src/types/public/config.js').RouteOptions} RouteOptions
* @typedef {import('../src/core/preview/index').PreviewServer} PreviewServer
* @typedef {import('../src/core/app/index').App} App
* @typedef {import('../src/core/app/types').SerializedSSRManifest} SerializedSSRManifest
* @typedef {import('../src/types/public/integrations').IntegrationResolvedRoute} IntegrationResolvedRoute
* @typedef {import('../src/cli/check/index').AstroChecker} AstroChecker
* @typedef {import('../src/cli/check/index').CheckPayload} CheckPayload
* @typedef {import('http').IncomingMessage} NodeRequest
Expand Down
79 changes: 79 additions & 0 deletions packages/astro/test/units/middleware-non-html.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { loadFixture } from '../test-utils.js';

describe('Middleware returning non-html, SSR', () => {
/** @type {import('../test-utils').Fixture} */
let fixture;
/** @type {import('../test-utils').DevServer} */
let devServer;

before(async () => {
fixture = await loadFixture({
root: './fixtures/middleware-non-html-ssr/',
});
// do an ssr build
await fixture.build();
// start the dev server
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});

it('should return placeholder.png with a custom content type', async () => {
const response = await fixture.fetch('/placeholder.png');
assert.equal(response.headers.get('Content-Type'), 'image/png');
assert.equal(response.headers.get('Content-Disposition'), 'inline; filename="placeholder.png"');

const imageBase64 = await response
.arrayBuffer()
.then((arrayBuffer) => Buffer.from(arrayBuffer).toString('base64'));

// Make sure the whole buffer (in base64) matches this snapshot
assert.equal(
imageBase64,
'iVBORw0KGgoAAAANSUhEUgAAAGQAAACWCAMAAAAfZt10AAAABlBMVEXd3d3+/v7B/CFgAAAA3UlEQVR42u3ZMQ7DIBQFQeb+l06bNgUbG/5eYApLFjzWNE3TNE3TNE035av9AhAQEBBQGAQEFAaFQWFQGBQGhUGCKAwKgwQpDJ6JECgCRYIEikH8YAyCRyEGyRCDvBWRIPNNBpm/8G6kUM45EhXKlQfuFSHFpbFH+jt2j/S7xwqUYvBaCRIozZy6X2km7v1K8uwQIIWBwkBAQEBg3Tyj3z4LnzRBKgwKg8KgMEgQhaEwSBCFQWBEiMIgQQqDBCkMEqQw+APixYgcsa0TERs7D/F6xGmIAxCD/Iw4AvEB92Ec3ZAPdlMAAAAASUVORK5CYII=',
);
});

it('should return rename-me.json renamed to data.json', async () => {
const response = await fixture.fetch('/rename-me.json');
assert.equal(response.status, 200);
assert.equal(response.headers.get('Content-Type'), 'application/json');
assert.equal(response.headers.get('Content-Disposition'), 'inline; filename="data.json"');

const data = await response.json();
assert.equal(data.name, 'alan');
});
});

describe('Middleware returning non-html, SSG', () => {
/** @type {import('../test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/middleware-non-html-ssg/',
});
// do an ssg build
await fixture.build();
});

it('should have built placeholder.png with a custom content type', async () => {
const imageText = await fixture.readFile('/placeholder.png', 'base64');

// Make sure the whole buffer (in base64) matches this snapshot
assert.equal(
imageText,
'iVBORw0KGgoAAAANSUhEUgAAAGQAAACWCAMAAAAfZt10AAAABlBMVEXd3d3+/v7B/CFgAAAA3UlEQVR42u3ZMQ7DIBQFQeb+l06bNgUbG/5eYApLFjzWNE3TNE3TNE035av9AhAQEBBQGAQEFAaFQWFQGBQGhUGCKAwKgwQpDJ6JECgCRYIEikH8YAyCRyEGyRCDvBWRIPNNBpm/8G6kUM45EhXKlQfuFSHFpbFH+jt2j/S7xwqUYvBaCRIozZy6X2km7v1K8uwQIIWBwkBAQEBg3Tyj3z4LnzRBKgwKg8KgMEgQhaEwSBCFQWBEiMIgQQqDBCkMEqQw+APixYgcsa0TERs7D/F6xGmIAxCD/Iw4AvEB92Ec3ZAPdlMAAAAASUVORK5CYII=',
);
});

it('should have built data.json with a custom name (rename-me.json)', async () => {
const file = await fixture.readFile('/data.json');
const data = JSON.parse(file);
assert.equal(data.name, 'alan');
});
});
67 changes: 67 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 52eec79

Please sign in to comment.