package fetcher import ( "encoding/base64" "fmt" "io/ioutil" "net/http" "os" "testing" "github.com/gobuffalo/httptest" "github.com/julienschmidt/httprouter" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestFetcher(t *testing.T) { router := httprouter.New() router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { _, _ = w.Write([]byte(`{"foo":"bar"}`)) }) ts := httptest.NewServer(router) t.Cleanup(ts.Close) file, err := ioutil.TempFile(os.TempDir(), "source.*.json") require.NoError(t, err) _, err = file.WriteString(`{"foo":"baz"}`) require.NoError(t, err) require.NoError(t, file.Close()) for fc, fetcher := range []*Fetcher{ NewFetcher(WithClient(ts.Client())), NewFetcher(), } { for k, tc := range []struct { source string expect string }{ { source: "base64://" + base64.StdEncoding.EncodeToString([]byte(`{"foo":"zab"}`)), expect: `{"foo":"zab"}`, }, { source: "file://" + file.Name(), expect: `{"foo":"baz"}`, }, { source: ts.URL, expect: `{"foo":"bar"}`, }, } { t.Run(fmt.Sprintf("config=%d/case=%d", fc, k), func(t *testing.T) { actual, err := fetcher.Fetch(tc.source) require.NoError(t, err) assert.JSONEq(t, tc.expect, actual.String()) }) } } }