-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathhelpers.go
More file actions
109 lines (98 loc) · 2.92 KB
/
helpers.go
File metadata and controls
109 lines (98 loc) · 2.92 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
package test
import (
"container/ring"
"encoding/base64"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
)
func loadMockJsons(commaDelimitedFilenames, pathToJsons string) ([][]byte, error) {
jsonFilenames := strings.Split(commaDelimitedFilenames, ",")
var bytesArray [][]byte
for _, jsonFilename := range jsonFilenames {
fullPath := path.Join(pathToJsons, jsonFilename)
jsonfile, err := os.Open(fullPath)
if err != nil {
return nil, err
}
fileBytes, err := io.ReadAll(jsonfile)
if err != nil {
return nil, err
}
if strings.HasSuffix(fullPath, "base64") {
fileBytesAsBase64String := string(fileBytes)
decodedBytes, err := base64.StdEncoding.DecodeString(fileBytesAsBase64String)
if err != nil {
return nil, err
}
bytesArray = append(bytesArray, decodedBytes)
} else {
bytesArray = append(bytesArray, fileBytes)
}
}
return bytesArray, nil
}
var mockServer *httptest.Server
var responseRing *ring.Ring
func mockHTTPResponsesInLoadedFromHelper(jsonfiles, directory string, status int) error {
jsons, err := loadMockJsons(jsonfiles, directory)
if err != nil {
return err
}
responseRing = ring.New(len(jsons))
for _, json := range jsons {
responseRing.Value = json
responseRing = responseRing.Next()
}
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
json := responseRing.Value.([]byte)
if status > 0 {
w.WriteHeader(status)
}
_, err = w.Write(json)
responseRing = responseRing.Next()
}))
return err
}
var receivedMethod string
var receivedPath string
func mockServerRecordingRequestPaths() error {
mockServer = httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
receivedMethod = r.Method
receivedPath = r.URL.String()
}))
return nil
}
func expectTheRequestToBe(expectedMethod, expectedPath string) error {
if !strings.EqualFold(expectedMethod, receivedMethod) {
return fmt.Errorf("method used to access mock server was %s but expected %s", receivedMethod, expectedMethod)
}
return expectThePathUsedToBe(expectedPath)
}
func expectThePathUsedToBe(expectedPath string) error {
if receivedPath != expectedPath {
return fmt.Errorf("path used to access mock server was %s but expected path %s", receivedPath, expectedPath)
}
return nil
}
var globalErrForExamination error
func expectErrorStringToContain(contains string) error {
if contains == "" {
if globalErrForExamination != nil {
return fmt.Errorf("expected no error but error was found: %s", globalErrForExamination.Error())
}
return nil
}
if strings.Contains(globalErrForExamination.Error(), contains) {
return nil
}
return fmt.Errorf("validated error did not contain expected substring, expected substring: %s,"+
"actual error string: %s", contains, globalErrForExamination.Error())
}
func loadResource(filepath string) ([]byte, error) {
return os.ReadFile(path.Join("features", "resources", filepath))
}