-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_test.go
175 lines (161 loc) · 4.38 KB
/
reader_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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package mimic
import (
"crypto/md5"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func absPathFinder(dirName string, otherPaths ...string) (string, error) {
fp, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("error with with getting working directory for absolute path: %v", err)
}
if !strings.Contains(fp, dirName) {
return "", fmt.Errorf("filepath %s does not contain '%s'; cannot get path to test data", fp, dirName)
}
split := strings.SplitAfterN(fp, dirName, 2)
return filepath.Join(append(split[:1], otherPaths...)...), nil
}
func TestLoad(t *testing.T) {
var r reader
err := r.load(filepath.Join(dataPath, "basic_test.json"))
require.NoError(t, err)
key, err := request{
Method: http.MethodGet,
URL: "http://url.test",
}.key()
assert.Equal(t,
reader{
cache: map[[md5.Size]byte]accessor{
key: &responses{
list: []response{{StatusCode: 200}},
},
},
}, r)
}
func TestLoadBody(t *testing.T) {
var r reader
err := r.load(filepath.Join(dataPath, "body.json"))
require.NoError(t, err)
key, err := request{
Method: http.MethodPost,
URL: "http://test.body",
Body: map[string]interface{}{
"boolean": true,
"key": "value",
"list": []int{1, 2, 4},
},
}.key()
assert.Equal(t,
reader{
cache: map[[md5.Size]byte]accessor{
key: &responses{
list: []response{{
StatusCode: 200,
Body: map[string]interface{}{
"status": "success",
},
}},
},
},
}, r)
}
func TestLoadMultipleElements(t *testing.T) {
var r reader
err := r.load(filepath.Join(dataPath, "multiple_elements.json"))
require.NoError(t, err)
key, err := request{
Method: http.MethodGet,
URL: "http://multi",
}.key()
otherKey, err := request{
Method: http.MethodGet,
URL: "http://something.else",
}.key()
assert.Equal(t,
reader{
cache: map[[md5.Size]byte]accessor{
key: &responses{
list: []response{{
StatusCode: 200,
}, {
StatusCode: 500,
}},
},
otherKey: &responses{
list: []response{{StatusCode: 404}},
},
},
}, r)
}
func TestLayerFiles(t *testing.T) {
var r reader
err := r.load(filepath.Join(dataPath, "multiple_elements.json"))
require.NoError(t, err)
err = r.load(filepath.Join(dataPath, "layering.json"))
require.NoError(t, err)
key, err := request{
Method: http.MethodGet,
URL: "http://multi",
}.key()
require.NoError(t, err)
otherKey, err := request{
Method: http.MethodGet,
URL: "http://something.else",
}.key()
require.NoError(t, err)
assert.Equal(t,
reader{
cache: map[[md5.Size]byte]accessor{
key: &responses{
list: []response{{StatusCode: 201}},
},
otherKey: &responses{
list: []response{{StatusCode: 404}},
},
},
}, r)
}
func TestLoadBadPath(t *testing.T) {
var r reader
err := r.load(filepath.Join(dataPath, "non_existant_file.json"))
require.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "error opening mimic cache file"),
fmt.Sprintf("error was masked: %s", err.Error()))
}
func TestBadFormatting(t *testing.T) {
tts := []struct {
errorMessage, filename, name string
}{
{
errorMessage: "error decoding mimic cache file: error unmarshaling mock cache: json: cannot unmarshal string into Go struct field response.status_code of type int",
filename: "bad_nested_formatting.json",
name: "nested object error correctly reported",
}, {
errorMessage: "error decoding mimic cache file: error unmarshaling mock cache: json: cannot unmarshal number into Go struct field request.url of type string",
filename: "bad_object_formatting.json",
name: "single object error correctly reported",
}, {
errorMessage: "error decoding mimic cache file: error unmarshaling mock cache: json: cannot unmarshal string into Go value of type mimic.entry",
filename: "bad_list_formatting.json",
name: "list error correctly reported",
}, {
errorMessage: "error decoding mimic cache file: invalid character 'l' looking for beginning of value",
filename: "not_json.txt",
name: "completely invalid json correctly reports",
},
}
for _, tt := range tts {
t.Run(tt.name, func(t *testing.T) {
var r reader
err := r.load(filepath.Join(dataPath, tt.filename))
require.Error(t, err)
assert.Equal(t, tt.errorMessage, err.Error())
})
}
}