Skip to content

Commit e1e43b0

Browse files
committed
test(rsc-mf): cover middleware context body response finalization
1 parent 18bbd03 commit e1e43b0

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

tests/integration/rsc-mf/tests/modernServerConfig.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,49 @@ describe('rsc-mf host modern.server middleware contracts', () => {
134134
await expect(context.res?.text()).resolves.toBe('proxied-js');
135135
});
136136

137+
it('finalizes proxied responses through context body API when available', async () => {
138+
const handler = getProxyMiddlewareHandler();
139+
const next = jest.fn(async (): Promise<void> => undefined);
140+
const fetchMock = installFetchMock(async () => {
141+
return new Response('proxied-via-body', {
142+
status: 203,
143+
headers: {
144+
'content-type': 'application/javascript',
145+
'content-length': '111',
146+
},
147+
});
148+
});
149+
const context: {
150+
req: { url: string };
151+
res?: Response;
152+
body: (
153+
body: BodyInit | null,
154+
status?: number,
155+
headers?: HeadersInit,
156+
) => Response;
157+
} = {
158+
req: {
159+
url: 'http://127.0.0.1:3007/static/js/async/__federation_expose_infoBundle.11dea89e81.js',
160+
},
161+
body: jest.fn((body, status, headers) => {
162+
return new Response(body, {
163+
status,
164+
headers,
165+
});
166+
}),
167+
};
168+
169+
await withRemotePort('3999', () => handler(context as any, next));
170+
171+
expect(fetchMock).toHaveBeenCalledTimes(1);
172+
expect(context.body).toHaveBeenCalledTimes(1);
173+
expect(context.res).toBeInstanceOf(Response);
174+
expect(context.res?.status).toBe(203);
175+
expect(context.res?.headers.get('content-length')).toBeNull();
176+
await expect(context.res?.text()).resolves.toBe('proxied-via-body');
177+
expect(next).not.toHaveBeenCalled();
178+
});
179+
137180
it('strips transfer headers from proxied upstream responses', async () => {
138181
const handler = getProxyMiddlewareHandler();
139182
const next = jest.fn(async (): Promise<void> => undefined);

tests/integration/rsc-mf/tests/remoteModernServerConfig.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,81 @@ describe('rsc-mf remote modern.server middleware contracts', () => {
146146
await expect(context.res?.text()).resolves.toBe('fallback-asset');
147147
});
148148

149+
it('finalizes recovered responses through context body API when available', async () => {
150+
const handler = getRecoverMiddlewareHandler();
151+
const next = jest.fn(async (): Promise<void> => undefined);
152+
const fetchMock = installFetchMock(
153+
jest
154+
.fn()
155+
.mockResolvedValueOnce(
156+
new Response(
157+
JSON.stringify({
158+
exposes: [
159+
{
160+
assets: {
161+
js: {
162+
sync: [
163+
'static/js/async/__federation_expose_RemoteServerCard.a1b2c3d4.js',
164+
],
165+
async: [],
166+
},
167+
css: {
168+
sync: [],
169+
async: [],
170+
},
171+
},
172+
},
173+
],
174+
}),
175+
{
176+
status: 200,
177+
headers: {
178+
'content-type': 'application/json',
179+
},
180+
},
181+
),
182+
)
183+
.mockResolvedValueOnce(
184+
new Response('fallback-via-body', {
185+
status: 203,
186+
headers: {
187+
'content-type': 'application/javascript',
188+
'content-length': '222',
189+
},
190+
}),
191+
),
192+
);
193+
const context: {
194+
req: { url: string };
195+
res?: Response;
196+
body: (
197+
body: BodyInit | null,
198+
status?: number,
199+
headers?: HeadersInit,
200+
) => Response;
201+
} = {
202+
req: {
203+
url: 'http://127.0.0.1:3008/static/js/async/__federation_expose_RemoteServerCard.js',
204+
},
205+
body: jest.fn((body, status, headers) => {
206+
return new Response(body, {
207+
status,
208+
headers,
209+
});
210+
}),
211+
};
212+
213+
await handler(context as any, next);
214+
215+
expect(fetchMock).toHaveBeenCalledTimes(2);
216+
expect(context.body).toHaveBeenCalledTimes(1);
217+
expect(context.res).toBeInstanceOf(Response);
218+
expect(context.res?.status).toBe(203);
219+
expect(context.res?.headers.get('content-length')).toBeNull();
220+
await expect(context.res?.text()).resolves.toBe('fallback-via-body');
221+
expect(next).not.toHaveBeenCalled();
222+
});
223+
149224
it('strips transfer headers from recovered fallback responses', async () => {
150225
const handler = getRecoverMiddlewareHandler();
151226
const next = jest.fn(async (): Promise<void> => undefined);

0 commit comments

Comments
 (0)