-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathfile_test.go
99 lines (84 loc) · 2.54 KB
/
file_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
package httpmock_test
import (
"bytes"
"encoding/json"
"path/filepath"
"strings"
"testing"
"github.com/jarcoal/httpmock"
)
var _ json.Marshaler = httpmock.File("test.json")
func TestFile(t *testing.T) {
dir, cleanup := tmpDir(t)
defer cleanup()
t.Run("Valid JSON file", func(t *testing.T) {
okFile := filepath.Join(dir, "ok.json")
writeFile(t, okFile, []byte(`{ "test": true }`))
encoded, err := json.Marshal(httpmock.File(okFile))
if err != nil {
t.Errorf("json.Marshal(%s) failed: %s", okFile, err)
return
}
got, expected := string(encoded), `{"test":true}`
if got != expected {
t.Errorf("json.Marshal(%s): got=<%s> expected=<%s>", okFile, got, expected)
}
})
t.Run("Nonexistent JSON file", func(t *testing.T) {
nonexistentFile := filepath.Join(dir, "nonexistent.json")
_, err := json.Marshal(httpmock.File(nonexistentFile))
if err == nil {
t.Errorf("json.Marshal(%s) succeeded, but an error is expected!", nonexistentFile)
}
})
t.Run("Invalid JSON file", func(t *testing.T) {
badFile := filepath.Join(dir, "bad.json")
writeFile(t, badFile, []byte(`[123`))
_, err := json.Marshal(httpmock.File(badFile))
if err == nil {
t.Errorf("json.Marshal(%s) succeeded, but an error is expected!", badFile)
}
})
t.Run("Bytes", func(t *testing.T) {
file := filepath.Join(dir, "ok.raw")
content := []byte(`abc123`)
writeFile(t, file, content)
if got := httpmock.File(file).Bytes(); !bytes.Equal(content, got) {
t.Errorf("bytes differ:\n got: %v\n expected: %v", got, content)
}
})
t.Run("Bytes panic", func(t *testing.T) {
nonexistentFile := filepath.Join(dir, "nonexistent.raw")
panicked, mesg := catchPanic(func() {
httpmock.File(nonexistentFile).Bytes()
})
if !panicked {
t.Error("No panic detected")
return
}
if !strings.HasPrefix(mesg, "Cannot read "+nonexistentFile) {
t.Errorf("Bad panic mesg: <%s>", mesg)
}
})
t.Run("String", func(t *testing.T) {
file := filepath.Join(dir, "ok.txt")
content := `abc123`
writeFile(t, file, []byte(content))
if got := httpmock.File(file).String(); got != content {
t.Errorf("strings differ:\n got: <%s>\n expected: <%s>", got, content)
}
})
t.Run("String panic", func(t *testing.T) {
nonexistentFile := filepath.Join(dir, "nonexistent.txt")
panicked, mesg := catchPanic(func() {
httpmock.File(nonexistentFile).String() //nolint: govet
})
if !panicked {
t.Error("No panic detected")
return
}
if !strings.HasPrefix(mesg, "Cannot read "+nonexistentFile) {
t.Errorf("Bad panic mesg: <%s>", mesg)
}
})
}