From 153c0b5f0a606be7979f506636d9b0d438e24aa6 Mon Sep 17 00:00:00 2001 From: qixuan <58852732+GiveMe-A-Name@users.noreply.github.com> Date: Mon, 20 May 2024 11:52:23 +0800 Subject: [PATCH] fix: new middleware integration test (#5755) --- pnpm-lock.yaml | 3 -- .../server-hook/new-middleware/package.json | 1 - .../new-middleware/server/index.ts | 10 +------ .../new-middleware/tests/index.test.ts | 28 +++++++++++++++---- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 94a09c999617..492020151e5b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6793,9 +6793,6 @@ importers: '@modern-js/runtime': specifier: workspace:* version: link:../../../../packages/runtime/plugin-runtime - axios: - specifier: ^1.6.0 - version: 1.6.7 react: specifier: ^18 version: 18.2.0 diff --git a/tests/integration/server-hook/new-middleware/package.json b/tests/integration/server-hook/new-middleware/package.json index 2547bb96d8ed..2181e35a5b86 100644 --- a/tests/integration/server-hook/new-middleware/package.json +++ b/tests/integration/server-hook/new-middleware/package.json @@ -13,7 +13,6 @@ }, "dependencies": { "@modern-js/runtime": "workspace:*", - "axios": "^1.6.0", "react": "^18", "react-dom": "^18" }, diff --git a/tests/integration/server-hook/new-middleware/server/index.ts b/tests/integration/server-hook/new-middleware/server/index.ts index 4b6edf2e61de..69829e67e63c 100644 --- a/tests/integration/server-hook/new-middleware/server/index.ts +++ b/tests/integration/server-hook/new-middleware/server/index.ts @@ -21,13 +21,6 @@ function parseQuery(request: Request): URLSearchParams { return url.searchParams; } -function getPathname(request: Request): string { - // eslint-disable-next-line node/no-unsupported-features/node-builtins, node/prefer-global/url - const url = new URL(request.url); - - return url.pathname; -} - function auth(): UnstableMiddleware { function getUserInfo(req: Request) { const query = parseQuery(req); @@ -42,8 +35,7 @@ function auth(): UnstableMiddleware { // eslint-disable-next-line consistent-return return async (c, next) => { - const pathname = getPathname(c.request); - if (pathname.startsWith('/login')) { + if (c.request.url.includes('/login')) { return next(); } diff --git a/tests/integration/server-hook/new-middleware/tests/index.test.ts b/tests/integration/server-hook/new-middleware/tests/index.test.ts index 4e1a47367bd5..00f9276d879b 100644 --- a/tests/integration/server-hook/new-middleware/tests/index.test.ts +++ b/tests/integration/server-hook/new-middleware/tests/index.test.ts @@ -1,15 +1,24 @@ import path from 'path'; -import axios from 'axios'; -import { launchApp, getPort, killApp } from '../../../../utils/modernTestUtils'; +import puppeteer, { Browser, Page } from 'puppeteer'; +import { + launchApp, + getPort, + killApp, + launchOptions, +} from '../../../../utils/modernTestUtils'; const appPath = path.resolve(__dirname, '../'); describe('test new middleware run correctly', () => { let app: any; let port: number; + let page: Page; + let browser: Browser; beforeAll(async () => { jest.setTimeout(1000 * 60 * 2); + browser = await puppeteer.launch(launchOptions as any); + page = await browser.newPage(); port = await getPort(); app = await launchApp(appPath, port); @@ -19,13 +28,16 @@ describe('test new middleware run correctly', () => { if (app) { await killApp(app); } + await page.close(); + await browser.close(); }); test('should request "/" correctly', async () => { const url = `http://localhost:${port}`; - const res = await axios.get(url); + const res = await page.goto(url); - const { headers, data: body } = res; + const headers = res?.headers(); + const body = await res?.text(); expect(body).toMatch('Liming'); @@ -38,9 +50,13 @@ describe('test new middleware run correctly', () => { test('should redirect corretly', async () => { const url = `http://localhost:${port}/?unlogin=1`; - const res = await axios.get(url); + const res = await page.goto(url); - const { data: body, headers } = res; + const body = await res?.text(); + const headers = res?.headers(); + const chain = res?.request().redirectChain(); + + expect(chain?.length).toBe(1); expect(body).toMatch('Login');