fix: API response to string (#10364)

This commit is contained in:
Yury Semikhatsky 2021-11-16 15:42:35 -08:00 committed by GitHub
parent 32bc83d322
commit 08a7470b0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 2 deletions

View File

@ -47,7 +47,7 @@ export class ConsoleMessage extends ChannelOwner<channels.ConsoleMessageChannel,
return this._initializer.location;
}
[(util.inspect as any).custom]() {
[util.inspect.custom]() {
return this.text();
}
}

View File

@ -17,6 +17,7 @@
import fs from 'fs';
import path from 'path';
import * as mime from 'mime';
import * as util from 'util';
import { Serializable } from '../../types/structs';
import * as api from '../../types/types';
import { HeadersArray } from '../common/types';
@ -272,6 +273,11 @@ export class APIResponse implements api.APIResponse {
});
}
[util.inspect.custom]() {
const headers = this.headersArray().map(({ name, value }) => ` ${name}: ${value}`);
return `APIResponse: ${this.status()} ${this.statusText()}\n${headers.join('\n')}`;
}
_fetchUid(): string {
return this._initializer.fetchUid;
}

View File

@ -241,7 +241,7 @@ export class Locator implements api.Locator {
});
}
[(util.inspect as any).custom]() {
[util.inspect.custom]() {
return this.toString();
}

View File

@ -15,6 +15,7 @@
*/
import http from 'http';
import * as util from 'util';
import { getPlaywrightVersion } from 'playwright-core/lib/utils/utils';
import { expect, playwrightTest as it } from './config/browserTest';
@ -294,3 +295,18 @@ it(`should accept already serialized data as Buffer when content-type is applica
expect(body.toString()).toEqual(value);
await request.dispose();
});
it(`should have nice toString`, async ({ playwright, server }) => {
const request = await playwright.request.newContext();
const response = await request.post(server.EMPTY_PAGE, {
headers: {
'content-type': 'application/json'
},
data: 'My post data'
});
const str = response[util.inspect.custom]();
expect(str).toContain('APIResponse: 200 OK');
for (const { name, value } of response.headersArray())
expect(str).toContain(` ${name}: ${value}`);
await request.dispose();
});