-
-
Notifications
You must be signed in to change notification settings - Fork 211
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
Carmine DiMascio
committed
Sep 15, 2019
1 parent
d7fd2b7
commit 7c27713
Showing
9 changed files
with
218 additions
and
177 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
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 |
---|---|---|
@@ -1,167 +1,52 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import * as express from 'express'; | ||
import * as jsyaml from 'js-yaml'; | ||
import { expect } from 'chai'; | ||
import * as request from 'supertest'; | ||
import { ResponseValidator } from '../src/middlewares/openapi.response.validator'; | ||
import { createApp } from './common/app'; | ||
|
||
const packageJson = require('../package.json'); | ||
const apiSpecPath = path.join('test', 'resources', 'response.validation.yaml'); | ||
const apiSpec = jsyaml.safeLoad(fs.readFileSync(apiSpecPath, 'utf8')); | ||
|
||
describe(packageJson.name, () => { | ||
let app = null; | ||
let basePath = null; | ||
|
||
before(async () => { | ||
// Set up the express app | ||
app = await createApp({ apiSpec, coerceTypes: false }, 3005, false); | ||
basePath = app.basePath; | ||
app.use( | ||
`${basePath}`, | ||
express.Router().get(`/pets/rejectadditionalProps`, (req, res) => | ||
res.json([ | ||
{ | ||
id: 1, | ||
type: 'test', | ||
tag: 'woah', | ||
}, | ||
]), | ||
), | ||
app = await createApp( | ||
{ apiSpec: apiSpecPath, validateResponses: true }, | ||
3005, | ||
false, | ||
); | ||
basePath = app.basePath; | ||
app.get(`${basePath}/pets`, (req, res) => { | ||
let json = {}; | ||
if ((req.query.mode = 'bad_type')) { | ||
json = [{ id: 'bad_id', name: 'name', tag: 'tag' }]; | ||
} | ||
return res.json(json); | ||
}); | ||
// Register error handler | ||
app.use((err, req, res, next) => { | ||
res.status(err.status || 500).json({ | ||
message: err.message, | ||
code: err.status, | ||
}); | ||
}); | ||
}); | ||
|
||
after(() => { | ||
app.server.close(); | ||
}); | ||
|
||
it('should fail if response does not match', async () => | ||
it('should fail if response field has a value of incorrect type', async () => | ||
request(app) | ||
.get(`${basePath}/rejectadditionalProps`) | ||
.get(`${basePath}/pets?mode=bad_type`) | ||
.expect(500) | ||
.then((r: any) => { | ||
expect(r.body.error).to.be.not.null; | ||
expect(r.body.message).to.contain('should be integer'); | ||
expect(r.body) | ||
.to.have.property('code') | ||
.that.equals(500); | ||
})); | ||
|
||
// TODO | ||
it.only('should always return valid for non-JSON responses', async () => { | ||
const v = new ResponseValidator(apiSpec); | ||
const responses = petsResponseSchema(); | ||
const statusCode = 200; | ||
|
||
try { | ||
const validators = v._getOrBuildValidator(null, responses); | ||
v._validate({ | ||
validators, | ||
body: { id: 1, name: 'test', tag: 'tag' }, | ||
statusCode, | ||
}); | ||
} catch (e) { | ||
expect(e).to.be.not.null; | ||
expect(e.message).to.contain('should be array'); | ||
} | ||
}); | ||
|
||
// TODO | ||
it.only('should validate the error object', async () => {}); | ||
|
||
it.only('should return an error if field type is invalid', async () => { | ||
const v = new ResponseValidator(apiSpec); | ||
const responses = petsResponseSchema(); | ||
const statusCode = 200; | ||
|
||
const validators = v._getOrBuildValidator(null, responses); | ||
try { | ||
v._validate({ | ||
validators, | ||
body: [{ id: 'bad-id', name: 'test', tag: 'tag' }], | ||
statusCode, | ||
}); | ||
} catch (e) { | ||
expect(e).to.be.not.null; | ||
expect(e.message).to.contain('should be integer'); | ||
expect(e.message).to.not.contain('additional properties'); | ||
} | ||
|
||
try { | ||
v._validate({ | ||
validators, | ||
body: { id: 1, name: 'test', tag: 'tag' }, | ||
statusCode, | ||
}); | ||
} catch (e) { | ||
expect(e).to.be.not.null; | ||
expect(e.message).to.contain('should be array'); | ||
} | ||
|
||
try { | ||
v._validate({ | ||
validators, | ||
body: [{ id: 1, name: [], tag: 'tag' }], | ||
statusCode, | ||
}); | ||
} catch (e) { | ||
expect(e).to.be.not.null; | ||
expect(e.message).to.contain('should be string'); | ||
} | ||
}); | ||
|
||
// TODO may not be possible to fix | ||
// https://github.com/epoberezkin/ajv/issues/837 | ||
it.skip('should if additional properties are provided when set false', async () => { | ||
const v = new ResponseValidator(apiSpec); | ||
const responses = petsResponseSchema(); | ||
const statusCode = 200; | ||
const body = [ | ||
{ | ||
id: 10, | ||
name: 'test', | ||
tag: 'tag', | ||
additionalProp: 'test', | ||
}, | ||
]; | ||
try { | ||
const validators = v._getOrBuildValidator(null, responses); | ||
v._validate({ validators, body, statusCode }); | ||
expect('here').to.be.null; | ||
} catch (e) { | ||
// TODO include params.additionalProperty: value in error message | ||
// TODO maybe params should be in the response | ||
expect(e.message).to.contain('should NOT have additional properties'); | ||
expect(e.status).to.equal(500); | ||
expect(e.errors[0].message).to.contain( | ||
'should NOT have additional properties', | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
function petsResponseSchema() { | ||
return { | ||
'200': { | ||
description: 'pet response', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'array', | ||
items: { | ||
$ref: '#/components/schemas/Pet', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
default: { | ||
description: 'unexpected error', | ||
content: { | ||
'application/json': { | ||
schema: { | ||
$ref: '#/components/schemas/Error', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} |
Oops, something went wrong.