From fa46e5a7ebbfbd6bf1438bfdae3f90d16ba2f5c1 Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Fri, 7 Jan 2022 12:23:37 +0000 Subject: [PATCH] Fix broken unit test in 404 use-case Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- types/function_list_builder.go | 3 ++- types/function_list_builder_test.go | 16 +++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/types/function_list_builder.go b/types/function_list_builder.go index 7388a0a..2037737 100644 --- a/types/function_list_builder.go +++ b/types/function_list_builder.go @@ -80,6 +80,7 @@ func (s *FunctionLookupBuilder) getNamespaces() ([]string, error) { if res.Body != nil { defer res.Body.Close() } + bytesOut, err := ioutil.ReadAll(res.Body) if err != nil { return namespaces, err @@ -94,7 +95,7 @@ func (s *FunctionLookupBuilder) getNamespaces() ([]string, error) { } if err := json.Unmarshal(bytesOut, &namespaces); err != nil { - return namespaces, err + return namespaces, fmt.Errorf("unable to marshal to JSON: %s, error: %w", string(bytesOut), err) } return namespaces, err diff --git a/types/function_list_builder_test.go b/types/function_list_builder_test.go index f14393f..70fc1d5 100644 --- a/types/function_list_builder_test.go +++ b/types/function_list_builder_test.go @@ -5,8 +5,10 @@ package types import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" + "strings" "testing" "github.com/openfaas/faas-provider/types" @@ -404,14 +406,14 @@ func Test_GetNamespaces_ProviderGives404(t *testing.T) { GatewayURL: srv.URL, } - namespaces, err := builder.getNamespaces() - if err != nil { - t.Errorf("%s", err.Error()) + _, err := builder.getNamespaces() + if err == nil { + t.Fatalf("expected error, but got none") } - want := 0 - got := len(namespaces) - if len(namespaces) != 0 { - t.Errorf("Namespaces when 404, want %d, but got: %d", want, got) + + wantStr := "unable to marshal to JSON" + if strings.Contains(err.Error(), wantStr) == false { + fmt.Errorf("want error to contain %s, but got %s", wantStr, err.Error()) } }