-
Notifications
You must be signed in to change notification settings - Fork 103
/
file_test.go
76 lines (59 loc) · 2 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
package httpmock_test
import (
"encoding/json"
"path/filepath"
"testing"
"github.com/maxatome/go-testdeep/td"
"github.com/jarcoal/httpmock"
)
var _ json.Marshaler = httpmock.File("test.json")
func TestFile(t *testing.T) {
assert := td.Assert(t)
dir, cleanup := tmpDir(assert)
defer cleanup()
assert.Run("Valid JSON file", func(assert *td.T) {
okFile := filepath.Join(dir, "ok.json")
writeFile(assert, okFile, []byte(`{ "test": true }`))
encoded, err := json.Marshal(httpmock.File(okFile))
if !assert.CmpNoError(err, "json.Marshal(%s)", okFile) {
return
}
assert.String(encoded, `{"test":true}`)
})
assert.Run("Nonexistent JSON file", func(assert *td.T) {
nonexistentFile := filepath.Join(dir, "nonexistent.json")
_, err := json.Marshal(httpmock.File(nonexistentFile))
assert.CmpError(err, "json.Marshal(%s), error expected", nonexistentFile)
})
assert.Run("Invalid JSON file", func(assert *td.T) {
badFile := filepath.Join(dir, "bad.json")
writeFile(assert, badFile, []byte(`[123`))
_, err := json.Marshal(httpmock.File(badFile))
assert.CmpError(err, "json.Marshal(%s), error expected", badFile)
})
assert.Run("Bytes", func(assert *td.T) {
file := filepath.Join(dir, "ok.raw")
content := []byte(`abc123`)
writeFile(assert, file, content)
assert.Cmp(httpmock.File(file).Bytes(), content)
})
assert.Run("Bytes panic", func(assert *td.T) {
nonexistentFile := filepath.Join(dir, "nonexistent.raw")
assert.CmpPanic(func() { httpmock.File(nonexistentFile).Bytes() },
td.HasPrefix("Cannot read "+nonexistentFile))
})
assert.Run("String", func(assert *td.T) {
file := filepath.Join(dir, "ok.txt")
content := `abc123`
writeFile(assert, file, []byte(content))
assert.Cmp(httpmock.File(file).String(), content)
})
assert.Run("String panic", func(assert *td.T) {
nonexistentFile := filepath.Join(dir, "nonexistent.txt")
assert.CmpPanic(
func() {
httpmock.File(nonexistentFile).String() //nolint: govet
},
td.HasPrefix("Cannot read "+nonexistentFile))
})
}