Skip to content

Commit

Permalink
fix: invalid character in header
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Aug 30, 2024
1 parent a99b27e commit 92507c8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
30 changes: 26 additions & 4 deletions packages/cli-server-api/src/__tests__/statusPageMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ import http from 'http';
import statusPageMiddleware from './../statusPageMiddleware';

describe('statusPageMiddleware', () => {
it('should set headers and end the response', () => {
process.cwd = () => '/mocked/path';
let res: jest.Mocked<http.ServerResponse>;
let mockReq: http.IncomingMessage;

const res: http.ServerResponse = {
beforeEach(() => {
res = {
setHeader: jest.fn(),
end: jest.fn(),
} as any;

const mockReq: http.IncomingMessage = {} as any;
mockReq = {} as any;
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should set headers and end the response', () => {
jest.spyOn(process, 'cwd').mockReturnValue('/mocked/path');

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.
Expand All @@ -20,4 +30,16 @@ describe('statusPageMiddleware', () => {
);
expect(res.end).toHaveBeenCalledWith('packager-status:running');
});

it('should set headers and end the response with invalid characters', () => {
jest.spyOn(process, 'cwd').mockReturnValue('/привіт/path');

statusPageMiddleware(mockReq, res);

expect(res.setHeader).toHaveBeenCalledWith(
'X-React-Native-Project-Root',
new URL(`file:///${process.cwd()}`).pathname.slice(1),
);
expect(res.end).toHaveBeenCalledWith('packager-status:running');
});
});
5 changes: 4 additions & 1 deletion packages/cli-server-api/src/statusPageMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default function statusPageMiddleware(
_req: http.IncomingMessage,
res: http.ServerResponse,
) {
res.setHeader('X-React-Native-Project-Root', process.cwd());
res.setHeader(
'X-React-Native-Project-Root',
new URL(`file:///${process.cwd()}`).pathname.slice(1),
);
res.end('packager-status:running');
}
5 changes: 4 additions & 1 deletion packages/cli-tools/src/getNextPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const getNextPort = async (port: number, root: string): Promise<Result> => {

const isRunning = typeof result === 'object' && result.status === 'running';

if (isRunning && result.root === root) {
if (
isRunning &&
result.root === new URL(`file:///${root}`).pathname.slice(1)
) {
// Found running bundler for this project, so we do not need to start packager!
start = false;
} else if (isRunning || result === 'unrecognized') {
Expand Down

0 comments on commit 92507c8

Please sign in to comment.