Skip to content

Commit 783117b

Browse files
author
Weffe
committed
Merge branch 'dev'
2 parents e9c5f4a + f107e02 commit 783117b

File tree

8 files changed

+212
-157
lines changed

8 files changed

+212
-157
lines changed

.circleci/config.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Javascript Node CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
4+
#
5+
version: 2
6+
jobs:
7+
build:
8+
docker:
9+
# specify the version you desire here
10+
- image: circleci/node:10.14.1
11+
12+
# Specify service dependencies here if necessary
13+
# CircleCI maintains a library of pre-built images
14+
# documented at https://circleci.com/docs/2.0/circleci-images/
15+
# - image: circleci/mongo:3.4.4
16+
17+
working_directory: ~/repo
18+
19+
steps:
20+
- checkout
21+
22+
# Download and cache dependencies
23+
- restore_cache:
24+
keys:
25+
- v1-dependencies-{{ checksum "package.json" }}
26+
# fallback to using the latest cache if no exact match is found
27+
- v1-dependencies-
28+
29+
- run: yarn install
30+
31+
- save_cache:
32+
paths:
33+
- node_modules
34+
key: v1-dependencies-{{ checksum "package.json" }}
35+
36+
# run tests!
37+
- run: yarn test

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"axios": "^0.18.0",
4343
"axios-mock-adapter": "^1.16.0",
4444
"docsify-cli": "^4.3.0",
45+
"http-status-codes": "^1.3.2",
4546
"jest": "^24.1.0",
4647
"npm-run-all": "^4.1.5",
4748
"rimraf": "^2.6.3",
@@ -74,4 +75,4 @@
7475
},
7576
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$"
7677
}
77-
}
78+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import axios from 'axios';
2+
import MockAdapter from 'axios-mock-adapter';
3+
import * as status from 'http-status-codes';
4+
import { withVersioning } from '../axios-api-versioning';
5+
import { IWithVersioningConfig, VersioningStrategy } from '../types';
6+
import { AxiosInstanceWithVersioning } from '../types/axios';
7+
8+
const test_url = 'http://localhost:3000';
9+
const MOCK_RES = 'hello_world';
10+
let mock: MockAdapter;
11+
let instance: AxiosInstanceWithVersioning;
12+
13+
describe('Testing correct response config of "QueryString" strategy', () => {
14+
let versioningConfig: IWithVersioningConfig = {
15+
apiVersion: '1.0',
16+
versioningStrategy: VersioningStrategy.QueryString
17+
};
18+
19+
beforeAll(() => {
20+
instance = withVersioning(axios, versioningConfig);
21+
mock = new MockAdapter(instance);
22+
})
23+
24+
test('it should have the "apiVersion" as a query param in the response config', async () => {
25+
mock.onGet(test_url)
26+
.reply(status.OK, MOCK_RES);
27+
28+
const res = await instance.get(test_url);
29+
const { params } = res.config;
30+
31+
expect(params).toHaveProperty('api-version');
32+
expect(params['api-version']).toBe(versioningConfig.apiVersion);
33+
})
34+
})
35+
36+
describe('Testing correct response config of "MediaType" strategy', () => {
37+
let versioningConfig: IWithVersioningConfig = {
38+
apiVersion: '1.0',
39+
versioningStrategy: VersioningStrategy.MediaType
40+
};
41+
42+
beforeAll(() => {
43+
instance = withVersioning(axios, versioningConfig);
44+
mock = new MockAdapter(instance);
45+
})
46+
47+
test('it should have the "apiVersion" as an accept-param in the Accept header', async () => {
48+
mock.onGet(test_url)
49+
.reply(status.OK, MOCK_RES);
50+
51+
const res = await instance.get(test_url);
52+
const { headers } = res.config;
53+
54+
expect(headers).toHaveProperty('Accept');
55+
expect(headers['Accept']).toMatch(`v=${versioningConfig.apiVersion}`)
56+
})
57+
})
58+
59+
describe('Testing correct response config of "UrlPath" strategy', () => {
60+
let versioningConfig: IWithVersioningConfig = {
61+
apiVersion: '1',
62+
versioningStrategy: VersioningStrategy.UrlPath
63+
};
64+
65+
const blank_test_url = test_url + "/v{apiVersion}";
66+
const versioned_test_url = test_url + `/v${versioningConfig.apiVersion}`;
67+
68+
beforeAll(() => {
69+
instance = withVersioning(axios, versioningConfig);
70+
mock = new MockAdapter(instance);
71+
})
72+
73+
test('it should have the "apiVersion" as a url param in the url', async () => {
74+
mock.onGet(versioned_test_url)
75+
.reply(status.OK, MOCK_RES);
76+
77+
const res = await instance.get(blank_test_url);
78+
const { url } = res.config;
79+
80+
expect(url).toBe(versioned_test_url);
81+
})
82+
})

