-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelper_test.go
69 lines (60 loc) · 1.38 KB
/
helper_test.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
package main
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func createDirectory() (string, func()) {
tempdir, _ := os.MkdirTemp(os.TempDir(), "testrun*")
directory := tempdir + string(os.PathSeparator)
return directory, func() {
_ = os.RemoveAll(tempdir)
}
}
func createFile(directory string, filename string, content string) {
_ = os.WriteFile(directory+filename, []byte(content), 0644)
}
func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io.Reader) (*http.Response, string) {
req, err := http.NewRequest(method, ts.URL+path, body)
if err != nil {
t.Fatal(err)
return nil, ""
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
return nil, ""
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
return nil, ""
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
return resp, string(respBody)
}
func checkLogMessage(t *testing.T, wantLogs []string) {
if wantLogs != nil {
entries := hooks.AllEntries()
for _, logMessage := range wantLogs {
found := false
var position int
for id, entry := range entries {
if !found {
if logMessage == entry.Message {
found = true
position = id
}
}
}
assert.Contains(t, entries[position].Message, logMessage)
}
}
hooks.Reset()
}