11import { fileURLToPath } from 'url'
22import path from 'path'
33import http from 'http'
4+ import { Socket } from 'net'
45
56import { describe , expect , test } from 'vitest'
67import type { Response } from 'express'
78
89import Page from '@/frame/lib/page'
910import findPage from '@/frame/middleware/find-page'
10- import type { ExtendedRequest } from '@/types'
11+ import type { ExtendedRequest , Context } from '@/types'
1112
1213const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
1314
15+ type TestResponse = Response & { _status ?: number ; _message ?: string }
16+
1417function makeRequestResponse (
1518 url : string ,
1619 currentVersion = 'free-pro-team@latest' ,
17- ) : [ ExtendedRequest , Response & { _status ?: number ; _message ?: string } ] {
18- const req = new http . IncomingMessage ( null as any ) as ExtendedRequest
20+ ) : [ ExtendedRequest , TestResponse ] {
21+ const req = new http . IncomingMessage ( new Socket ( ) ) as ExtendedRequest
22+
23+ Object . defineProperty ( req , 'path' , {
24+ value : url ,
25+ writable : true ,
26+ } )
27+
1928 req . method = 'GET'
2029 req . url = url
21- // @ts -expect-error - path is read-only but we need to set it for testing
22- req . path = url
2330 req . cookies = { }
2431 req . headers = { }
2532
26- // Custom keys on the request
33+ const context : Context = {
34+ currentVersion,
35+ pages : { } ,
36+ }
37+
2738 req . pagePath = url
28- req . context = { }
29- req . context . currentVersion = currentVersion
30- req . context . pages = { }
39+ req . context = context
3140
32- const res = new http . ServerResponse ( req ) as Response & {
33- _status ?: number
34- _message ?: string
35- }
36- res . status = function ( code : number ) {
41+ const res = new http . ServerResponse ( req ) as TestResponse
42+ res . status = function status ( this : TestResponse , code : number ) {
3743 this . _status = code
38- return {
44+ return Object . assign ( this , {
3945 send : ( message : string ) => {
4046 this . _message = message
47+ return this
4148 } ,
42- } as any
49+ } )
4350 }
4451
4552 return [ req , res ]
@@ -56,7 +63,7 @@ describe('find page middleware', () => {
5663 } )
5764 if ( page && req . context ) {
5865 req . context . pages = {
59- '/en/foo/bar' : page as any ,
66+ '/en/foo/bar' : page ,
6067 }
6168 }
6269
@@ -88,7 +95,7 @@ describe('find page middleware', () => {
8895 } )
8996 if ( page && req . context ) {
9097 req . context . pages = {
91- '/en/page-with-redirects' : page as any ,
98+ '/en/page-with-redirects' : page ,
9299 }
93100 }
94101
@@ -98,6 +105,7 @@ describe('find page middleware', () => {
98105 } )
99106 expect ( req . context ?. page ) . toBeInstanceOf ( Page )
100107 } )
108+
101109 test ( 'finds it for non-fpt version URLS' , async ( ) => {
102110 const [ req , res ] = makeRequestResponse ( '/en/page-with-redirects' , 'enterprise-cloud@latest' )
103111 const page = await Page . init ( {
@@ -107,7 +115,7 @@ describe('find page middleware', () => {
107115 } )
108116 if ( page && req . context ) {
109117 req . context . pages = {
110- '/en/page-with-redirects' : page as any ,
118+ '/en/page-with-redirects' : page ,
111119 }
112120 }
113121
@@ -129,7 +137,7 @@ describe('find page middleware', () => {
129137 } )
130138 if ( page && req . context ) {
131139 req . context . pages = {
132- '/en/page-with-redirects' : page as any ,
140+ '/en/page-with-redirects' : page ,
133141 }
134142 }
135143
0 commit comments