diff --git a/package-lock.json b/package-lock.json index a1934bb68d7..ac3ec8b04fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -81,6 +81,7 @@ "@types/node-fetch": "^2.5.12", "@types/rimraf": "^3.0.2", "@types/serve-handler": "^6.1.1", + "@types/supertest": "^6.0.2", "@types/uuid": "^9.0.8", "@types/wrap-ansi": "^8.0.1", "@types/ws": "^8.2.0", @@ -104,6 +105,7 @@ "react-dom": "^16.14.0", "rimraf": "^3.0.2", "simple-git": "^3.16.0", + "supertest": "^6.3.4", "ts-node": "^10.9.1", "ts-node-dev": "^2.0.0", "tslib": "^2.3.1", @@ -43190,6 +43192,12 @@ "@types/node": "*" } }, + "node_modules/@types/cookiejar": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.5.tgz", + "integrity": "sha512-he+DHOWReW0nghN24E1WUqM0efK4kI9oTqDm6XmK8ZPe2djZ90BSNdGnIyCLzCPw7/pogPlGbzI2wHGGmi4O/Q==", + "dev": true + }, "node_modules/@types/cors": { "version": "2.8.17", "dev": true, @@ -43552,6 +43560,12 @@ "@types/lodash": "*" } }, + "node_modules/@types/methods": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@types/methods/-/methods-1.1.4.tgz", + "integrity": "sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==", + "dev": true + }, "node_modules/@types/mime": { "version": "1.3.5", "dev": true, @@ -43684,6 +43698,27 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/superagent": { + "version": "8.1.4", + "resolved": "https://registry.npmjs.org/@types/superagent/-/superagent-8.1.4.tgz", + "integrity": "sha512-uzSBYwrpal8y2X2Pul5ZSWpzRiDha2FLcquaN95qUPnOjYgm/zQ5LIdqeJpQJTRWNTN+Rhm0aC8H06Ds2rqCYw==", + "dev": true, + "dependencies": { + "@types/cookiejar": "^2.1.5", + "@types/methods": "^1.1.4", + "@types/node": "*" + } + }, + "node_modules/@types/supertest": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/@types/supertest/-/supertest-6.0.2.tgz", + "integrity": "sha512-137ypx2lk/wTQbW6An6safu9hXmajAifU/s7szAHLN/FeIm5w7yR0Wkl9fdJMRSHwOn4HLAI0DaB2TOORuhPDg==", + "dev": true, + "dependencies": { + "@types/methods": "^1.1.4", + "@types/superagent": "^8.1.0" + } + }, "node_modules/@types/through": { "version": "0.0.33", "dev": true, @@ -45717,6 +45752,15 @@ "dot-prop": "^5.1.0" } }, + "node_modules/component-emitter": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.1.tgz", + "integrity": "sha512-T0+barUSQRTUQASh8bx02dl+DhF54GtIDY13Y3m9oWTklKbb3Wv974meRpeZ3lp1JpLVECWWNHC4vaG2XHXouQ==", + "dev": true, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/compress-commons": { "version": "5.0.1", "license": "MIT", @@ -45865,6 +45909,12 @@ "version": "1.0.6", "license": "MIT" }, + "node_modules/cookiejar": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz", + "integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==", + "dev": true + }, "node_modules/core-js-compat": { "version": "3.35.1", "dev": true, @@ -48621,6 +48671,12 @@ "version": "2.5.2", "license": "MIT" }, + "node_modules/fast-safe-stringify": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", + "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==", + "dev": true + }, "node_modules/fast-url-parser": { "version": "1.1.3", "license": "MIT", @@ -48908,6 +48964,36 @@ "node": ">= 6" } }, + "node_modules/formidable": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-2.1.2.tgz", + "integrity": "sha512-CM3GuJ57US06mlpQ47YcunuUZ9jpm8Vx+P2CGt2j7HpgkKZO/DJYQ0Bobim8G6PFQmK5lOqOOdUXboU+h73A4g==", + "dev": true, + "dependencies": { + "dezalgo": "^1.0.4", + "hexoid": "^1.0.0", + "once": "^1.4.0", + "qs": "^6.11.0" + }, + "funding": { + "url": "https://ko-fi.com/tunnckoCore/commissions" + } + }, + "node_modules/formidable/node_modules/qs": { + "version": "6.12.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.12.0.tgz", + "integrity": "sha512-trVZiI6RMOkO476zLGaBIzszOdFPnCCXHPG9kn0yuS1uz6xdVxPfZdB3vUig9pxPFDM9BRAgz/YUIVQ1/vuiUg==", + "dev": true, + "dependencies": { + "side-channel": "^1.0.6" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/forwarded": { "version": "0.2.0", "license": "MIT", @@ -49528,6 +49614,15 @@ "node": ">=16.0.0" } }, + "node_modules/hexoid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/hexoid/-/hexoid-1.0.0.tgz", + "integrity": "sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/highlight.js": { "version": "10.7.3", "license": "BSD-3-Clause", @@ -73051,6 +73146,67 @@ "resolved": "https://registry.npmjs.org/strnum/-/strnum-1.0.5.tgz", "integrity": "sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA==" }, + "node_modules/superagent": { + "version": "8.1.2", + "resolved": "https://registry.npmjs.org/superagent/-/superagent-8.1.2.tgz", + "integrity": "sha512-6WTxW1EB6yCxV5VFOIPQruWGHqc3yI7hEmZK6h+pyk69Lk/Ut7rLUY6W/ONF2MjBuGjvmMiIpsrVJ2vjrHlslA==", + "dev": true, + "dependencies": { + "component-emitter": "^1.3.0", + "cookiejar": "^2.1.4", + "debug": "^4.3.4", + "fast-safe-stringify": "^2.1.1", + "form-data": "^4.0.0", + "formidable": "^2.1.2", + "methods": "^1.1.2", + "mime": "2.6.0", + "qs": "^6.11.0", + "semver": "^7.3.8" + }, + "engines": { + "node": ">=6.4.0 <13 || >=14" + } + }, + "node_modules/superagent/node_modules/mime": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.6.0.tgz", + "integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==", + "dev": true, + "bin": { + "mime": "cli.js" + }, + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/superagent/node_modules/qs": { + "version": "6.12.0", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.12.0.tgz", + "integrity": "sha512-trVZiI6RMOkO476zLGaBIzszOdFPnCCXHPG9kn0yuS1uz6xdVxPfZdB3vUig9pxPFDM9BRAgz/YUIVQ1/vuiUg==", + "dev": true, + "dependencies": { + "side-channel": "^1.0.6" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/supertest": { + "version": "6.3.4", + "resolved": "https://registry.npmjs.org/supertest/-/supertest-6.3.4.tgz", + "integrity": "sha512-erY3HFDG0dPnhw4U+udPfrzXa4xhSG+n4rxfRuZWCUvjFWwKl+OxWf/7zk50s84/fAAs7vf5QAb9uRa0cCykxw==", + "dev": true, + "dependencies": { + "methods": "^1.1.2", + "superagent": "^8.1.2" + }, + "engines": { + "node": ">=6.4.0" + } + }, "node_modules/supports-color": { "version": "8.1.1", "license": "MIT", diff --git a/package.json b/package.json index 48330ac8ce4..395e567a2a6 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "@types/node-fetch": "^2.5.12", "@types/rimraf": "^3.0.2", "@types/serve-handler": "^6.1.1", + "@types/supertest": "^6.0.2", "@types/uuid": "^9.0.8", "@types/wrap-ansi": "^8.0.1", "@types/ws": "^8.2.0", @@ -100,6 +101,7 @@ "react-dom": "^16.14.0", "rimraf": "^3.0.2", "simple-git": "^3.16.0", + "supertest": "^6.3.4", "ts-node": "^10.9.1", "ts-node-dev": "^2.0.0", "tslib": "^2.3.1", diff --git a/src/adapters/api/app.ts b/src/adapters/api/app.ts index 9801f5e6aa8..007fb699925 100644 --- a/src/adapters/api/app.ts +++ b/src/adapters/api/app.ts @@ -36,9 +36,8 @@ export class App { await this.initializeErrorHandling(); } public listen() { - this.app.listen(this.port, () => { + return this.app.listen(this.port, () => { logger.info('================================='); - logger.info(`= ENV: ${this.env}`); logger.info(`= 🚀 AsyncAPI Server API listening on the port ${this.port}`); logger.info('================================='); }); diff --git a/src/api.ts b/src/api.ts index db47db2946b..bfd2bbf88b8 100644 --- a/src/api.ts +++ b/src/api.ts @@ -8,6 +8,6 @@ export async function runApi(port: string| number) { new GeneratorController() ], port); await app.init(); - app.listen(); + return app.listen(); } // runApi(); diff --git a/test/integration/api.test.ts b/test/integration/api.test.ts new file mode 100644 index 00000000000..ed015c3a260 --- /dev/null +++ b/test/integration/api.test.ts @@ -0,0 +1,33 @@ +// api.test.js +import { expect, test } from '@oclif/test'; +import supertest from 'supertest'; +import { App } from '../../src/adapters/api/app'; +import { PingController } from '../../src/adapters/api/controllers/ping.controller'; +import { GeneratorController } from '../../src/adapters/api/controllers/generator.controller'; + +const apiUrl = 'http://localhost:3001/v1'; +let server: any; +let api: any; + +describe('API Tests', () => { + before(async() => { + api = new App([ + new PingController(), + new GeneratorController() + ], 3001); + await api.init(); + server = api.listen(); + }); + after(() => { + server.close(); + }); + describe('/ping', () => { + test + .it('should return 200 for /ping', async (ctx, done) => { + const response = await supertest(apiUrl).get('/ping'); + console.log(`response: ${JSON.stringify(response)}`); + expect(response.status).to.equal(200); + done(); + }); + }); +});