mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
171 lines
5.8 KiB
TypeScript
171 lines
5.8 KiB
TypeScript
import browser from './scripts/browser';
|
|
import { cleanup } from './scripts/cleanup';
|
|
import { compileWithTypescript } from './scripts/compileWithTypescript';
|
|
import { copyAsset } from './scripts/copyAsset';
|
|
import { generateClient } from './scripts/generateClient';
|
|
import server from './scripts/server';
|
|
|
|
describe('client.fetch', () => {
|
|
beforeAll(async () => {
|
|
cleanup('client/fetch');
|
|
await generateClient('client/fetch', 'v3', 'fetch', false, false, 'ApiClient');
|
|
copyAsset('index.html', 'client/fetch/index.html');
|
|
copyAsset('main.ts', 'client/fetch/main.ts');
|
|
compileWithTypescript('client/fetch');
|
|
await server.start('client/fetch');
|
|
await browser.start();
|
|
}, 30000);
|
|
|
|
afterAll(async () => {
|
|
await browser.stop();
|
|
await server.stop();
|
|
});
|
|
|
|
it('requests token', async () => {
|
|
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
|
|
const result = await browser.evaluate(async () => {
|
|
const { ApiClient } = (window as any).api;
|
|
const client = new ApiClient({
|
|
TOKEN: (window as any).tokenRequest,
|
|
USERNAME: undefined,
|
|
PASSWORD: undefined,
|
|
});
|
|
return await client.simple.getCallWithoutParametersAndResponse();
|
|
});
|
|
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
|
|
});
|
|
|
|
it('uses credentials', async () => {
|
|
const result = await browser.evaluate(async () => {
|
|
const { ApiClient } = (window as any).api;
|
|
const client = new ApiClient({
|
|
TOKEN: undefined,
|
|
USERNAME: 'username',
|
|
PASSWORD: 'password',
|
|
});
|
|
return await client.simple.getCallWithoutParametersAndResponse();
|
|
});
|
|
expect(result.headers.authorization).toBe('Basic dXNlcm5hbWU6cGFzc3dvcmQ=');
|
|
});
|
|
|
|
it('supports complex params', async () => {
|
|
const result = await browser.evaluate(async () => {
|
|
const { ApiClient } = (window as any).api;
|
|
const client = new ApiClient();
|
|
return await client.complex.complexTypes({
|
|
first: {
|
|
second: {
|
|
third: 'Hello World!',
|
|
},
|
|
},
|
|
});
|
|
});
|
|
expect(result).toBeDefined();
|
|
});
|
|
|
|
it('support form data', async () => {
|
|
const result = await browser.evaluate(async () => {
|
|
const { ApiClient } = (window as any).api;
|
|
const client = new ApiClient();
|
|
return await client.parameters.callWithParameters(
|
|
'valueHeader',
|
|
'valueQuery',
|
|
'valueForm',
|
|
'valueCookie',
|
|
'valuePath',
|
|
{
|
|
prop: 'valueBody',
|
|
}
|
|
);
|
|
});
|
|
expect(result).toBeDefined();
|
|
});
|
|
|
|
it('can abort the request', async () => {
|
|
let error;
|
|
try {
|
|
await browser.evaluate(async () => {
|
|
const { ApiClient } = (window as any).api;
|
|
const client = new ApiClient();
|
|
const promise = client.simple.getCallWithoutParametersAndResponse();
|
|
setTimeout(() => {
|
|
promise.cancel();
|
|
}, 10);
|
|
await promise;
|
|
});
|
|
} catch (e) {
|
|
error = (e as Error).message;
|
|
}
|
|
expect(error).toContain('CancelError: Request aborted');
|
|
});
|
|
|
|
it('should throw known error (500)', async () => {
|
|
const error = await browser.evaluate(async () => {
|
|
try {
|
|
const { ApiClient } = (window as any).api;
|
|
const client = new ApiClient();
|
|
await client.error.testErrorCode(500);
|
|
} catch (e) {
|
|
const error = e as any;
|
|
return JSON.stringify({
|
|
name: error.name,
|
|
message: error.message,
|
|
url: error.url,
|
|
status: error.status,
|
|
statusText: error.statusText,
|
|
body: error.body,
|
|
});
|
|
}
|
|
return;
|
|
});
|
|
|
|
expect(error).toBe(
|
|
JSON.stringify({
|
|
name: 'ApiError',
|
|
message: 'Custom message: Internal Server Error',
|
|
url: 'http://localhost:3000/base/api/v1.0/error?status=500',
|
|
status: 500,
|
|
statusText: 'Internal Server Error',
|
|
body: {
|
|
status: 500,
|
|
message: 'hello world',
|
|
},
|
|
})
|
|
);
|
|
});
|
|
|
|
it('should throw unknown error (409)', async () => {
|
|
const error = await browser.evaluate(async () => {
|
|
try {
|
|
const { ApiClient } = (window as any).api;
|
|
const client = new ApiClient();
|
|
await client.error.testErrorCode(409);
|
|
} catch (e) {
|
|
const error = e as any;
|
|
return JSON.stringify({
|
|
name: error.name,
|
|
message: error.message,
|
|
url: error.url,
|
|
status: error.status,
|
|
statusText: error.statusText,
|
|
body: error.body,
|
|
});
|
|
}
|
|
return;
|
|
});
|
|
expect(error).toBe(
|
|
JSON.stringify({
|
|
name: 'ApiError',
|
|
message: 'Generic Error',
|
|
url: 'http://localhost:3000/base/api/v1.0/error?status=409',
|
|
status: 409,
|
|
statusText: 'Conflict',
|
|
body: {
|
|
status: 409,
|
|
message: 'hello world',
|
|
},
|
|
})
|
|
);
|
|
});
|
|
});
|