-
-
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
1 parent
4dac650
commit cea6d1a
Showing
7 changed files
with
229 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Request, Response, Router } from 'express'; | ||
import { AsyncAPIDocument } from '@asyncapi/parser'; | ||
|
||
import { validationMiddleware } from '../middlewares/validation.middleware'; | ||
|
||
import { Controller} from '../interfaces'; | ||
|
||
/** | ||
* Controller which exposes the Parser functionality, to parse the AsyncAPI document. | ||
*/ | ||
export class ParseController implements Controller { | ||
public basepath = '/parse'; | ||
|
||
private async parse(req: Request, res: Response) { | ||
const stringified = AsyncAPIDocument.stringify(req.asyncapi?.parsedDocument); | ||
res.status(200).json({ | ||
parsed: stringified, | ||
}); | ||
} | ||
|
||
public async boot(): Promise<Router> { | ||
const router = Router(); | ||
|
||
router.post( | ||
`${this.basepath}`, | ||
await validationMiddleware({ | ||
path: this.basepath, | ||
method: 'post', | ||
documents: ['asyncapi'], | ||
}), | ||
this.parse.bind(this) | ||
); | ||
|
||
return router; | ||
} | ||
} |
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,79 @@ | ||
import request from 'supertest'; | ||
|
||
import { App } from '../../app'; | ||
import { ProblemException } from '../../exceptions/problem.exception'; | ||
|
||
import { ParseController } from '../parse.controller'; | ||
|
||
const validJSONAsyncAPI = { | ||
asyncapi: '2.0.0', | ||
info: { | ||
title: 'My API', | ||
version: '1.0.0' | ||
}, | ||
channels: {} | ||
}; | ||
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('ParseController', () => { | ||
describe('[POST] /parse', () => { | ||
it('should return stringified AsyncAPI document', async () => { | ||
const app = new App([new ParseController()]); | ||
await app.init(); | ||
|
||
return request(app.getServer()) | ||
.post('/v1/parse') | ||
.send({ | ||
asyncapi: validJSONAsyncAPI | ||
}) | ||
.expect(200, { | ||
parsed: '{"asyncapi":"2.0.0","info":{"title":"My API","version":"1.0.0"},"channels":{},"x-parser-spec-parsed":true,"x-parser-spec-stringified":true}', | ||
}); | ||
}); | ||
|
||
it('should throw error when sent an invalid AsyncAPI document', async () => { | ||
const app = new App([new ParseController()]); | ||
await app.init(); | ||
|
||
return request(app.getServer()) | ||
.post('/v1/parse') | ||
.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
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
Oops, something went wrong.