-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
packages/astro/test/fixtures/middleware-non-html-ssg/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
output: "static", | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/middleware-non-html-ssg/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:*" | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/astro/test/fixtures/middleware-non-html-ssg/src/middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
9 changes: 9 additions & 0 deletions
9
packages/astro/test/fixtures/middleware-non-html-ssr/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}) | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/astro/test/fixtures/middleware-non-html-ssr/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/astro/test/fixtures/middleware-non-html-ssr/src/middleware.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.