-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints.go
123 lines (102 loc) · 2.71 KB
/
endpoints.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
package copper
import (
"strings"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
)
type methods struct {
methods map[string]responses
}
type responses struct {
responses map[string]bool
}
type endpoints struct {
paths map[string]methods
checkInternalServerErrors bool
}
func newEndpoints(model *v3.Document, checkInternalServerErrors bool) *endpoints {
e := &endpoints{
paths: make(map[string]methods),
checkInternalServerErrors: checkInternalServerErrors,
}
e.loadPaths(model)
return e
}
func (e *endpoints) loadPaths(model *v3.Document) {
for path, pathItem := range model.Paths.PathItems.FromOldest() {
e.loadPath(path, pathItem)
}
}
func (e *endpoints) loadPath(path string, i *v3.PathItem) {
if _, ok := e.paths[path]; !ok {
e.paths[path] = methods{
methods: make(map[string]responses),
}
}
for method, op := range i.GetOperations().FromNewest() {
method = strings.ToUpper(method)
if _, ok := e.paths[path].methods[method]; !ok {
e.paths[path].methods[method] = responses{
responses: make(map[string]bool),
}
}
if op.Responses != nil {
for responseCode := range op.Responses.Codes.KeysFromNewest() {
if !e.checkInternalServerErrors && responseCode == "500" {
continue
}
e.paths[path].methods[method].responses[responseCode] = false
}
}
}
}
// Endpoint represents a single coordinate in the endpoints tree.
type Endpoint struct {
Path string
Method string
ResponseCode string
}
func (e *endpoints) responseMap(path, method string) map[string]bool {
p, ok := e.paths[path]
if !ok {
return nil
}
method = strings.ToUpper(method)
m, ok := p.methods[method]
if !ok {
return nil
}
return m.responses
}
func (e *endpoints) IsChecked(path, method, resCode string) bool {
r := e.responseMap(path, method)
return r[resCode]
}
// Unchecked returns a list of Endpoint entries, that all represent a coordinate in the endpoints tree that has not been
// marked as checked.
func (e *endpoints) Unchecked() []Endpoint {
var ends []Endpoint
for path, m := range e.paths {
for method, r := range m.methods {
for resCode, checked := range r.responses {
if !checked {
ends = append(ends, Endpoint{
Path: path,
Method: method,
ResponseCode: resCode,
})
}
}
}
}
return ends
}
// MarkChecked will set an endpoint as checked, but only if it has been previously inserted. Will return false if
// no endpoint is present for the coordinate. Returns true even if the endpoint was previously checked.
func (e *endpoints) MarkChecked(path, method, resCode string) bool {
r := e.responseMap(path, method)
if r == nil {
return false
}
r[resCode] = true
return true
}