Skip to content

Commit 1a71411

Browse files
committed
chore: print friendly localhost address from http server
1 parent 2734a05 commit 1a71411

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

packages/playwright-core/src/server/trace/viewer/traceViewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export async function runTraceInBrowser(traceUrls: string[], options: TraceViewe
155155
validateTraceUrls(traceUrls);
156156
const server = await startTraceViewerServer(options);
157157
await installRootRedirect(server, traceUrls, options);
158-
await openTraceInBrowser(server.urlPrefix());
158+
await openTraceInBrowser(server.urlPrefixLocalhost());
159159
}
160160

161161
export async function openTraceViewerApp(url: string, browserName: string, options?: TraceViewerAppOptions): Promise<Page> {

packages/playwright-core/src/utils/httpServer.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ export type Transport = {
3434

3535
export class HttpServer {
3636
private _server: http.Server;
37-
private _urlPrefix: string;
37+
private _urlPrefix: string = '';
38+
private _urlPrefixLocalhost: string = '';
3839
private _port: number = 0;
3940
private _started = false;
4041
private _routes: { prefix?: string, exact?: string, handler: ServerRouteHandler }[] = [];
4142
private _wsGuid: string | undefined;
4243

43-
constructor(address: string = '') {
44-
this._urlPrefix = address;
44+
constructor() {
4545
this._server = createHttpServer(this._onRequest.bind(this));
4646
}
4747

@@ -102,7 +102,7 @@ export class HttpServer {
102102
return this._wsGuid;
103103
}
104104

105-
async start(options: { port?: number, preferredPort?: number, host?: string } = {}): Promise<string> {
105+
async start(options: { port?: number, preferredPort?: number, host?: string } = {}): Promise<void> {
106106
assert(!this._started, 'server already started');
107107
this._started = true;
108108

@@ -121,16 +121,15 @@ export class HttpServer {
121121

122122
const address = this._server.address();
123123
assert(address, 'Could not bind server socket');
124-
if (!this._urlPrefix) {
125-
if (typeof address === 'string') {
126-
this._urlPrefix = address;
127-
} else {
128-
this._port = address.port;
129-
const resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
130-
this._urlPrefix = `http://${resolvedHost}:${address.port}`;
131-
}
124+
if (typeof address === 'string') {
125+
this._urlPrefix = address;
126+
this._urlPrefixLocalhost = address;
127+
} else {
128+
this._port = address.port;
129+
const resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
130+
this._urlPrefix = `http://${resolvedHost}:${address.port}`;
131+
this._urlPrefixLocalhost = `http://localhost:${address.port}`;
132132
}
133-
return this._urlPrefix;
134133
}
135134

136135
async stop() {
@@ -141,6 +140,10 @@ export class HttpServer {
141140
return this._urlPrefix;
142141
}
143142

143+
urlPrefixLocalhost(): string {
144+
return this._urlPrefixLocalhost;
145+
}
146+
144147
serveFile(request: http.IncomingMessage, response: http.ServerResponse, absoluteFilePath: string, headers?: { [name: string]: string }): boolean {
145148
try {
146149
for (const [name, value] of Object.entries(headers || {}))

packages/playwright/src/reporters/html.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ export async function showHTMLReport(reportFolder: string | undefined, host: str
182182
return;
183183
}
184184
const server = startHtmlReportServer(folder);
185-
let url = await server.start({ port, host, preferredPort: port ? undefined : 9323 });
185+
await server.start({ port, host, preferredPort: port ? undefined : 9323 });
186+
let url = server.urlPrefixLocalhost();
186187
console.log('');
187188
console.log(colors.cyan(` Serving HTML report at ${url}. Press Ctrl+C to quit.`));
188189
if (testId)

packages/playwright/src/runner/testServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ export async function runUIMode(configFile: string | undefined, options: TraceVi
418418
return await innerRunTestServer(configLocation, options, async (server: HttpServer, cancelPromise: ManualPromise<void>) => {
419419
await installRootRedirect(server, [], { ...options, webApp: 'uiMode.html' });
420420
if (options.host !== undefined || options.port !== undefined) {
421-
await openTraceInBrowser(server.urlPrefix());
421+
await openTraceInBrowser(server.urlPrefixLocalhost());
422422
} else {
423423
const page = await openTraceViewerApp(server.urlPrefix(), 'chromium', {
424424
headless: isUnderTest() && process.env.PWTEST_HEADED_FOR_TEST !== '1',

0 commit comments

Comments
 (0)