File: file_test.go

package info (click to toggle)
golang-github-jarcoal-httpmock 1.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 220 kB
  • sloc: makefile: 2
file content (99 lines) | stat: -rw-r--r-- 2,602 bytes parent folder | download
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)
		}
	})
}