Skip to content

Commit

Permalink
WIP: add unit test for http fileloader
Browse files Browse the repository at this point in the history
  • Loading branch information
yujunz committed Feb 28, 2020
1 parent ea3e87c commit 0d900fa
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions api/loader/fileloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package loader

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -42,6 +44,62 @@ var testCases = []testData{
},
}

// TestLoaderHTTP test http file loader
func TestLoaderHTTP(t *testing.T) {
var testCasesFile = []testData{
{
path: "http/file.yaml",
expectedContent: "file content",
},
// {
// path: "httpsnotreal://example.com/resource.yaml",
// expectedContent: "invalid",
// }, bhy678
}

l1 := NewFileLoaderAtRoot(MakeFakeFs(testCasesFile))
if "/" != l1.Root() {
t.Fatalf("incorrect root: '%s'\n", l1.Root())
}
for _, x := range testCases {
b, err := l1.Load(x.path)
if err != nil {
t.Fatalf("unexpected load error: %v", err)
}
if !reflect.DeepEqual([]byte(x.expectedContent), b) {
t.Fatalf("in load expected %s, but got %s", x.expectedContent, b)
}
}

httpPath := `/some/path`
httpContent := `http content`

server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
equals(t, req.URL.String(), httpPath)
rw.Write([]byte(httpContent))
}))

b, err := l2.Load(server.URL + httpPath)
if err != nil {
t.Fatalf("unexpected load error: %v", err)
}
if !reflect.DeepEqual([]byte(httpContent), b) {
t.Fatalf("in load expected %s, but got %s", httpContent, b)
}

// https: //hassansin.github.io/Unit-Testing-http-client-in-Go
// var testCasesHTTP = []testData{
// {
// path: "http://example.com/resource.yaml",
// expectedContent: "http content",
// },
// {
// path: "https://example.com/resource.yaml",
// expectedContent: "https content",
// }
// }
}

func MakeFakeFs(td []testData) filesys.FileSystem {
fSys := filesys.MakeFsInMemory()
for _, x := range td {
Expand Down

0 comments on commit 0d900fa

Please sign in to comment.