-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { beforeEach, describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import HttpApiRouter from '../../../src/controllers/http-api/http-api-router.js'; | ||
import JsonSchemaServiceMock from '../mock/json-schema-service-mock.js'; | ||
import HttpClientModuleManagerMock from '../mock/http-client-module-manager-mock.js'; | ||
import { HTTP_API_ROUTES } from '../../../src/constants/constants.js'; | ||
|
||
describe('HTTP API Router test', async () => { | ||
let httpApiRouter; | ||
const controllerMocks = {}; | ||
|
||
beforeEach(() => { | ||
// Mock Controllers | ||
Object.keys(HTTP_API_ROUTES).forEach((version) => { | ||
Object.keys(HTTP_API_ROUTES[version]).forEach((operation) => { | ||
const versionedController = `${operation}HttpApiController${ | ||
version.charAt(1).toUpperCase() + version.slice(2) | ||
}`; | ||
controllerMocks[versionedController] = { handleRequest: sinon.stub() }; | ||
}); | ||
}); | ||
|
||
// Mock context | ||
const ctx = { | ||
httpClientModuleManager: new HttpClientModuleManagerMock(), | ||
jsonSchemaService: new JsonSchemaServiceMock(), | ||
...controllerMocks, | ||
}; | ||
|
||
// Initialize HttpApiRouter with mocks | ||
httpApiRouter = new HttpApiRouter(ctx); | ||
}); | ||
|
||
it('Router has all defined routes', async () => { | ||
// Extract unique HTTP methods present across all versions | ||
const httpMethods = new Set(); | ||
Object.values(HTTP_API_ROUTES).forEach((routes) => { | ||
Object.values(routes).forEach((route) => { | ||
httpMethods.add(route.method); | ||
}); | ||
}); | ||
|
||
// Create spies for each extracted HTTP method on each router instance and httpClientModuleManager | ||
const spies = {}; | ||
Object.keys(HTTP_API_ROUTES).forEach((version) => { | ||
spies[version] = {}; | ||
Array.from(httpMethods).forEach((method) => { | ||
spies[version][method] = sinon.spy(httpApiRouter.routers[version], method); | ||
}); | ||
}); | ||
const httpClientModuleManagerUseSpy = sinon.spy( | ||
httpApiRouter.httpClientModuleManager, | ||
'use', | ||
); | ||
|
||
// Initialize the routes | ||
await httpApiRouter.initialize(); | ||
|
||
// Validate each route | ||
Object.entries(HTTP_API_ROUTES).forEach(([version, routes]) => { | ||
expect(httpClientModuleManagerUseSpy.calledWith(`/${version}`)).to.equal(true); | ||
|
||
Object.values(routes).forEach((routeDetails) => { | ||
const { method, path } = routeDetails; | ||
expect(spies[version][method].calledWith(path)).to.equal(true); | ||
}); | ||
}); | ||
expect(httpClientModuleManagerUseSpy.calledWith('/latest')).to.equal(true); | ||
expect(httpClientModuleManagerUseSpy.calledWith('/')).to.equal(true); | ||
|
||
// Restore all spies | ||
Object.values(spies).forEach((versionSpies) => { | ||
Object.values(versionSpies).forEach((spy) => { | ||
spy.restore(); | ||
}); | ||
}); | ||
httpClientModuleManagerUseSpy.restore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import express from 'express'; | ||
|
||
class HttpClientModuleManagerMock { | ||
createRouterInstance() { | ||
return express.Router(); | ||
} | ||
|
||
initializeBeforeMiddlewares() {} | ||
|
||
async listen() {} | ||
|
||
use(path, callback) {} | ||
|
||
get() {} | ||
|
||
post() {} | ||
|
||
selectMiddlewares(options) { | ||
return []; | ||
} | ||
|
||
initializeAfterMiddlewares() {} | ||
} | ||
|
||
export default HttpClientModuleManagerMock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class JsonSchemaServiceMock {} | ||
|
||
export default JsonSchemaServiceMock; |