From 22cc556bbb70cdc9c171bcf77d0714afed346215 Mon Sep 17 00:00:00 2001 From: Szymon Rybczak Date: Wed, 16 Aug 2023 08:12:16 +0200 Subject: [PATCH] fix: change request response in `/status` endpoint (#2056) * fix: change request response in `status` endpoint * feat: add test for `statusPageMiddleware` --- .../__tests__/statusPageMiddleware.test.ts | 20 +++++++++++++++++++ .../src/statusPageMiddleware.ts | 8 ++------ packages/cli-tools/src/isPackagerRunning.ts | 11 +++++++--- 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts diff --git a/packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts b/packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts new file mode 100644 index 000000000..49b7557f9 --- /dev/null +++ b/packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts @@ -0,0 +1,20 @@ +import http from 'http'; +import statusPageMiddleware from './../statusPageMiddleware'; + +describe('statusPageMiddleware', () => { + it('should set headers and end the response', () => { + process.cwd = () => '/mocked/path'; + + const res: http.ServerResponse = { + setHeader: jest.fn(), + end: jest.fn(), + } as any; + + const mockReq: http.IncomingMessage = {} as any; + statusPageMiddleware(mockReq, res); + + // We're strictly checking response here, because React Native is strongly depending on this response. Changing the response might be a breaking change. + expect(res.setHeader).toHaveBeenCalledWith('Project-Root', '/mocked/path'); + expect(res.end).toHaveBeenCalledWith('packager-status:running'); + }); +}); diff --git a/packages/cli-server-api/src/statusPageMiddleware.ts b/packages/cli-server-api/src/statusPageMiddleware.ts index 28dcbffff..dad691fa0 100644 --- a/packages/cli-server-api/src/statusPageMiddleware.ts +++ b/packages/cli-server-api/src/statusPageMiddleware.ts @@ -14,10 +14,6 @@ export default function statusPageMiddleware( _req: http.IncomingMessage, res: http.ServerResponse, ) { - res.end( - JSON.stringify({ - status: 'running', - root: process.cwd(), - }), - ); + res.setHeader('Project-Root', process.cwd()); + res.end('packager-status:running'); } diff --git a/packages/cli-tools/src/isPackagerRunning.ts b/packages/cli-tools/src/isPackagerRunning.ts index 9d93fcad9..fe3d49f78 100644 --- a/packages/cli-tools/src/isPackagerRunning.ts +++ b/packages/cli-tools/src/isPackagerRunning.ts @@ -26,11 +26,16 @@ async function isPackagerRunning( | 'unrecognized' > { try { - const {data} = await fetch(`http://localhost:${packagerPort}/status`); + const {data, headers} = await fetch( + `http://localhost:${packagerPort}/status`, + ); try { - if (data.status === 'running') { - return data; + if (data === 'packager-status:running') { + return { + status: 'running', + root: headers.get('Project-Root') ?? '', + }; } } catch (_error) { return 'unrecognized';