Skip to content

Commit

Permalink
fix: change request response in /status endpoint (#2056)
Browse files Browse the repository at this point in the history
* fix: change request response in `status` endpoint

* feat: add test for `statusPageMiddleware`
  • Loading branch information
szymonrybczak authored Aug 16, 2023
1 parent f621ae4 commit 22cc556
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
20 changes: 20 additions & 0 deletions packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
8 changes: 2 additions & 6 deletions packages/cli-server-api/src/statusPageMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
11 changes: 8 additions & 3 deletions packages/cli-tools/src/isPackagerRunning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 22cc556

Please sign in to comment.