-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes Node adapter receiving a request body
- Loading branch information
Showing
9 changed files
with
274 additions
and
16 deletions.
There are no files selected for viewing
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,6 @@ | ||
--- | ||
'astro': patch | ||
'@astrojs/node': patch | ||
--- | ||
|
||
Fixes Node adapter to accept a request body |
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
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,37 @@ | ||
import nodejs from '../dist/index.js'; | ||
import { loadFixture, createRequestAndResponse, toPromise } from './test-utils.js'; | ||
import { expect } from 'chai'; | ||
|
||
|
||
describe('API routes', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/api-route/', | ||
experimental: { | ||
ssr: true, | ||
}, | ||
adapter: nodejs(), | ||
}); | ||
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' | ||
}); | ||
|
||
handler(req, res); | ||
req.send(JSON.stringify({ id: 2 })); | ||
|
||
let [ buffer ] = await done; | ||
let json = JSON.parse(buffer.toString('utf-8')); | ||
expect(json.length).to.equal(1); | ||
expect(json[0].name).to.equal('Broccoli Soup'); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/integrations/node/test/fixtures/api-route/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/nodejs-api-route", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*", | ||
"@astrojs/node": "workspace:*" | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/integrations/node/test/fixtures/api-route/src/pages/recipes.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,24 @@ | ||
|
||
export async function post({ request }) { | ||
let body = await request.json(); | ||
const recipes = [ | ||
{ | ||
id: 1, | ||
name: 'Potato Soup' | ||
}, | ||
{ | ||
id: 2, | ||
name: 'Broccoli Soup' | ||
} | ||
]; | ||
|
||
let out = recipes.filter(r => { | ||
return r.id === body.id; | ||
}); | ||
|
||
return new Response(JSON.stringify(out), { | ||
headers: { | ||
'Content-Type': 'application/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,41 @@ | ||
import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js'; | ||
import httpMocks from 'node-mocks-http'; | ||
import { EventEmitter } from 'events'; | ||
|
||
/** | ||
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture | ||
*/ | ||
|
||
export function loadFixture(inlineConfig) { | ||
if (!inlineConfig || !inlineConfig.root) | ||
throw new Error("Must provide { root: './fixtures/...' }"); | ||
|
||
// resolve the relative root (i.e. "./fixtures/tailwindcss") to a full filepath | ||
// without this, the main `loadFixture` helper will resolve relative to `packages/astro/test` | ||
return baseLoadFixture({ | ||
...inlineConfig, | ||
root: new URL(inlineConfig.root, import.meta.url).toString(), | ||
}); | ||
} | ||
|
||
export function createRequestAndResponse(reqOptions) { | ||
let req = httpMocks.createRequest(reqOptions); | ||
|
||
let res = httpMocks.createResponse({ | ||
eventEmitter: EventEmitter, | ||
req | ||
}); | ||
|
||
let done = toPromise(res); | ||
|
||
return { req, res, done }; | ||
} | ||
|
||
export function toPromise(res) { | ||
return new Promise(resolve => { | ||
res.on('end', () => { | ||
let chunks = res._getChunks(); | ||
resolve(chunks); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.