src/__tests__/axios-api-versioning.mocks.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import axios, { AxiosInstance } from 'axios';
2+
import { withVersioning } from '../axios-api-versioning';
3+
import { IWithVersioningConfig, VersioningStrategy } from '../types';
4+
import { AxiosInstanceWithVersioning } from '../types/axios';
5+
6+
const testVersioningConfig: IWithVersioningConfig = {
7+
apiVersion: '1.0',
8+
versioningStrategy: VersioningStrategy.QueryString
9+
}
10+
11+
// define an index type to stop TS from complaining about
12+
// accessing key/values with an index lookup
13+
declare module 'axios' {
14+
interface AxiosRequestConfig {
15+
[key: string]: any
16+
}
17+
}
18+
19+
describe('Testing no pollution on default exported axios object using withVersioning()', () => {
20+
beforeAll(() => {
21+
const instance = withVersioning(axios, testVersioningConfig);
22+
})
23+
24+
test('it should not add "apiVersion" to AxiosStatic defaults', () => {
25+
expect(axios.defaults['apiVersion']).toBe(undefined);
26+
})
27+
28+
test('it should not add "versioningStrategy" to AxiosStatic defaults', () => {
29+
expect(axios.defaults['versioningStrategy']).toBe(undefined);
30+
})
31+
32+
test('it should not add "ApiVersioningInterceptor" to AxiosStatic interceptors', () => {
33+
// @ts-ignore
34+
// we directly check the length of the handlers array for the request interceptors
35+
expect(axios.interceptors.request["handlers"].length)
36+
.toBe(0);
37+
})
38+
})
39+
40+
describe('Testing correct return value of withVersioning()', () => {
41+
let instance: AxiosInstanceWithVersioning;
42+
43+
beforeAll(() => {
44+
instance = withVersioning(axios, testVersioningConfig);
45+
})
46+
47+
test('it should correctly add "ApiVersioningInterceptor" to instance interceptors', () => {
48+
// @ts-ignore
49+
// we directly check the length of the handlers array for the request interceptors
50+
expect(instance.interceptors.request["handlers"].length)
51+
.toBe(1);
52+
})
53+
})
54+
55+
describe('Testing correct return value of withVersioning() using axios.create()', () => {
56+
let axiosInstance: AxiosInstance;
57+
let instance: AxiosInstanceWithVersioning;
58+
59+
beforeAll(() => {
60+
axiosInstance = axios.create();
61+
instance = withVersioning(axiosInstance, testVersioningConfig);
62+
})
63+
64+
test('it should correctly add "ApiVersioningInterceptor" to instance interceptors', () => {
65+
// @ts-ignore
66+
// we directly check the length of the handlers array for the request interceptors
67+
expect(instance.interceptors.request["handlers"].length)
68+
.toBe(1);
69+
})
70+
71+
test('it should not add "apiVersion" to axios.create() instance', () => {
72+
expect(axiosInstance.defaults['apiVersion']).toBe(undefined)
73+
})
74+
75+
test('it should not add "versioningStrategy" to axios.create() instance', () => {
76+
expect(axiosInstance.defaults['versioningStrategy']).toBe(undefined)
77+
})
78+
79+
test('it should not add "ApiVersioningInterceptor" to axios.create() instance', () => {
80+
// @ts-ignore
81+
// we directly check the length of the handlers array for the request interceptors
82+
expect(axiosInstance.interceptors.request["handlers"].length)
83+
.toBe(0);
84+
})
85+
})

src/__tests__/axios-api-versioning.test.ts

Lines changed: 0 additions & 124 deletions
This file was deleted.

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
66
"lib": [
77
"es2015",
8+
"dom"
89
], /* Specify library files to be included in the compilation. */
910
// "allowJs": true, /* Allow javascript files to be compiled. */
1011
// "checkJs": true, /* Report errors in .js files. */

0 commit comments

Comments
 (0)