This commit is contained in:
Simon Knott 2024-11-04 09:55:53 +01:00
parent ebc738a248
commit 47756f7b23
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
2 changed files with 138 additions and 0 deletions

View file

@ -248,6 +248,32 @@ class CSharpCodeGen implements APIRequestCodegen {
}
}
class JavaCodeGen implements APIRequestCodegen {
generatePlaywrightRequestCall(request: har.Request, body: string | undefined): string {
const url = new URL(request.url);
const params = [`"${url.origin}${url.pathname}"`];
const options: string[] = [];
let method = request.method.toLowerCase();
if (!['delete', 'get', 'head', 'post', 'put', 'patch'].includes(method)) {
options.push(`setMethod("${method}")`);
method = 'fetch';
}
for (const [key, value] of url.searchParams)
options.push(`setQueryParam("${key}", "${value}")`);
if (body)
options.push(`setData("${body.replaceAll('"', '\\"')}")`);
for (const header of request.headers)
options.push(`setHeader("${header.name}", "${header.value}")`);
if (options.length > 0)
params.push(`RequestOptions.create()\n .${options.join('\n .')}\n`);
return `request.${method}(${params.join(', ')});`;
}
}
export function getAPIRequestCodeGen(language: Language): APIRequestCodegen {
if (language === 'javascript')
return new JSCodeGen();
@ -255,5 +281,7 @@ export function getAPIRequestCodeGen(language: Language): APIRequestCodegen {
return new PythonCodeGen();
if (language === 'csharp')
return new CSharpCodeGen();
if (language === 'java')
return new JavaCodeGen();
throw new Error('Unsupported language: ' + language);
}

View file

@ -480,3 +480,113 @@ await request.PutAsync("http://example.com/foo", new() {
});`.trim());
});
});
test.describe('java', () => {
const impl = getAPIRequestCodeGen('java');
test('generatePlaywrightRequestCall', () => {
expect(impl.generatePlaywrightRequestCall({
url: 'http://example.com/foo?bar=baz',
method: 'GET',
headers: [{ name: 'User-Agent', value: 'Mozilla/5.0' }, { name: 'Date', value: '2021-01-01' }],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, 'foo')).toEqual(`
request.get("http://example.com/foo", RequestOptions.create()
.setQueryParam("bar", "baz")
.setData("foo")
.setHeader("User-Agent", "Mozilla/5.0")
.setHeader("Date", "2021-01-01")
);`.trim());
expect(impl.generatePlaywrightRequestCall({
url: 'http://example.com/foo?bar=baz',
method: 'OPTIONS',
headers: [],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, undefined)).toEqual(`
request.fetch("http://example.com/foo", RequestOptions.create()
.setMethod("options")
.setQueryParam("bar", "baz")
);`.trim());
});
test('generatePlaywrightRequestCall with POST method and no body', () => {
expect(impl.generatePlaywrightRequestCall({
url: 'http://example.com/foo',
method: 'POST',
headers: [{ name: 'Content-Type', value: 'application/json' }],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, undefined)).toEqual(`
request.post("http://example.com/foo", RequestOptions.create()
.setHeader("Content-Type", "application/json")
);`.trim());
});
test('generatePlaywrightRequestCall with PUT method and JSON body', () => {
expect(impl.generatePlaywrightRequestCall({
url: 'http://example.com/foo',
method: 'PUT',
headers: [{ name: 'Content-Type', value: 'application/json' }],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, '{"key":"value"}')).toEqual(`
request.put("http://example.com/foo", RequestOptions.create()
.setData("{\\"key\\":\\"value\\"}")
.setHeader("Content-Type", "application/json")
);`.trim());
});
test('generatePlaywrightRequestCall with PATCH method and form data', () => {
expect(impl.generatePlaywrightRequestCall({
url: 'http://example.com/foo',
method: 'PATCH',
headers: [{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' }],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, 'key=value')).toEqual(`
request.patch("http://example.com/foo", RequestOptions.create()
.setData("key=value")
.setHeader("Content-Type", "application/x-www-form-urlencoded")
);`.trim());
});
test('generatePlaywrightRequestCall with DELETE method and custom header', () => {
expect(impl.generatePlaywrightRequestCall({
url: 'http://example.com/foo',
method: 'DELETE',
headers: [{ name: 'Authorization', value: 'Bearer token' }],
httpVersion: '1.1',
cookies: [],
queryString: [],
headersSize: 0,
bodySize: 0,
comment: '',
}, undefined)).toEqual(`
request.delete("http://example.com/foo", RequestOptions.create()
.setHeader("Authorization", "Bearer token")
);`.trim());
});
});