@@ -3,70 +3,73 @@ import { describe, it } from 'node:test';
3
3
4
4
import { type FetchProxyOptions , createFetchProxy } from './fetch-proxy.ts' ;
5
5
6
- async function runProxy (
7
- request : Request ,
6
+ async function testProxy (
7
+ input : URL | RequestInfo ,
8
+ init : RequestInit | undefined ,
8
9
target : string | URL ,
9
10
options ?: FetchProxyOptions ,
10
11
) : Promise < { request : Request ; response : Response } > {
11
- let outgoingRequest : Request ;
12
+ let request : Request ;
12
13
let proxy = createFetchProxy ( target , {
13
14
...options ,
14
- async fetch ( input , init ) {
15
- outgoingRequest = new Request ( input , init ) ;
16
- return options ?. fetch ?.( input , init ) ?? new Response ( ) ;
15
+ fetch ( input , init ) {
16
+ request = new Request ( input , init ) ;
17
+ return options ?. fetch ?.( input , init ) ?? Promise . resolve ( new Response ( ) ) ;
17
18
} ,
18
19
} ) ;
19
20
20
- let proxyResponse = await proxy ( request ) ;
21
+ let response = await proxy ( input , init ) ;
21
22
22
- assert . ok ( outgoingRequest ! ) ;
23
+ assert . ok ( request ! ) ;
23
24
24
- return {
25
- request : outgoingRequest ,
26
- response : proxyResponse ,
27
- } ;
25
+ return { request, response } ;
28
26
}
29
27
30
28
describe ( 'fetch proxy' , ( ) => {
31
29
it ( 'appends the request URL pathname + search to the target URL' , async ( ) => {
32
- let { request : request1 } = await runProxy (
33
- new Request ( 'http://shopify.com' ) ,
30
+ let { request : request1 } = await testProxy (
31
+ 'http://shopify.com' ,
32
+ undefined ,
34
33
'https://remix.run:3000/rsc' ,
35
34
) ;
36
35
37
36
assert . equal ( request1 . url , 'https://remix.run:3000/rsc' ) ;
38
37
39
- let { request : request2 } = await runProxy (
40
- new Request ( 'http://shopify.com/?q=remix' ) ,
38
+ let { request : request2 } = await testProxy (
39
+ 'http://shopify.com/?q=remix' ,
40
+ undefined ,
41
41
'https://remix.run:3000/rsc' ,
42
42
) ;
43
43
44
44
assert . equal ( request2 . url , 'https://remix.run:3000/rsc?q=remix' ) ;
45
45
46
- let { request : request3 } = await runProxy (
47
- new Request ( 'http://shopify.com/search?q=remix' ) ,
46
+ let { request : request3 } = await testProxy (
47
+ 'http://shopify.com/search?q=remix' ,
48
+ undefined ,
48
49
'https://remix.run:3000/' ,
49
50
) ;
50
51
51
52
assert . equal ( request3 . url , 'https://remix.run:3000/search?q=remix' ) ;
52
53
53
- let { request : request4 } = await runProxy (
54
- new Request ( 'http://shopify.com/search?q=remix' ) ,
54
+ let { request : request4 } = await testProxy (
55
+ 'http://shopify.com/search?q=remix' ,
56
+ undefined ,
55
57
'https://remix.run:3000/rsc' ,
56
58
) ;
57
59
58
60
assert . equal ( request4 . url , 'https://remix.run:3000/rsc/search?q=remix' ) ;
59
61
} ) ;
60
62
61
63
it ( 'forwards request method, headers, and body' , async ( ) => {
62
- let { request } = await runProxy (
63
- new Request ( 'http://shopify.com/search?q=remix' , {
64
+ let { request } = await testProxy (
65
+ 'http://shopify.com/search?q=remix' ,
66
+ {
64
67
method : 'POST' ,
65
68
headers : {
66
69
'Content-Type' : 'text/plain' ,
67
70
} ,
68
71
body : 'hello' ,
69
- } ) ,
72
+ } ,
70
73
'https://remix.run:3000/rsc' ,
71
74
) ;
72
75
@@ -75,9 +78,24 @@ describe('fetch proxy', () => {
75
78
assert . equal ( await request . text ( ) , 'hello' ) ;
76
79
} ) ;
77
80
81
+ it ( 'forwards an empty request body' , async ( ) => {
82
+ let { request } = await testProxy (
83
+ 'http://shopify.com/search?q=remix' ,
84
+ {
85
+ method : 'POST' ,
86
+ } ,
87
+ 'https://remix.run:3000/rsc' ,
88
+ ) ;
89
+
90
+ assert . equal ( request . method , 'POST' ) ;
91
+ assert . equal ( request . headers . get ( 'Content-Type' ) , null ) ;
92
+ assert . equal ( await request . text ( ) , '' ) ;
93
+ } ) ;
94
+
78
95
it ( 'does not append X-Forwarded-Proto and X-Forwarded-Host headers by default' , async ( ) => {
79
- let { request } = await runProxy (
80
- new Request ( 'http://shopify.com:8080/search?q=remix' ) ,
96
+ let { request } = await testProxy (
97
+ 'http://shopify.com:8080/search?q=remix' ,
98
+ undefined ,
81
99
'https://remix.run:3000/rsc' ,
82
100
) ;
83
101
@@ -86,8 +104,9 @@ describe('fetch proxy', () => {
86
104
} ) ;
87
105
88
106
it ( 'appends X-Forwarded-Proto and X-Forwarded-Host headers when desired' , async ( ) => {
89
- let { request } = await runProxy (
90
- new Request ( 'http://shopify.com:8080/search?q=remix' ) ,
107
+ let { request } = await testProxy (
108
+ 'http://shopify.com:8080/search?q=remix' ,
109
+ undefined ,
91
110
'https://remix.run:3000/rsc' ,
92
111
{
93
112
xForwardedHeaders : true ,
@@ -98,9 +117,26 @@ describe('fetch proxy', () => {
98
117
assert . equal ( request . headers . get ( 'X-Forwarded-Host' ) , 'shopify.com:8080' ) ;
99
118
} ) ;
100
119
120
+ it ( 'forwards additional request init options' , async ( ) => {
121
+ let { request } = await testProxy (
122
+ 'http://shopify.com/search?q=remix' ,
123
+ {
124
+ cache : 'no-cache' ,
125
+ credentials : 'include' ,
126
+ redirect : 'manual' ,
127
+ } ,
128
+ 'https://remix.run:3000/rsc' ,
129
+ ) ;
130
+
131
+ assert . equal ( request . cache , 'no-cache' ) ;
132
+ assert . equal ( request . credentials , 'include' ) ;
133
+ assert . equal ( request . redirect , 'manual' ) ;
134
+ } ) ;
135
+
101
136
it ( 'rewrites cookie domain and path' , async ( ) => {
102
- let { response } = await runProxy (
103
- new Request ( 'http://shopify.com/search?q=remix' ) ,
137
+ let { response } = await testProxy (
138
+ 'http://shopify.com/search?q=remix' ,
139
+ undefined ,
104
140
'https://remix.run:3000/rsc' ,
105
141
{
106
142
async fetch ( ) {
@@ -122,8 +158,9 @@ describe('fetch proxy', () => {
122
158
} ) ;
123
159
124
160
it ( 'does not rewrite cookie domain and path when opting-out' , async ( ) => {
125
- let { response } = await runProxy (
126
- new Request ( 'http://shopify.com/?q=remix' ) ,
161
+ let { response } = await testProxy (
162
+ 'http://shopify.com/?q=remix' ,
163
+ undefined ,
127
164
'https://remix.run:3000/rsc' ,
128
165
{
129
166
rewriteCookieDomain : false ,
0 commit comments