Skip to content

Commit

Permalink
await the result of handleRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Sep 14, 2022
1 parent 2d8362a commit a9ca414
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 121 deletions.
6 changes: 2 additions & 4 deletions packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ async function handleRequest(
);
filePath = matchedRoute?.filePath;

return handleRoute(
return await handleRoute(
matchedRoute,
url,
pathname,
Expand All @@ -306,7 +306,7 @@ async function handleRequest(
config,
req,
res
)
);
} catch(_err) {
const err = fixViteErrorMessage(_err, viteServer, filePath);
const errorWithMetadata = collectErrorMetadata(err);
Expand All @@ -329,8 +329,6 @@ async function handleRoute(
req: http.IncomingMessage,
res: http.ServerResponse
): Promise<void> {


if (!matchedRoute) {
return handle404Response(origin, config, req, res);
}
Expand Down
98 changes: 32 additions & 66 deletions packages/astro/test/api-routes.test.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,47 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';
import * as fs from 'fs';

import { FormData, File } from 'node-fetch'
import { FormData, File } from 'node-fetch';

describe('API routes', () => {
describe('Development - SSR', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
fixture = await loadFixture({
output: 'server',
root: './fixtures/api-routes/'
});
devServer = await fixture.startDevServer();
});

after(async () => {
await devServer.stop();
});
/** @type {import('./test-utils').Fixture} */
let fixture;

it('Can be passed binary data from multipart formdata', async () => {
const formData = new FormData();
const raw = await fs.promises.readFile(new URL('./fixtures/api-routes/src/images/penguin.jpg', import.meta.url));
const file = new File([raw], 'penguin.jpg', { type: 'text/jpg' });
formData.set('file', file, 'penguin.jpg');
before(async () => {
fixture = await loadFixture({ root: './fixtures/api-routes/' });
await fixture.build();
});

const res = await fixture.fetch('/binary', {
method: 'POST',
body: formData
describe('Deprecated API', () => {
it('two argument supported', async () => {
const one = JSON.parse(await fixture.readFile('/old-api/twoarg/one.json'));
expect(one).to.deep.equal({
param: 'one',
pathname: '/old-api/twoarg/one.json',
});
const two = JSON.parse(await fixture.readFile('/old-api/twoarg/two.json'));
expect(two).to.deep.equal({
param: 'two',
pathname: '/old-api/twoarg/two.json',
});

expect(res.status).to.equal(200);
});
});

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

before(async () => {
fixture = await loadFixture({ root: './fixtures/api-routes/' });
await fixture.build();
});

describe('Deprecated API', () => {
it('two argument supported', async () => {
const one = JSON.parse(await fixture.readFile('/old-api/twoarg/one.json'));
expect(one).to.deep.equal({
param: 'one',
pathname: '/old-api/twoarg/one.json',
});
const two = JSON.parse(await fixture.readFile('/old-api/twoarg/two.json'));
expect(two).to.deep.equal({
param: 'two',
pathname: '/old-api/twoarg/two.json',
});
});

it('param first argument is supported', async () => {
const one = JSON.parse(await fixture.readFile('/old-api/onearg/one.json'));
expect(one).to.deep.equal({
param: 'one',
});
it('param first argument is supported', async () => {
const one = JSON.parse(await fixture.readFile('/old-api/onearg/one.json'));
expect(one).to.deep.equal({
param: 'one',
});
});

describe('1.0 API', () => {
it('Receives a context argument', async () => {
const one = JSON.parse(await fixture.readFile('/context/data/one.json'));
expect(one).to.deep.equal({
param: 'one',
pathname: '/context/data/one.json',
});
});

describe('1.0 API', () => {
it('Receives a context argument', async () => {
const one = JSON.parse(await fixture.readFile('/context/data/one.json'));
expect(one).to.deep.equal({
param: 'one',
pathname: '/context/data/one.json',
});
});
})
});
});
Binary file not shown.
32 changes: 0 additions & 32 deletions packages/astro/test/fixtures/api-routes/src/pages/binary.js

This file was deleted.

19 changes: 0 additions & 19 deletions packages/astro/test/fixtures/api-routes/src/pages/index.astro

This file was deleted.

0 comments on commit a9ca414

Please sign in to comment.