Use httptest instead of http in unit tests 33/82733/2
authorKiran Kamineni <kiran.k.kamineni@intel.com>
Thu, 14 Mar 2019 22:38:13 +0000 (15:38 -0700)
committerKiran Kamineni <kiran.k.kamineni@intel.com>
Fri, 22 Mar 2019 20:44:09 +0000 (13:44 -0700)
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 <kiran.k.kamineni@intel.com>
src/k8splugin/api/defhandler.go
src/k8splugin/api/defhandler_test.go
src/k8splugin/api/handler.go
src/k8splugin/api/handler_test.go
src/k8splugin/api/profilehandler.go
src/k8splugin/api/profilehandler_test.go

index 44521dd..f72247a 100644 (file)
@@ -18,6 +18,7 @@ package api
 
 import (
        "encoding/json"
+       "io"
        "io/ioutil"
        "k8splugin/internal/rb"
        "net/http"
@@ -37,13 +38,12 @@ type rbDefinitionHandler struct {
 func (h rbDefinitionHandler) createHandler(w http.ResponseWriter, r *http.Request) {
        var v rb.Definition
 
-       if r.Body == nil {
+       err := json.NewDecoder(r.Body).Decode(&v)
+       switch {
+       case err == io.EOF:
                http.Error(w, "Empty body", http.StatusBadRequest)
                return
-       }
-
-       err := json.NewDecoder(r.Body).Decode(&v)
-       if err != nil {
+       case err != nil:
                http.Error(w, err.Error(), http.StatusUnprocessableEntity)
                return
        }
@@ -74,17 +74,17 @@ func (h rbDefinitionHandler) uploadHandler(w http.ResponseWriter, r *http.Reques
        vars := mux.Vars(r)
        uuid := vars["rbdID"]
 
-       if r.Body == nil {
-               http.Error(w, "Empty Body", http.StatusBadRequest)
-               return
-       }
-
        inpBytes, err := ioutil.ReadAll(r.Body)
        if err != nil {
                http.Error(w, "Unable to read body", http.StatusBadRequest)
                return
        }
 
+       if len(inpBytes) == 0 {
+               http.Error(w, "Empty body", http.StatusBadRequest)
+               return
+       }
+
        err = h.client.Upload(uuid, inpBytes)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
index 48e2406..ed5f298 100644 (file)
@@ -130,25 +130,20 @@ func TestRBDefCreateHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbDefinitionHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("POST", "/v1/rb/definition", testCase.reader)
-
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
+                       req := httptest.NewRequest("POST", "/v1/rb/definition", testCase.reader)
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.createHandler)
-                       hr.ServeHTTP(rr, req)
+                       vh.createHandler(rr, req)
+                       resp := rr.Result()
 
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
 
                        //Check returned body only if statusCreated
-                       if rr.Code == http.StatusCreated {
+                       if resp.StatusCode == http.StatusCreated {
                                got := rb.Definition{}
-                               json.NewDecoder(rr.Body).Decode(&got)
+                               json.NewDecoder(resp.Body).Decode(&got)
 
                                if reflect.DeepEqual(testCase.expected, got) == false {
                                        t.Errorf("createHandler returned unexpected body: got %v;"+
@@ -207,24 +202,20 @@ func TestRBDefListHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbDefinitionHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("GET", "/v1/rb/definition", nil)
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
+                       req := httptest.NewRequest("GET", "/v1/rb/definition", nil)
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.listHandler)
+                       vh.listHandler(rr, req)
+                       resp := rr.Result()
 
-                       hr.ServeHTTP(rr, req)
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
 
                        //Check returned body only if statusOK
-                       if rr.Code == http.StatusOK {
+                       if resp.StatusCode == http.StatusOK {
                                got := []rb.Definition{}
-                               json.NewDecoder(rr.Body).Decode(&got)
+                               json.NewDecoder(resp.Body).Decode(&got)
 
                                // Since the order of returned slice is not guaranteed
                                // Check both and return error if both don't match
@@ -292,24 +283,19 @@ func TestRBDefGetHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbDefinitionHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("GET", "/v1/rb/definition/"+testCase.inpUUID, nil)
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
+                       req := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.inpUUID, nil)
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.getHandler)
-
-                       hr.ServeHTTP(rr, req)
+                       vh.getHandler(rr, req)
+                       resp := rr.Result()
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
 
                        //Check returned body only if statusOK
-                       if rr.Code == http.StatusOK {
+                       if resp.StatusCode == http.StatusOK {
                                got := rb.Definition{}
-                               json.NewDecoder(rr.Body).Decode(&got)
+                               json.NewDecoder(resp.Body).Decode(&got)
 
                                if reflect.DeepEqual(testCase.expected, got) == false {
                                        t.Errorf("listHandler returned unexpected body: got %v;"+
@@ -347,18 +333,13 @@ func TestRBDefDeleteHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbDefinitionHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("GET", "/v1/rb/definition/"+testCase.inpUUID, nil)
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
+                       req := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.inpUUID, nil)
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.deleteHandler)
-
-                       hr.ServeHTTP(rr, req)
+                       vh.deleteHandler(rr, req)
+                       resp := rr.Result()
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
                })
        }
@@ -406,20 +387,14 @@ func TestRBDefUploadHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbDefinitionHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("POST",
+                       req := httptest.NewRequest("POST",
                                "/v1/rb/definition/"+testCase.inpUUID+"/content", testCase.body)
-
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.uploadHandler)
-
-                       hr.ServeHTTP(rr, req)
+                       vh.uploadHandler(rr, req)
+                       resp := rr.Result()
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
                })
        }
index 32bfa59..4d6abe2 100644 (file)
@@ -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
        }
index fccda3f..d1e4de0 100644 (file)
@@ -47,12 +47,12 @@ func (c *mockCSAR) DestroyVNF(data map[string][]string, namespace string,
        return c.err
 }
 
-func executeRequest(req *http.Request) *httptest.ResponseRecorder {
+func executeRequest(req *http.Request) *http.Response {
        router := NewRouter("", nil, nil)
        recorder := httptest.NewRecorder()
        router.ServeHTTP(recorder, req)
 
-       return recorder
+       return recorder.Result()
 }
 
 func checkResponseCode(t *testing.T, expected, actual int) {
@@ -161,13 +161,13 @@ func TestCreateHandler(t *testing.T) {
                                db.DBconn = testCase.mockStore
                        }
 
-                       request, _ := http.NewRequest("POST", "/v1/vnf_instances/", testCase.input)
+                       request := httptest.NewRequest("POST", "/v1/vnf_instances/", testCase.input)
                        result := executeRequest(request)
 
-                       if testCase.expectedCode != result.Code {
-                               t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v", result.Code, testCase.expectedCode)
+                       if testCase.expectedCode != result.StatusCode {
+                               t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v", result.StatusCode, testCase.expectedCode)
                        }
-                       if result.Code == http.StatusCreated {
+                       if result.StatusCode == http.StatusCreated {
                                var response CreateVnfResponse
                                err := json.NewDecoder(result.Body).Decode(&response)
                                if err != nil {
@@ -218,14 +218,14 @@ func TestListHandler(t *testing.T) {
                                db.DBconn = testCase.mockStore
                        }
 
-                       request, _ := http.NewRequest("GET", "/v1/vnf_instances/cloud1/default", nil)
+                       request := httptest.NewRequest("GET", "/v1/vnf_instances/cloud1/default", nil)
                        result := executeRequest(request)
 
-                       if testCase.expectedCode != result.Code {
+                       if testCase.expectedCode != result.StatusCode {
                                t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v",
-                                       result.Code, testCase.expectedCode)
+                                       result.StatusCode, testCase.expectedCode)
                        }
-                       if result.Code == http.StatusOK {
+                       if result.StatusCode == http.StatusOK {
                                var response ListVnfsResponse
                                err := json.NewDecoder(result.Body).Decode(&response)
                                if err != nil {
@@ -331,11 +331,11 @@ func TestDeleteHandler(t *testing.T) {
                                helper.DestroyVNF = testCase.mockDeleteVNF.DestroyVNF
                        }
 
-                       request, _ := http.NewRequest("DELETE", "/v1/vnf_instances/cloudregion1/testnamespace/uuid1", nil)
+                       request := httptest.NewRequest("DELETE", "/v1/vnf_instances/cloudregion1/testnamespace/uuid1", nil)
                        result := executeRequest(request)
 
-                       if testCase.expectedCode != result.Code {
-                               t.Fatalf("Request method returned: %v and it was expected: %v", result.Code, testCase.expectedCode)
+                       if testCase.expectedCode != result.StatusCode {
+                               t.Fatalf("Request method returned: %v and it was expected: %v", result.StatusCode, testCase.expectedCode)
                        }
                })
        }
@@ -367,7 +367,7 @@ func TestVNFInstanceUpdate(t *testing.T) {
 
                var result UpdateVnfResponse
 
-               req, _ := http.NewRequest("PUT", "/v1/vnf_instances/1", bytes.NewBuffer(payload))
+               req := httptest.NewRequest("PUT", "/v1/vnf_instances/1", bytes.NewBuffer(payload))
 
                GetVNFClient = func(configPath string) (krd.VNFInstanceClientInterface, error) {
                        return &mockClient{
@@ -457,14 +457,14 @@ func TestGetHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        db.DBconn = testCase.mockStore
-                       request, _ := http.NewRequest("GET", "/v1/vnf_instances/cloud1/default/1", nil)
+                       request := httptest.NewRequest("GET", "/v1/vnf_instances/cloud1/default/1", nil)
                        result := executeRequest(request)
 
-                       if testCase.expectedCode != result.Code {
+                       if testCase.expectedCode != result.StatusCode {
                                t.Fatalf("Request method returned: %v and it was expected: %v",
-                                       result.Code, testCase.expectedCode)
+                                       result.StatusCode, testCase.expectedCode)
                        }
-                       if result.Code == http.StatusOK {
+                       if result.StatusCode == http.StatusOK {
                                var response GetVnfResponse
                                err := json.NewDecoder(result.Body).Decode(&response)
                                if err != nil {
index 86e0d47..267dae0 100644 (file)
@@ -18,6 +18,7 @@ package api
 
 import (
        "encoding/json"
+       "io"
        "io/ioutil"
        "k8splugin/internal/rb"
        "net/http"
@@ -37,13 +38,12 @@ type rbProfileHandler struct {
 func (h rbProfileHandler) createHandler(w http.ResponseWriter, r *http.Request) {
        var v rb.Profile
 
-       if r.Body == nil {
+       err := json.NewDecoder(r.Body).Decode(&v)
+       switch {
+       case err == io.EOF:
                http.Error(w, "Empty body", http.StatusBadRequest)
                return
-       }
-
-       err := json.NewDecoder(r.Body).Decode(&v)
-       if err != nil {
+       case err != nil:
                http.Error(w, err.Error(), http.StatusUnprocessableEntity)
                return
        }
@@ -80,17 +80,17 @@ func (h rbProfileHandler) uploadHandler(w http.ResponseWriter, r *http.Request)
        vars := mux.Vars(r)
        uuid := vars["rbpID"]
 
-       if r.Body == nil {
-               http.Error(w, "Empty Body", http.StatusBadRequest)
-               return
-       }
-
        inpBytes, err := ioutil.ReadAll(r.Body)
        if err != nil {
                http.Error(w, "Unable to read body", http.StatusBadRequest)
                return
        }
 
+       if len(inpBytes) == 0 {
+               http.Error(w, "Empty body", http.StatusBadRequest)
+               return
+       }
+
        err = h.client.Upload(uuid, inpBytes)
        if err != nil {
                http.Error(w, err.Error(), http.StatusInternalServerError)
index 7594afe..f6b27fc 100644 (file)
@@ -120,25 +120,20 @@ func TestRBProfileCreateHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbProfileHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("POST", "/v1/rb/profile", testCase.reader)
-
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
+                       req := httptest.NewRequest("POST", "/v1/rb/profile", testCase.reader)
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.createHandler)
-                       hr.ServeHTTP(rr, req)
+                       vh.createHandler(rr, req)
+                       resp := rr.Result()
 
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
 
                        //Check returned body only if statusCreated
-                       if rr.Code == http.StatusCreated {
+                       if resp.StatusCode == http.StatusCreated {
                                got := rb.Profile{}
-                               json.NewDecoder(rr.Body).Decode(&got)
+                               json.NewDecoder(resp.Body).Decode(&got)
 
                                if reflect.DeepEqual(testCase.expected, got) == false {
                                        t.Errorf("createHandler returned unexpected body: got %v;"+
@@ -201,24 +196,20 @@ func TestRBProfileListHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbProfileHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("GET", "/v1/rb/profile", nil)
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
+                       req := httptest.NewRequest("GET", "/v1/rb/profile", nil)
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.listHandler)
+                       vh.listHandler(rr, req)
 
-                       hr.ServeHTTP(rr, req)
+                       resp := rr.Result()
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
 
                        //Check returned body only if statusOK
-                       if rr.Code == http.StatusOK {
+                       if resp.StatusCode == http.StatusOK {
                                got := []rb.Profile{}
-                               json.NewDecoder(rr.Body).Decode(&got)
+                               json.NewDecoder(resp.Body).Decode(&got)
 
                                // Since the order of returned slice is not guaranteed
                                // Check both and return error if both don't match
@@ -288,24 +279,20 @@ func TestRBProfileGetHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbProfileHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil)
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
+                       req := httptest.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil)
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.getHandler)
+                       vh.getHandler(rr, req)
 
-                       hr.ServeHTTP(rr, req)
+                       resp := rr.Result()
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
 
                        //Check returned body only if statusOK
-                       if rr.Code == http.StatusOK {
+                       if resp.StatusCode == http.StatusOK {
                                got := rb.Profile{}
-                               json.NewDecoder(rr.Body).Decode(&got)
+                               json.NewDecoder(resp.Body).Decode(&got)
 
                                if reflect.DeepEqual(testCase.expected, got) == false {
                                        t.Errorf("listHandler returned unexpected body: got %v;"+
@@ -343,18 +330,14 @@ func TestRBProfileDeleteHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbProfileHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil)
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
+                       req := httptest.NewRequest("GET", "/v1/rb/profile/"+testCase.inpUUID, nil)
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.deleteHandler)
+                       vh.deleteHandler(rr, req)
 
-                       hr.ServeHTTP(rr, req)
+                       resp := rr.Result()
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
                })
        }
@@ -402,20 +385,15 @@ func TestRBProfileUploadHandler(t *testing.T) {
        for _, testCase := range testCases {
                t.Run(testCase.label, func(t *testing.T) {
                        vh := rbProfileHandler{client: testCase.rbDefClient}
-                       req, err := http.NewRequest("POST",
+                       req := httptest.NewRequest("POST",
                                "/v1/rb/profile/"+testCase.inpUUID+"/content", testCase.body)
-
-                       if err != nil {
-                               t.Fatal(err)
-                       }
-
                        rr := httptest.NewRecorder()
-                       hr := http.HandlerFunc(vh.uploadHandler)
+                       vh.uploadHandler(rr, req)
 
-                       hr.ServeHTTP(rr, req)
+                       resp := rr.Result()
                        //Check returned code
-                       if rr.Code != testCase.expectedCode {
-                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, rr.Code)
+                       if resp.StatusCode != testCase.expectedCode {
+                               t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode)
                        }
                })
        }