Skip to content

Commit

Permalink
Configure axios client
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Aug 28, 2020
1 parent 0731f8e commit aa1adae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/lib/PoWebClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ import { ServerError } from './errors';
import { CRA_CONTENT_TYPE, PoWebClient } from './PoWebClient';

describe('PoWebClient', () => {
describe('Common Axios instance attributes', () => {
test('responseType should be ArrayBuffer', () => {
const client = PoWebClient.initLocal();

expect(client.internalAxios.defaults.responseType).toEqual('arraybuffer');
});

test('maxContentLength should be 1 MiB', () => {
const client = PoWebClient.initLocal();

expect(client.internalAxios.defaults.maxContentLength).toEqual(1048576);
});

test('Redirects should be disabled', () => {
const client = PoWebClient.initLocal();

expect(client.internalAxios.defaults.maxRedirects).toEqual(0);
});
});

describe('initLocal', () => {
test('Host name should be the localhost IP address', () => {
const client = PoWebClient.initLocal();
Expand Down
9 changes: 7 additions & 2 deletions src/lib/PoWebClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const DEFAULT_REMOVE_PORT = 443;
const DEFAULT_LOCAL_TIMEOUT_MS = 3_000;
const DEFAULT_REMOTE_TIMEOUT_MS = 5_000;

const OCTETS_IN_ONE_MIB = 2 ** 20;

export const CRA_CONTENT_TYPE = 'application/vnd.relaynet.cra';

/**
Expand Down Expand Up @@ -49,10 +51,13 @@ export class PoWebClient {
) {
const httpSchema = useTLS ? 'https' : 'http';
const agentName = useTLS ? 'httpsAgent' : 'httpAgent';
const agent = useTLS ? new HttpsAgent({ keepAlive: true }) : new HttpAgent({ keepAlive: true });
const agentClass = useTLS ? HttpsAgent : HttpAgent;
this.internalAxios = axios.create({
[agentName]: new agentClass({ keepAlive: true }),
baseURL: `${httpSchema}://${hostName}:${port}/v1`,
[agentName]: agent,
maxContentLength: OCTETS_IN_ONE_MIB,
maxRedirects: 0,
responseType: 'arraybuffer',
timeout: timeoutMs,
});
}
Expand Down

0 comments on commit aa1adae

Please sign in to comment.