forked from gemaraproj/go-gemara
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuri_test.go
More file actions
91 lines (74 loc) · 2.27 KB
/
uri_test.go
File metadata and controls
91 lines (74 loc) · 2.27 KB
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
// SPDX-License-Identifier: Apache-2.0
package fetcher
import (
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestURI_FileScheme(t *testing.T) {
tmp := t.TempDir()
p := filepath.Join(tmp, "data.yaml")
require.NoError(t, os.WriteFile(p, []byte("ok: true\n"), 0600))
f := &URI{}
rc, err := f.Fetch(context.Background(), "file://"+p)
require.NoError(t, err)
defer rc.Close() //nolint:errcheck
data, err := io.ReadAll(rc)
require.NoError(t, err)
assert.Equal(t, "ok: true\n", string(data))
}
func TestURI_HTTPScheme(t *testing.T) {
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("remote: true\n"))
}))
defer srv.Close()
f := &URI{Client: srv.Client()}
rc, err := f.Fetch(context.Background(), srv.URL+"/remote.yaml")
require.NoError(t, err)
defer rc.Close() //nolint:errcheck
data, err := io.ReadAll(rc)
require.NoError(t, err)
assert.Equal(t, "remote: true\n", string(data))
}
func TestURI_UnsupportedScheme(t *testing.T) {
f := &URI{}
_, err := f.Fetch(context.Background(), "ftp://example.com/file.yaml")
require.Error(t, err)
assert.Contains(t, err.Error(), "unsupported URI scheme")
}
func TestURI_BarePath_Absolute(t *testing.T) {
tmp := t.TempDir()
p := filepath.Join(tmp, "data.yaml")
require.NoError(t, os.WriteFile(p, []byte("ok: true\n"), 0600))
f := &URI{}
rc, err := f.Fetch(context.Background(), p)
require.NoError(t, err)
defer rc.Close() //nolint:errcheck
data, err := io.ReadAll(rc)
require.NoError(t, err)
assert.Equal(t, "ok: true\n", string(data))
}
func TestURI_BarePath_Relative(t *testing.T) {
tmp := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(tmp, "data.yaml"), []byte("ok: true\n"), 0600))
t.Chdir(tmp)
f := &URI{}
rc, err := f.Fetch(context.Background(), "./data.yaml")
require.NoError(t, err)
defer rc.Close() //nolint:errcheck
data, err := io.ReadAll(rc)
require.NoError(t, err)
assert.Equal(t, "ok: true\n", string(data))
}
func TestURI_TypoScheme(t *testing.T) {
f := &URI{}
_, err := f.Fetch(context.Background(), "htps://example.com/file.yaml")
require.Error(t, err)
assert.Contains(t, err.Error(), "unsupported URI scheme")
}