-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
81 lines (72 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
/**
* Example for demonstrating hippie-swagger usage, including dereferencing
*
* Usage: mocha example/index.js
*/
var SwaggerParser = require('swagger-parser')
var parser = new SwaggerParser()
var hippie = require('..')
var app = require('./server')
var expect = require('chai').expect
var path = require('path')
var dereferencedSwagger
describe('Example of', function () {
this.timeout(10000) // very large swagger files may take a few seconds to parse
before(function (done) {
// if using mocha, dereferencing can be performed prior during initialization via the delay flag:
// https://mochajs.org/#delayed-root-suite
parser.dereference(path.join(__dirname, './api.swagger.json'), function (err, api) {
if (err) return done(err)
dereferencedSwagger = api
done()
})
})
describe('correct usage', function () {
it('works when the request matches the swagger file', function (done) {
hippie(app, dereferencedSwagger)
.get('/tags/{tagId}')
.pathParams({
tagId: 1
})
.expectStatus(200)
.expectValue('[0].id', 1)
.expectValue('[0].name', 'user')
.expectValue('[1].id', 2)
.expectValue('[1].name', 'store')
.end(done)
})
})
describe('things hippie-swagger will punish you for:', function () {
it('validates paths', function (done) {
try {
hippie(app, dereferencedSwagger)
.get('/undocumented-endpoint')
.end(done)
} catch (ex) {
expect(ex.message).to.equal('Swagger spec does not define path: /undocumented-endpoint')
done()
}
})
it('validates parameters', function (done) {
try {
hippie(app, dereferencedSwagger)
.get('/tags/{tagId}')
.qs({ username: 'not-in-swagger' })
.end(done)
} catch (ex) {
expect(ex.message).to.equal('query parameter not mentioned in swagger spec: "username", available params: tagId')
done()
}
})
it('validates responses', function (done) {
hippie(app, dereferencedSwagger)
.get('/tags/invalidResponse')
.end(function (err) {
expect(err.message).to.match(/Response failed validation/)
done()
})
})
it('validates many other things! See README for the complete list of validations.')
})
})