From 4cd4539c71919a322180713c225fe23edf0eb12e Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Thu, 14 Mar 2019 15:38:13 -0700 Subject: Use httptest instead of http in unit tests Use httptest instead of http in unit tests similar to: https://golang.org/pkg/net/http/httptest/#example_ResponseRecorder Update empty body checking to account for change Issue-ID: MULTICLOUD-545 Change-Id: Ib9775078c2c9ae2878b714363b569d8d79bd7698 Signed-off-by: Kiran Kamineni --- src/k8splugin/api/handler.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/k8splugin/api/handler.go') diff --git a/src/k8splugin/api/handler.go b/src/k8splugin/api/handler.go index 32bfa594..4d6abe27 100644 --- a/src/k8splugin/api/handler.go +++ b/src/k8splugin/api/handler.go @@ -16,6 +16,7 @@ package api import ( "encoding/json" "errors" + "io" "log" "net/http" "os" @@ -75,13 +76,12 @@ func validateBody(body interface{}) error { func CreateHandler(w http.ResponseWriter, r *http.Request) { var resource CreateVnfRequest - if r.Body == nil { + err := json.NewDecoder(r.Body).Decode(&resource) + switch { + case err == io.EOF: http.Error(w, "Body empty", http.StatusBadRequest) return - } - - err := json.NewDecoder(r.Body).Decode(&resource) - if err != nil { + case err != nil: http.Error(w, err.Error(), http.StatusUnprocessableEntity) return } -- cgit 1.2.3-korg