mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
--wip-- [skip ci]
This commit is contained in:
parent
2ee8412bbc
commit
1dad448bef
8 changed files with 106 additions and 0 deletions
|
@ -0,0 +1,8 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
import nodejs from '@astrojs/node';
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
output: 'server',
|
||||
adapter: nodejs({ mode: 'standalone' }),
|
||||
});
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"name": "@e2e/server-client-address",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "astro dev"
|
||||
},
|
||||
"dependencies": {
|
||||
"astro": "workspace:*",
|
||||
"@astrojs/node": "^9.0.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
import { defineAction } from 'astro:actions';
|
||||
|
||||
export const server = {
|
||||
address: defineAction({
|
||||
accept: 'form',
|
||||
handler: async (_, ctx) => {
|
||||
console.log('Actions executed');
|
||||
return ctx.clientAddress;
|
||||
}
|
||||
})
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
export const onRequest = (ctx, next) => {
|
||||
console.log(ctx.clientAddress);
|
||||
|
||||
return next();
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
export const GET = (ctx) => {
|
||||
return Response.json({
|
||||
clientAddress: ctx.clientAddress !== undefined
|
||||
});
|
||||
}
|
||||
|
||||
export const POST = (ctx) => {
|
||||
return Response.json({
|
||||
clientAddress: ctx.clientAddress !== undefined
|
||||
});
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
const address = Astro.clientAddress;
|
||||
---
|
||||
|
||||
<div id="address">{address !== undefined}</div>
|
50
packages/astro/e2e/server-client-address.test.js
Normal file
50
packages/astro/e2e/server-client-address.test.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
import { expect } from '@playwright/test';
|
||||
import { testFactory } from './test-utils.js';
|
||||
|
||||
const test = testFactory(import.meta.url, {
|
||||
root: './fixtures/server-client-address/',
|
||||
});
|
||||
|
||||
test.describe('Server access to client address', () => {
|
||||
test.describe('Development', () => {
|
||||
let devServer;
|
||||
|
||||
test.beforeAll(async ({ astro }) => {
|
||||
devServer = await astro.startDevServer({
|
||||
logLevel: 'debug',
|
||||
vite: {
|
||||
logLevel: 'debug',
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test.afterAll(async () => {
|
||||
await devServer.stop();
|
||||
});
|
||||
|
||||
test('Load a page using client address', async ({ page, astro }) => {
|
||||
await page.goto(astro.resolveUrl('/'));
|
||||
let el = page.locator('div#address');
|
||||
await el.click();
|
||||
|
||||
await expect(el, 'element rendered').toBeVisible();
|
||||
await expect(el, 'should have content').toHaveText('true');
|
||||
});
|
||||
|
||||
test('Load an endpoint using client address', async ({ page, astro }) => {
|
||||
await page.goto(astro.resolveUrl('/endpoint'));
|
||||
const res = await astro.fetch('/endpoint');
|
||||
const content = await res.json();
|
||||
|
||||
expect(content).toEqual({ clientAddress: true });
|
||||
});
|
||||
|
||||
test('Load a POST endpoint using client address', async ({ page, astro }) => {
|
||||
await page.goto(astro.resolveUrl('/endpoint'));
|
||||
const res = await astro.fetch('/endpoint', {method: 'POST'});
|
||||
const content = await res.json();
|
||||
|
||||
expect(content).toEqual({ clientAddress: true });
|
||||
});
|
||||
});
|
||||
});
|
|
@ -62,6 +62,10 @@ export function createRequest({
|
|||
url.search = '';
|
||||
}
|
||||
|
||||
console.log({
|
||||
isPrerendered, body,
|
||||
});
|
||||
|
||||
const request = new Request(url, {
|
||||
method: method,
|
||||
headers: headersObj,
|
||||
|
|
Loading…
Reference in a new issue