/
common_functions.go
107 lines (84 loc) 路 2.27 KB
/
common_functions.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
package testutil
import (
"encoding/json"
"io"
"net/http"
"net/http/httputil"
"testing"
"github.com/Jeffail/gabs/v2"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func dumpResponseIfNot(t *testing.T, success bool, resp *http.Response) {
t.Helper()
if success {
return
}
b, _ := httputil.DumpResponse(resp, true)
t.Log("\n", string(b))
t.FailNow()
}
func generateRandomString() string {
generator := id.NewRandGenerator()
return generator.TraceID().String()
}
func RemoveFieldFromJSONResource(field, jsonResource string) string {
jsonParsed, err := gabs.ParseJSON([]byte(jsonResource))
if err != nil {
panic(err)
}
field = "spec." + field
jf := jsonParsed.Path(field)
if jf == nil {
// field not exists, do nothing
return jsonResource
}
err = jsonParsed.DeleteP(field)
if err != nil {
panic(err)
}
return jsonParsed.String()
}
func removeIDFromJSON(input string) string {
return RemoveFieldFromJSONResource("id", input)
}
func parseJSON(input string) map[string]any {
parsed := map[string]any{}
err := json.Unmarshal([]byte(input), &parsed)
if err != nil {
panic(err)
}
return parsed
}
func extractID(input string) string {
parsed := parseJSON(input)
id := parsed["spec"].(map[string]any)["id"]
if id == nil {
return ""
}
return id.(string)
}
func responseBody(t *testing.T, resp *http.Response) string {
require.NotNil(t, resp.Body)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
return string(body)
}
func responseBodyJSON(t *testing.T, resp *http.Response, ct contentTypeConverter) string {
body := responseBody(t, resp)
jsonBody := ct.toJSON(string(body))
return jsonBody
}
func assertInternalError(t *testing.T, resp *http.Response, ct contentTypeConverter, resourceType, verb string) {
dumpResponseIfNot(t, assert.Equal(t, 500, resp.StatusCode), resp)
jsonBody := responseBodyJSON(t, resp, ct)
// hacky way to get the types we want
bodyValues := struct {
Code int `json:"code"`
Error string `json:"error"`
}{}
json.Unmarshal([]byte(jsonBody), &bodyValues)
dumpResponseIfNot(t, assert.Equal(t, 500, bodyValues.Code), resp)
dumpResponseIfNot(t, assert.Contains(t, bodyValues.Error, "error "+verb+" resource "+resourceType), resp)
}