|
1 | | -import { closeApp, creatApp, createHttpRequest } from './utils'; |
| 1 | +import { closeApp, creatApp, createHttpRequest, createRealHttpRequest } from './utils'; |
2 | 2 | import { IMidwayKoaApplication, Framework } from '../src'; |
3 | | -import { Controller, Get, makeHttpRequest } from '@midwayjs/core'; |
| 3 | +import { Controller, Get, makeHttpRequest, sleep } from '@midwayjs/core'; |
4 | 4 | import { createLightApp } from '@midwayjs/mock'; |
5 | 5 |
|
6 | 6 | describe('/test/feature.test.ts', () => { |
@@ -274,6 +274,7 @@ describe('/test/feature.test.ts', () => { |
274 | 274 | .get('/'); |
275 | 275 | expect(result.status).toEqual(200); |
276 | 276 | expect(result.text).toEqual('123'); |
| 277 | + await closeApp(app); |
277 | 278 | }); |
278 | 279 |
|
279 | 280 | it('should test default onerror set status', async () => { |
@@ -494,7 +495,7 @@ describe('/test/feature.test.ts', () => { |
494 | 495 | } |
495 | 496 | }); |
496 | 497 |
|
497 | | - const port = (app.getFramework() as Framework).getPort(); |
| 498 | + const port = (app.getFramework() as unknown as Framework).getPort(); |
498 | 499 | expect(port).not.toBe('0'); |
499 | 500 |
|
500 | 501 | await closeApp(app); |
@@ -662,4 +663,116 @@ describe('/test/feature.test.ts', () => { |
662 | 663 | await closeApp(app); |
663 | 664 | }); |
664 | 665 | }); |
| 666 | + |
| 667 | + describe('test http listen options', () => { |
| 668 | + @Controller() |
| 669 | + class HomeController { |
| 670 | + @Get('/') |
| 671 | + async query() { |
| 672 | + return 'hello world' |
| 673 | + } |
| 674 | + } |
| 675 | + |
| 676 | + it('should test default port', async () => { |
| 677 | + const app = await createLightApp('', { |
| 678 | + imports: [ |
| 679 | + require('../src'), |
| 680 | + ], |
| 681 | + preloadModules: [ |
| 682 | + HomeController |
| 683 | + ], |
| 684 | + globalConfig: { |
| 685 | + keys: '123', |
| 686 | + koa: { |
| 687 | + port: 0, // 使用随机端口 |
| 688 | + } |
| 689 | + } |
| 690 | + }); |
| 691 | + await sleep(); |
| 692 | + |
| 693 | + // 验证端口配置 |
| 694 | + const framework = app.getFramework() as unknown as Framework; |
| 695 | + const port = framework.getPort(); |
| 696 | + expect(port).toBeDefined(); |
| 697 | + expect(port).not.toBe('0'); |
| 698 | + |
| 699 | + // 使用真正的 HTTP 请求测试 |
| 700 | + const result = await createRealHttpRequest(app as unknown as IMidwayKoaApplication, '/'); |
| 701 | + expect(result.status).toBe(200); |
| 702 | + |
| 703 | + await closeApp(app); |
| 704 | + }); |
| 705 | + |
| 706 | + it('should test listenOptions configuration', async () => { |
| 707 | + const app = await createLightApp('', { |
| 708 | + imports: [ |
| 709 | + require('../src'), |
| 710 | + ], |
| 711 | + preloadModules: [ |
| 712 | + HomeController |
| 713 | + ], |
| 714 | + globalConfig: { |
| 715 | + keys: '123', |
| 716 | + koa: { |
| 717 | + listenOptions: { |
| 718 | + port: 0, // 使用随机端口 |
| 719 | + host: '127.0.0.1' |
| 720 | + } |
| 721 | + } |
| 722 | + } |
| 723 | + }); |
| 724 | + |
| 725 | + // 等待服务器真正启动 |
| 726 | + await sleep(); |
| 727 | + |
| 728 | + // 验证端口配置 |
| 729 | + const framework = app.getFramework() as unknown as Framework; |
| 730 | + const port = framework.getPort(); |
| 731 | + expect(port).toBeDefined(); |
| 732 | + expect(port).not.toBe('0'); |
| 733 | + |
| 734 | + // 使用真正的 HTTP 请求测试 |
| 735 | + const result = await createRealHttpRequest(app as unknown as IMidwayKoaApplication, '/'); |
| 736 | + expect(result.status).toBe(200); |
| 737 | + |
| 738 | + await closeApp(app); |
| 739 | + }); |
| 740 | + |
| 741 | + it('should test listenOptions with port priority', async () => { |
| 742 | + const app = await createLightApp('', { |
| 743 | + imports: [ |
| 744 | + require('../src'), |
| 745 | + ], |
| 746 | + preloadModules: [ |
| 747 | + HomeController |
| 748 | + ], |
| 749 | + globalConfig: { |
| 750 | + keys: '123', |
| 751 | + koa: { |
| 752 | + port: 3000, |
| 753 | + hostname: 'localhost', |
| 754 | + listenOptions: { |
| 755 | + port: 0, // listenOptions.port 应该优先于 port |
| 756 | + host: '127.0.0.1', // listenOptions.host 应该优先于 hostname |
| 757 | + backlog: 200, |
| 758 | + } |
| 759 | + } |
| 760 | + } |
| 761 | + }); |
| 762 | + |
| 763 | + await sleep(); |
| 764 | + |
| 765 | + // 验证端口配置 - listenOptions.port 应该优先于 port |
| 766 | + const framework = app.getFramework() as unknown as Framework; |
| 767 | + const port = framework.getPort(); |
| 768 | + expect(port).toBeDefined(); |
| 769 | + expect(port).not.toBe('3000'); // 应该使用 listenOptions.port (0) 而不是 port (3000) |
| 770 | + |
| 771 | + // 使用真正的 HTTP 请求测试 |
| 772 | + const result = await createRealHttpRequest(app as unknown as IMidwayKoaApplication, '/'); |
| 773 | + expect(result.status).toBe(200); |
| 774 | + |
| 775 | + await closeApp(app); |
| 776 | + }); |
| 777 | + }); |
665 | 778 | }); |
0 commit comments