Skip to content

Commit 80c1c32

Browse files
author
cfernandes
committed
Tests for Kong API objects
1 parent 83fa40f commit 80c1c32

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

gulpfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const gutil = require('gulp-util');
66
const eslint = require('gulp-eslint');
77

88
const sourceFiles = ['lib/**/*.js', 'bin/**/*.js'];
9-
const testSourceFiles = ['test/**/**.spec.js'];
9+
const testSourceFiles = ['test/**/*.spec.js'];
1010
const allSourceFiles = sourceFiles.concat(testSourceFiles);
1111

1212
gulp.task('test', () =>

test/api.spec.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,55 @@
22

33
const chai = require('chai');
44
const expect = chai.expect;
5+
const kongContext = require('../lib/kong/context');
6+
const kongApi = require('../lib/kong/index');
7+
const localKongHost = 'http://127.0.0.1:8001';
58

69
describe('Kong API Object Specification', () => {
7-
it('must be able to create an API', () => {
8-
expect(10).to.equal(10);
10+
it('must be able to create an API', async () => {
11+
const context = kongContext('', '', localKongHost);
12+
const kong = kongApi(context);
13+
const apiName = 'example-api-0';
14+
const uriPath = '/test';
15+
const upstreamUrl = 'https://github.com';
16+
await kong.apis.createOrUpdateApi({
17+
name: apiName,
18+
uris: [uriPath],
19+
upstream_url: upstreamUrl
20+
});
21+
const result = await kong.apis.allApis();
22+
expect(result).to.have.lengthOf(1);
23+
const firstResult = result[0];
24+
expect(firstResult.name).to.equal(apiName);
25+
expect(firstResult.uris).to.have.lengthOf(1);
26+
expect(firstResult.uris[0]).to.equal(uriPath);
27+
expect(firstResult.upstream_url).to.equal(upstreamUrl);
28+
});
29+
30+
it('must be able to remove a created API', async () => {
31+
const context = kongContext('', '', localKongHost);
32+
const kong = kongApi(context);
33+
const apiName = 'example-api-1';
34+
const uriPath = '/test';
35+
const upstreamUrl = 'https://github.com';
36+
await kong.apis.createOrUpdateApi({
37+
name: apiName,
38+
uris: [uriPath],
39+
upstream_url: upstreamUrl
40+
});
41+
const result = await kong.apis.allApis();
42+
expect(result).to.have.lengthOf(1);
43+
await kong.apis.removeApi(apiName);
44+
const updatedResult = await kong.apis.allApis();
45+
expect(updatedResult).to.be.empty;
46+
});
47+
48+
afterEach(async () => {
49+
// clean up all entities
50+
const context = kongContext('', '', localKongHost);
51+
const kong = kongApi(context);
52+
const apis = await kong.apis.allApis();
53+
const apiNames = apis.map(eachApi => eachApi.name);
54+
apiNames.map(async name => await kong.apis.removeApi(name).catch(err => console.log(err.message)));
955
});
1056
});

yarn.lock

+7-1
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,13 @@ object.pick@^1.2.0:
15171517
dependencies:
15181518
isobject "^2.1.0"
15191519

1520-
once@^1.3.0, once@~1.3.0:
1520+
once@^1.3.0:
1521+
version "1.4.0"
1522+
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
1523+
dependencies:
1524+
wrappy "1"
1525+
1526+
once@~1.3.0:
15211527
version "1.3.3"
15221528
resolved "https://registry.yarnpkg.com/once/-/once-1.3.3.tgz#b2e261557ce4c314ec8304f3fa82663e4297ca20"
15231529
dependencies:

0 commit comments

Comments
 (0)