-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
11 changed files
with
319 additions
and
73 deletions.
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
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,164 @@ | ||
import request from 'supertest'; | ||
|
||
import { App } from '../../app'; | ||
import { ProblemException } from '../../exceptions/problem.exception'; | ||
|
||
import { ValidateController } from '../validate.controller'; | ||
|
||
const validJSONAsyncAPI = { | ||
asyncapi: '2.2.0', | ||
info: { | ||
title: 'Account Service', | ||
version: '1.0.0', | ||
description: 'This service is in charge of processing user signups' | ||
}, | ||
channels: { | ||
'user/signedup': { | ||
subscribe: { | ||
message: { | ||
$ref: '#/components/messages/UserSignedUp' | ||
} | ||
} | ||
} | ||
}, | ||
components: { | ||
messages: { | ||
UserSignedUp: { | ||
payload: { | ||
type: 'object', | ||
properties: { | ||
displayName: { | ||
type: 'string', | ||
description: 'Name of the user' | ||
}, | ||
email: { | ||
type: 'string', | ||
format: 'email', | ||
description: 'Email of the user' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
const validYAMLAsyncAPI = ` | ||
asyncapi: '2.2.0' | ||
info: | ||
title: Account Service | ||
version: 1.0.0 | ||
description: This service is in charge of processing user signups | ||
channels: | ||
user/signedup: | ||
subscribe: | ||
message: | ||
$ref: '#/components/messages/UserSignedUp' | ||
components: | ||
messages: | ||
UserSignedUp: | ||
payload: | ||
type: object | ||
properties: | ||
displayName: | ||
type: string | ||
description: Name of the user | ||
email: | ||
type: string | ||
format: email | ||
description: Email of the user | ||
`; | ||
const invalidJSONAsyncAPI = { | ||
asyncapi: '2.0.0', | ||
info: { | ||
tite: 'My API', // spelled wrong on purpose to throw an error in the test | ||
version: '1.0.0' | ||
}, | ||
channels: {} | ||
}; | ||
|
||
describe('ValidateController', () => { | ||
describe('[POST] /validate', () => { | ||
it('should validate AsyncAPI document in JSON', async () => { | ||
const app = new App([new ValidateController()]); | ||
|
||
return request(app.getServer()) | ||
.post('/validate') | ||
.send({ | ||
asyncapi: validJSONAsyncAPI | ||
}) | ||
.expect(204); | ||
}); | ||
|
||
it('should validate AsyncAPI document in YAML', async () => { | ||
const app = new App([new ValidateController()]); | ||
|
||
return request(app.getServer()) | ||
.post('/validate') | ||
.send({ | ||
asyncapi: validYAMLAsyncAPI | ||
}) | ||
.expect(204); | ||
}); | ||
|
||
it('should throw error when sent an empty document', async () => { | ||
const app = new App([new ValidateController()]); | ||
|
||
return request(app.getServer()) | ||
.post('/validate') | ||
.send({}) | ||
.expect(422, { | ||
type: ProblemException.createType('invalid-request-body'), | ||
title: 'Invalid Request Body', | ||
status: 422, | ||
validationErrors: [ | ||
{ | ||
instancePath: '', | ||
schemaPath: '#/required', | ||
keyword: 'required', | ||
params: { | ||
missingProperty: 'asyncapi' | ||
}, | ||
message: 'must have required property \'asyncapi\'' | ||
} | ||
] | ||
}); | ||
}); | ||
|
||
it('should throw error when sent an invalid AsyncAPI document', async () => { | ||
const app = new App([new ValidateController()]); | ||
|
||
return request(app.getServer()) | ||
.post('/validate') | ||
.send({ | ||
asyncapi: invalidJSONAsyncAPI | ||
}) | ||
.expect(422, { | ||
type: ProblemException.createType('validation-errors'), | ||
title: 'There were errors validating the AsyncAPI document.', | ||
status: 422, | ||
validationErrors: [ | ||
{ | ||
title: '/info should NOT have additional properties', | ||
location: { | ||
jsonPointer: '/info' | ||
} | ||
}, | ||
{ | ||
title: '/info should have required property \'title\'', | ||
location: { | ||
jsonPointer: '/info' | ||
} | ||
} | ||
], | ||
parsedJSON: { | ||
asyncapi: '2.0.0', | ||
info: { | ||
tite: 'My API', | ||
version: '1.0.0' | ||
}, | ||
channels: {} | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
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,34 @@ | ||
import { NextFunction, Request, Response, Router } from 'express'; | ||
|
||
import { Controller } from '../interfaces'; | ||
|
||
import { parse, prepareParserConfig, tryConvertToProblemException } from '../utils/parser'; | ||
|
||
/** | ||
* Controller which exposes the Parser functionality, to validate the AsyncAPI document. | ||
*/ | ||
export class ValidateController implements Controller { | ||
public basepath = '/validate'; | ||
|
||
private async validate(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const options = prepareParserConfig(req); | ||
await parse(req.body?.asyncapi, options); | ||
|
||
res.status(204).end(); | ||
} catch (err: unknown) { | ||
return next(tryConvertToProblemException(err)); | ||
} | ||
} | ||
|
||
public boot(): Router { | ||
const router = Router(); | ||
|
||
router.post( | ||
`${this.basepath}`, | ||
this.validate.bind(this) | ||
); | ||
|
||
return router; | ||
} | ||
} |
Oops, something went wrong.