-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_utils.go
138 lines (113 loc) · 3.39 KB
/
test_utils.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
package test_utils
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"testing"
go_test_utils "github.com/programmfabrik/go-test-utils"
"github.com/sergi/go-diff/diffmatchpatch"
)
var TestServer = go_test_utils.NewTestServer(go_test_utils.Routes{
"/api/v1/session": func(w *http.ResponseWriter, r *http.Request) {
(*w).Write([]byte("{\"token\": \"mock\"}"))
},
"/api/v1/session/authenticate": func(w *http.ResponseWriter, r *http.Request) {
(*w).Write([]byte("{\"authenticated\": \"true\"}"))
},
"/api/v1/settings/purge": func(w *http.ResponseWriter, r *http.Request) {
(*w).WriteHeader(500)
},
"/api/v1/mock": func(w *http.ResponseWriter, r *http.Request) {
(*w).Write([]byte("{\"mocked\": \"true\"}"))
},
})
var TestClient = TestServer.Client()
type LoggingMessage struct {
Level string
Msg string
}
type LoggingRegexAssertion struct {
Level string
MsgRegex *regexp.Regexp
}
type LoggingRegexAssertions []LoggingRegexAssertion
func (assertion LoggingRegexAssertion) String() string {
return fmt.Sprintf("Level: %s MsgRegex: %s", assertion.Level, assertion.MsgRegex.String())
}
func (assertions LoggingRegexAssertions) String() string {
content := ""
for i := range assertions {
content += assertions[i].String()
content += "\n"
}
return content
}
func getMessagesFromLogBuffer(log bytes.Buffer) (res []LoggingMessage) {
logString := log.String()
logLines := strings.Split(logString, "\n")
for _, line := range logLines[:len(logLines)-1] {
replaceRegex := regexp.MustCompile("elapsed=.*$")
line = replaceRegex.ReplaceAllString(line, "")
logRegex := regexp.MustCompile("level=(.*) msg=(.*)$")
match := logRegex.FindStringSubmatch(line)
if len(match) > 0 {
res = append(res, LoggingMessage{Level: match[1], Msg: match[2]})
}
}
return res
}
func assertLoggingMessageEqualsRegex(logMsg LoggingMessage, ass LoggingRegexAssertion) bool {
return (logMsg.Level == ass.Level) && ass.MsgRegex.Match([]byte(logMsg.Msg))
}
func AssertLoggingEqualsRegex(log bytes.Buffer, want []LoggingRegexAssertion) (bool, []string) {
success := true
notMatched := make([]string, 0)
logMessages := getMessagesFromLogBuffer(log)
if len(logMessages) != len(want) {
return false, []string{fmt.Sprintf("Len: Exp %d != %d Got", len(want), len(logMessages))}
}
for i := range logMessages {
equal := assertLoggingMessageEqualsRegex(logMessages[i], want[i])
if !equal {
notMatched = append(
notMatched,
fmt.Sprintf(
"[%s] '%s' != [%s] '%s'",
logMessages[i].Level, logMessages[i].Msg, want[i].Level, want[i].MsgRegex))
}
success = success && equal
}
return success, notMatched
}
// AssertJsonStringEquals checks if two json strings are equal when minified
func AssertJsonStringEquals(t testing.TB, expected, got string) {
var (
expectedJson, gotJson interface{}
expectedMinified, gotMinifed []byte
)
err := json.Unmarshal([]byte(expected), &expectedJson)
if err != nil {
t.Error(err)
}
expectedMinified, err = json.MarshalIndent(expectedJson, "", "")
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal([]byte(got), &gotJson)
if err != nil {
t.Error(err)
}
gotMinifed, err = json.MarshalIndent(gotJson, "", "")
if err != nil {
log.Fatal(err)
}
if string(expectedMinified) != string(gotMinifed) {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(expected, got, false)
t.Error(dmp.DiffPrettyText(diffs))
}
}