some more tests

This commit is contained in:
Simon Knott 2024-10-30 13:48:29 +01:00
parent 5be1c4dce5
commit 734353bbfa
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
2 changed files with 67 additions and 2 deletions

View file

@ -31,7 +31,12 @@ export function generatePlaywrightRequestCall(request: har.Request, body: string
options.data = body;
if (request.headers.length)
options.headers = Object.fromEntries(request.headers.map(header => [header.name, header.value]));
return `await page.request.${method}('${urlParam}', ${prettyPrintObject(options)});`;
const params = [`'${urlParam}'`];
const hasOptions = Object.keys(options).length > 0;
if (hasOptions)
params.push(prettyPrintObject(options));
return `await page.request.${method}(${params.join(', ')});`;
}
function prettyPrintObject(obj: any, indent = 2, level = 0): string {

View file

@ -15,7 +15,7 @@
*/
import { test, expect } from '@playwright/test';
import { generatePlaywrightRequestCall } from "../../../packages/trace-viewer/src/ui/codegen";
import { generatePlaywrightRequestCall } from '../../../packages/trace-viewer/src/ui/codegen';
test('generatePlaywrightRequestCall', () => {
expect(generatePlaywrightRequestCall({
@ -136,3 +136,63 @@ await page.request.delete('http://example.com/foo', {
}
});`.trim());
});
test('generatePlaywrightRequestCall with HEAD method', () => {
expect(generatePlaywrightRequestCall({
url: 'http://example.com/foo',
method: 'HEAD',
headers: [],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, undefined)).toEqual(`
await page.request.head('http://example.com/foo');`.trim());
});
test('generatePlaywrightRequestCall with complex query parameters', () => {
expect(generatePlaywrightRequestCall({
url: 'http://example.com/foo?bar=baz&qux=quux',
method: 'GET',
headers: [],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, undefined)).toEqual(`
await page.request.get('http://example.com/foo', {
params: {
bar: 'baz',
qux: 'quux'
}
});`.trim());
});
test('generatePlaywrightRequestCall with multiple headers', () => {
expect(generatePlaywrightRequestCall({
url: 'http://example.com/foo',
method: 'GET',
headers: [
{ name: 'User-Agent', value: 'Mozilla/5.0' },
{ name: 'Accept', value: 'application/json' },
{ name: 'Authorization', value: 'Bearer token' }
],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, undefined)).toEqual(`
await page.request.get('http://example.com/foo', {
headers: {
'User-Agent': 'Mozilla/5.0',
Accept: 'application/json',
Authorization: 'Bearer token'
}
});`.trim());
});