-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyzer.go
236 lines (194 loc) · 5.5 KB
/
analyzer.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package oaichecker
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/go-openapi/analysis"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/middleware/denco"
"github.com/go-openapi/spec"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/validate"
)
// Analyzer analyze a pair of http.Request/http.Response with the previously
// Specs loaded at initialization.
type Analyzer struct {
analyzer *analysis.Spec
schema *spec.Schema
router *denco.Router
}
// NewAnalyzer instantiate a new Analyzer based on the given Specs.
//
// If the specs is nil, the function panics.
func NewAnalyzer(specs *Specs) *Analyzer {
if specs == nil {
panic("specs is nil")
}
return &Analyzer{
analyzer: specs.document.Analyzer,
schema: specs.document.Schema(),
router: createRouter(specs.document.Analyzer),
}
}
func createRouter(analyzer *analysis.Spec) *denco.Router {
var records []denco.Record
for _, paths := range analyzer.Operations() {
for pathName := range paths {
// Go from the OAI path definition to the denco format:
//
// i.e : "/foo/{variable}/bar" => "/foo/:variable/bar"
dencoPath := strings.Replace(pathName, "{", ":", -1)
dencoPath = strings.Replace(dencoPath, "}", "", -1)
records = append(records, denco.Record{
Key: dencoPath,
Value: pathName,
})
}
}
r := denco.New()
err := r.Build(records)
if err != nil {
panic(err)
}
return r
}
// Analyze the given pair of http.Request/http.Response with the previously
// loaded Specs.
//
// This method checks:
// - If the Operation exists (method / path)
// - The Parameters defined inside the Operation (path / header / body / query / formData)
// - The Response (status / body)
//
// In case of incorrectess, an error is returned.
func (t *Analyzer) Analyze(req *http.Request, res *http.Response) error {
if req == nil {
return errors.New("no request defined")
}
pathName, pathParams, ok := t.router.Lookup(req.URL.Path)
if !ok {
return errors.New("operation not defined inside the specs")
}
operation, ok := t.analyzer.OperationFor(req.Method, pathName.(string))
if !ok {
return errors.New("operation not defined inside the specs")
}
for _, param := range operation.Parameters {
var err error
switch param.In {
case "path":
err = t.validatePathParameter(pathParams, ¶m)
case "header":
err = t.validateHeaderParameter(req, ¶m)
case "body":
err = t.validateBodyParameter(req, ¶m)
case "query":
err = t.validateQueryParameter(req, ¶m)
case "formData":
err = t.validateFormDataParameter(req, ¶m)
}
if err != nil {
return err
}
}
err := t.validateResponse(res, operation.Responses)
return err
}
func (t *Analyzer) validateResponse(res *http.Response, resSpec *spec.Responses) error {
for status, response := range resSpec.StatusCodeResponses {
if status == res.StatusCode {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
res.Body = ioutil.NopCloser(bytes.NewReader(body))
if response.ResponseProps.Schema == nil {
if len(body) > 0 {
return fmt.Errorf("validation failure list:\nno response body defined inside the specs but have %q", body)
}
return nil
}
var input interface{}
err = json.Unmarshal(body, &input)
if err != nil {
return fmt.Errorf("validation failure list:\nfailed to parse response body: %s", err)
}
err = validate.AgainstSchema(response.Schema, input, strfmt.Default)
if err != nil {
return err
}
return nil
}
}
return fmt.Errorf("validation failure list:\nresponse status %s not defined inside the specs", res.Status)
}
func (t *Analyzer) validateBodyParameter(req *http.Request, param *spec.Parameter) error {
bodyReader, err := req.GetBody()
if err != nil {
return err
}
input := map[string]interface{}{}
err = json.NewDecoder(bodyReader).Decode(&input)
if err != nil {
return err
}
err = validate.AgainstSchema(param.Schema, input, strfmt.Default)
if err != nil {
return err
}
return nil
}
func (t *Analyzer) validateHeaderParameter(req *http.Request, param *spec.Parameter) error {
errs := validate.NewParamValidator(param, strfmt.Default).Validate(req.Header.Get(param.Name))
if errs != nil {
return errs.AsError()
}
return nil
}
func (t *Analyzer) validateQueryParameter(req *http.Request, param *spec.Parameter) error {
query := req.URL.Query()
errs := validate.NewParamValidator(param, strfmt.Default).Validate(query[param.Name])
if errs != nil {
return errs.AsError()
}
return nil
}
func (t *Analyzer) validatePathParameter(pathParams denco.Params, param *spec.Parameter) error {
var res interface{} = pathParams.Get(param.Name)
if param.Type == "integer" {
nParam, err := strconv.Atoi(pathParams.Get(param.Name))
if err == nil {
res = nParam
}
}
errs := validate.NewParamValidator(param, strfmt.Default).Validate(res)
if errs != nil {
return errs.AsError()
}
return nil
}
func (t *Analyzer) validateFormDataParameter(req *http.Request, param *spec.Parameter) error {
var res interface{}
if param.Type == "file" {
data, header, err := req.FormFile(param.Name)
if err != nil && param.ParamProps.Required {
return fmt.Errorf("validation failure list:\n%s in formData is required", param.Name)
}
res = runtime.File{
Data: data,
Header: header,
}
} else {
res = req.PostFormValue(param.Name)
}
errs := validate.NewParamValidator(param, strfmt.Default).Validate(res)
if errs != nil {
return errs.AsError()
}
return nil
}