Skip to content

Commit

Permalink
Merge pull request #64 from wwade/main
Browse files Browse the repository at this point in the history
Add unit test for repo.getterImpl (and file:// scheme support)
  • Loading branch information
rmohr authored Oct 6, 2023
2 parents 09e65b6 + 0ec2d49 commit 6ce8d7f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/repo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ go_library(

go_test(
name = "repo_test",
srcs = ["repo_test.go"],
srcs = [
"fetch_test.go",
"repo_test.go",
],
data = glob(["testdata/**"]),
embed = [":repo"],
deps = ["//pkg/api"],
Expand Down
63 changes: 63 additions & 0 deletions pkg/repo/fetch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package repo

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
)

func TestGetter(t *testing.T) {
content := []byte("my file contents\n")
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Fatalf("wrong method, %v instead of %v", r.Method, http.MethodGet)
}
n, err := rw.Write(content)
if err != nil {
t.Fatal("write content: ", err)
}
if n != len(content) {
t.Fatalf("short write, %v instead of %v", n, len(content))
}
}))
defer s.Close()

localFile := path.Join(t.TempDir(), "contentfile")
if err := os.WriteFile(localFile, content, os.ModePerm); err != nil {
t.Fatalf("WriteFile %v failed: %v", localFile, err)
}

for _, tc := range []struct {
name string
url string
}{
{
name: "HTTP",
url: s.URL,
},
{
name: "local",
url: "file://" + localFile,
},
} {
t.Run(tc.name, func(t *testing.T) {
t.Logf("Getter.Get %v", tc.url)
resp, err := Getter(&getterImpl{}).Get(tc.url)
if err != nil {
t.Fatalf("Get %v: %v", tc.url, err)
}
defer resp.Body.Close()
recv, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Read response failed: %v", err)
}
if !bytes.Equal(recv, content) {
t.Fatalf("Read wrong content, %v instead of %v", recv, content)
}
})
}
}

0 comments on commit 6ce8d7f

Please sign in to comment.