forked from goproxy/goproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cacher_test.go
87 lines (74 loc) · 1.77 KB
/
cacher_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
package goproxy
import (
"context"
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
type errorReadSeeker struct{}
func (errorReadSeeker) Read([]byte) (int, error) {
return 0, errors.New("cannot read")
}
func (errorReadSeeker) Seek(int64, int) (int64, error) {
return 0, errors.New("cannot seek")
}
func TestDirCacher(t *testing.T) {
tempDir, err := ioutil.TempDir("", "goproxy.TestDirCacher")
if err != nil {
t.Fatalf("unexpected error %q", err)
}
defer os.RemoveAll(tempDir)
dirCacher := DirCacher(tempDir)
if rc, err := dirCacher.Get(
context.Background(),
"a/b/c",
); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("got error %q, want error %q", err, os.ErrNotExist)
} else if rc != nil {
t.Errorf("got %v, want nil", rc)
}
if err := dirCacher.Put(
context.Background(),
"a/b/c",
strings.NewReader("foobar"),
); err != nil {
t.Fatalf("unexpected error %q", err)
}
rc, err := dirCacher.Get(context.Background(), "a/b/c")
if err != nil {
t.Fatalf("unexpected error %q", err)
} else if rc == nil {
t.Fatal("unexpected nil")
}
if b, err := ioutil.ReadAll(rc); err != nil {
t.Fatalf("unexpected error %q", err)
} else if want := "foobar"; string(b) != want {
t.Errorf("got %q, want %q", b, want)
}
if err := rc.Close(); err != nil {
t.Fatalf("unexpected error %q", err)
}
if err := dirCacher.Put(
context.Background(),
"d/e/f",
&errorReadSeeker{},
); err == nil {
t.Fatal("expected error")
} else if got, want := err.Error(), "cannot read"; got != want {
t.Errorf("got %q, want %q", got, want)
}
dirCacher = DirCacher(filepath.Join(
tempDir,
filepath.FromSlash("a/b/c"),
))
if err := dirCacher.Put(
context.Background(),
"d/e/f",
strings.NewReader("foobar"),
); err == nil {
t.Fatal("expected error")
}
}