-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathmap_test.go
More file actions
150 lines (140 loc) · 3.32 KB
/
pathmap_test.go
File metadata and controls
150 lines (140 loc) · 3.32 KB
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
package main
import (
"fmt"
"os"
"strings"
"testing"
"github.com/google/uuid"
)
func TestAddSwaggerToPathMap(t *testing.T) {
dir, err := os.Getwd()
filepath := fmt.Sprintf("file:///%s/PetstoreSwagger.json", strings.Replace(dir, "\\", "/", -1))
c := Config{
Services: []ServiceEntry{
ServiceEntry{
RoutePath: "petstore",
Swagger: filepath,
},
},
}
pm := NewPathMap()
err = pm.ReadSwagger(c)
AssertSuccess(t, err)
CheckGold(t, "PathMapFromSwaggerTest.json", pm.JSON())
}
func CheckTransactionLogEntry(t *testing.T) {
dir, err := os.Getwd()
filepath := fmt.Sprintf("file:///%s/coverage-report.txt", strings.Replace(dir, "\\", "/", -1))
lr := NewTransactionLogReader()
err = lr.SetLogURL(filepath)
AssertSuccess(t, err)
pm := NewPathMap()
lel, err := lr.GetLogEntries()
AssertSuccess(t, err)
for _, entry := range lel {
pm.CheckRequestLogEntry(entry)
}
CheckGold(t, "PathMapFromLogTest.json", pm.JSON())
}
func GenerateAccessLogFromSwagger(t *testing.T) {
dir, err := os.Getwd()
AssertSuccess(t, err)
entries := [][7]string{}
sumo := true
domain := "https://127.0.0.1:8081"
if sumo {
domain = ""
}
GenerateEntries(t, &entries, dir, domain)
f, err := os.Create("temp/petstore-log.txt")
AssertSuccess(t, err)
defer f.Close()
if sumo {
f.WriteString("API,Response Code\n")
} else {
f.WriteString("duration(ms)\tstart-time\tend-time\tmethod\turl\tbody\tresponse\n")
}
for _, line := range entries {
s := fmt.Sprintf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n", line[0], line[1], line[2], line[3], line[4], line[5], line[6])
if sumo {
s = fmt.Sprintf("%s %s,%s\n", line[3], line[4], line[6])
}
f.WriteString(s)
}
}
func GenerateEntries(t *testing.T, entries *[][7]string, dir string, domain string) {
filepath := fmt.Sprintf("file:///%s/PetStoreSwagger.json", strings.Replace(dir, "\\", "/", -1))
c := Config{
Services: []ServiceEntry{
ServiceEntry{
RoutePath: "petstore",
Swagger: filepath,
},
},
}
pm := NewPathMap()
err := pm.ReadSwagger(c)
AssertSuccess(t, err)
for _, pi := range pm.Services["petstore"].PathItems {
WalkPathItems(entries, pi, fmt.Sprintf("%s/petstore", domain))
}
}
func WalkPathItems(entries *[][7]string, pi *PathItem, path string) {
key := pi.MapKey()
if pi.IsParameter() {
id, _ := uuid.NewUUID()
key = id.String()
}
cpath := fmt.Sprintf("%s/%s", path, key)
if pi.Verbs != nil {
for _, verb := range pi.Verbs {
if len(verb.Responses) > 0 {
for _, response := range verb.Responses {
entry := [7]string{
"0",
"0",
"0",
verb.Name,
cpath,
"undefined",
response.Response,
}
*entries = append(*entries, entry)
}
} else {
entry := [7]string{
"0",
"0",
"0",
verb.Name,
cpath,
"undefined",
"200",
}
*entries = append(*entries, entry)
}
if len(verb.QueryParameters) > 0 {
i := 0
query := []string{}
for _, param := range verb.QueryParameters {
query = append(query, fmt.Sprintf("%s=val%d", param.Key, i))
i++
}
cpath = fmt.Sprintf("%s?%s", cpath, strings.Join(query, "&"))
entry := [7]string{
"0",
"0",
"0",
verb.Name,
cpath,
"undefined",
"200",
}
*entries = append(*entries, entry)
}
}
}
for _, child := range pi.PathItems {
WalkPathItems(entries, child, cpath)
}
}