-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_collection_test.go
97 lines (88 loc) · 2 KB
/
version_collection_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
package caddy_wasm
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"testing/fstest"
)
type testHandler struct {
requests []*http.Request
}
func (t *testHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
t.requests = append(t.requests, req)
res.WriteHeader(200)
res.Write([]byte("Ok"))
}
func TestUrlVersions(t *testing.T) {
handler := &testHandler{}
server := httptest.NewServer(handler)
collection := ForURL(server.URL)
v, e := collection.GetVersions()
if e != nil {
t.Errorf("Unexpected error: %v", e.Error())
}
if len(v) != 1 {
t.Errorf("Unexpected versions: %v", v)
}
d, e := collection.GetWebAssembly(v[0])
if e != nil {
t.Errorf("Unexpected error: %v", e.Error())
}
if string(d) != "Ok" {
t.Errorf("Unexpected data: %v", string(d))
}
}
func TestFileVersions(t *testing.T) {
m := fstest.MapFS{}
m["some/dir/foo.wasm"] = &fstest.MapFile{Data: []byte("foo")}
m["some/dir/bar.wasm"] = &fstest.MapFile{Data: []byte("bar")}
f := &fileSystemVersionCollection{
m,
"some/dir/",
"*.wasm",
}
v, e := f.GetVersions()
if e != nil {
t.Errorf("Unexpected error: %v", e.Error())
}
tests := []struct {
v string
d string
}{
{"foo.wasm", "foo"},
{"bar.wasm", "bar"},
}
if len(v) != len(tests) {
t.Errorf("Unexpected versions: %v", v)
}
for _, test := range tests {
d, e := f.GetWebAssembly(test.v)
if e != nil {
t.Errorf("Unexpected error: %v", e.Error())
}
if string(d) != test.d {
t.Errorf("Unexpected data: %v", string(d))
}
}
}
func TestGithub(t *testing.T) {
v, e := ForGithubRepository("brendandburns", "caddy-wasm", "tinygo.wasm")
if e != nil {
t.Errorf("unexpected error: %v", e.Error())
}
vs, e := v.GetVersions()
if e != nil {
t.Errorf("unexpected error: %v", e.Error())
}
if !reflect.DeepEqual(vs, []string{"0.0.1"}) {
t.Errorf("unexpected output: %v", vs)
}
data, e := v.GetWebAssembly("0.0.1")
if e != nil {
t.Errorf("unexpected error: %v", e.Error())
}
if len(data) == 0 {
t.Errorf("unexpected data: %v", data)
}
}