openapi-typescript-codegen/test/e2e/client.axios.spec.ts

151 lines
5.0 KiB
TypeScript

import { cleanup } from './scripts/cleanup';
import { compileWithTypescript } from './scripts/compileWithTypescript';
import { generateClient } from './scripts/generateClient';
import server from './scripts/server';
describe('client.axios', () => {
beforeAll(async () => {
cleanup('client/axios');
await generateClient('client/axios', 'v3', 'axios', false, false, 'ApiClient');
compileWithTypescript('client/axios');
await server.start('client/axios');
}, 30000);
afterAll(async () => {
await server.stop();
});
it('requests token', async () => {
const { ApiClient } = require('./generated/client/axios/index.js');
const tokenRequest = jest.fn().mockResolvedValue('MY_TOKEN');
const client = new ApiClient({
TOKEN: tokenRequest,
USERNAME: undefined,
PASSWORD: undefined,
});
const result = await client.simple.getCallWithoutParametersAndResponse();
expect(tokenRequest.mock.calls.length).toBe(1);
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
});
it('uses credentials', async () => {
const { ApiClient } = require('./generated/client/axios/index.js');
const client = new ApiClient({
TOKEN: undefined,
USERNAME: 'username',
PASSWORD: 'password',
});
const result = await client.simple.getCallWithoutParametersAndResponse();
expect(result.headers.authorization).toBe('Basic dXNlcm5hbWU6cGFzc3dvcmQ=');
});
it('supports complex params', async () => {
const { ApiClient } = require('./generated/client/axios/index.js');
const client = new ApiClient();
const result = await client.complex.complexTypes({
first: {
second: {
third: 'Hello World!',
},
},
});
expect(result).toBeDefined();
});
it('supports form data', async () => {
const { ApiClient } = require('./generated/client/axios/index.js');
const client = new ApiClient();
const result = await client.parameters.callWithParameters(
'valueHeader',
'valueQuery',
'valueForm',
'valueCookie',
'valuePath',
{
prop: 'valueBody',
}
);
expect(result).toBeDefined();
});
it('can abort the request', async () => {
let error;
try {
const { ApiClient } = require('./generated/client/axios/index.js');
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('Request aborted');
});
it('should throw known error (500)', async () => {
let error;
try {
const { ApiClient } = require('./generated/client/axios/index.js');
const client = new ApiClient();
await client.error.testErrorCode(500);
} catch (e) {
const err = e as any;
error = JSON.stringify({
name: err.name,
message: err.message,
url: err.url,
status: err.status,
statusText: err.statusText,
body: err.body,
});
}
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 () => {
let error;
try {
const { ApiClient } = require('./generated/client/axios/index.js');
const client = new ApiClient();
await client.error.testErrorCode(409);
} catch (e) {
const err = e as any;
error = JSON.stringify({
name: err.name,
message: err.message,
url: err.url,
status: err.status,
statusText: err.statusText,
body: err.body,
});
}
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',
},
})
);
});
});