Skip to content

Commit 22cc556

Browse files
fix: change request response in /status endpoint (#2056)
* fix: change request response in `status` endpoint * feat: add test for `statusPageMiddleware`
1 parent f621ae4 commit 22cc556

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import http from 'http';
2+
import statusPageMiddleware from './../statusPageMiddleware';
3+
4+
describe('statusPageMiddleware', () => {
5+
it('should set headers and end the response', () => {
6+
process.cwd = () => '/mocked/path';
7+
8+
const res: http.ServerResponse = {
9+
setHeader: jest.fn(),
10+
end: jest.fn(),
11+
} as any;
12+
13+
const mockReq: http.IncomingMessage = {} as any;
14+
statusPageMiddleware(mockReq, res);
15+
16+
// We're strictly checking response here, because React Native is strongly depending on this response. Changing the response might be a breaking change.
17+
expect(res.setHeader).toHaveBeenCalledWith('Project-Root', '/mocked/path');
18+
expect(res.end).toHaveBeenCalledWith('packager-status:running');
19+
});
20+
});

packages/cli-server-api/src/statusPageMiddleware.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ export default function statusPageMiddleware(
1414
_req: http.IncomingMessage,
1515
res: http.ServerResponse,
1616
) {
17-
res.end(
18-
JSON.stringify({
19-
status: 'running',
20-
root: process.cwd(),
21-
}),
22-
);
17+
res.setHeader('Project-Root', process.cwd());
18+
res.end('packager-status:running');
2319
}

packages/cli-tools/src/isPackagerRunning.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ async function isPackagerRunning(
2626
| 'unrecognized'
2727
> {
2828
try {
29-
const {data} = await fetch(`http://localhost:${packagerPort}/status`);
29+
const {data, headers} = await fetch(
30+
`http://localhost:${packagerPort}/status`,
31+
);
3032

3133
try {
32-
if (data.status === 'running') {
33-
return data;
34+
if (data === 'packager-status:running') {
35+
return {
36+
status: 'running',
37+
root: headers.get('Project-Root') ?? '',
38+
};
3439
}
3540
} catch (_error) {
3641
return 'unrecognized';

0 commit comments

Comments
 (0)