0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/node/test/api-route.test.js
Emanuele Stoppa fc21a3c306
chore(@astrojs/node): use Node.js for testing (#9758)
* chore(@astrojs/node): use Node.js for testing

* revert file

* address feedback

* feedback

* Run tests in a single process (#9823)

* Run tests in a single process

* Make test less flaky

* chore: remove module

---------

Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
2024-01-25 16:17:31 +00:00

112 lines
2.8 KiB
JavaScript

import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import crypto from 'node:crypto';
import { describe, it, before } from 'node:test';
import * as assert from 'node:assert/strict';
describe('API routes', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/api-route/',
output: 'server',
adapter: nodejs({ mode: 'middleware' }),
});
await fixture.build();
});
it('Can get the request body', async () => {
const { handler } = await import('./fixtures/api-route/dist/server/entry.mjs');
let { req, res, done } = createRequestAndResponse({
method: 'POST',
url: '/recipes',
});
req.once('async_iterator', () => {
req.send(JSON.stringify({ id: 2 }));
});
handler(req, res);
let [buffer] = await done;
let json = JSON.parse(buffer.toString('utf-8'));
assert.equal(json.length, 1);
assert.equal(json[0].name, 'Broccoli Soup');
});
it('Can get binary data', async () => {
const { handler } = await import('./fixtures/api-route/dist/server/entry.mjs');
let { req, res, done } = createRequestAndResponse({
method: 'POST',
url: '/binary',
});
req.once('async_iterator', () => {
req.send(Buffer.from(new Uint8Array([1, 2, 3, 4, 5])));
});
handler(req, res);
let [out] = await done;
let arr = Array.from(new Uint8Array(out.buffer));
assert.deepEqual(arr, [5, 4, 3, 2, 1]);
});
it('Can post large binary data', async () => {
const { handler } = await import('./fixtures/api-route/dist/server/entry.mjs');
let { req, res, done } = createRequestAndResponse({
method: 'POST',
url: '/hash',
});
handler(req, res);
let expectedDigest = null;
req.once('async_iterator', () => {
// Send 256MB of garbage data in 256KB chunks. This should be fast (< 1sec).
let remainingBytes = 256 * 1024 * 1024;
const chunkSize = 256 * 1024;
const hash = crypto.createHash('sha256');
while (remainingBytes > 0) {
const size = Math.min(remainingBytes, chunkSize);
const chunk = Buffer.alloc(size, Math.floor(Math.random() * 256));
hash.update(chunk);
req.emit('data', chunk);
remainingBytes -= size;
}
req.emit('end');
expectedDigest = hash.digest();
});
let [out] = await done;
assert.deepEqual(new Uint8Array(out.buffer), new Uint8Array(expectedDigest));
});
it('Can bail on streaming', async () => {
const { handler } = await import('./fixtures/api-route/dist/server/entry.mjs');
let { req, res, done } = createRequestAndResponse({
url: '/streaming',
});
let locals = { cancelledByTheServer: false };
handler(req, res, () => {}, locals);
req.send();
await new Promise((resolve) => setTimeout(resolve, 500));
res.emit('close');
await done;
assert.deepEqual(locals, { cancelledByTheServer: true });
});
});