Skip to content

Commit

Permalink
fix: new middleware integration test (#5755)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiveMe-A-Name authored May 20, 2024
1 parent e759876 commit 153c0b5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/integration/server-hook/new-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"dependencies": {
"@modern-js/runtime": "workspace:*",
"axios": "^1.6.0",
"react": "^18",
"react-dom": "^18"
},
Expand Down
10 changes: 1 addition & 9 deletions tests/integration/server-hook/new-middleware/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Var> {
function getUserInfo(req: Request) {
const query = parseQuery(req);
Expand All @@ -42,8 +35,7 @@ function auth(): UnstableMiddleware<Var> {

// 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();
}

Expand Down
28 changes: 22 additions & 6 deletions tests/integration/server-hook/new-middleware/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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');

Expand All @@ -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');

Expand Down

0 comments on commit 153c0b5

Please sign in to comment.