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/defhandler.go | 20 ++++---- src/k8splugin/api/defhandler_test.go | 87 ++++++++++++-------------------- src/k8splugin/api/handler.go | 10 ++-- src/k8splugin/api/handler_test.go | 36 ++++++------- src/k8splugin/api/profilehandler.go | 20 ++++---- src/k8splugin/api/profilehandler_test.go | 84 ++++++++++++------------------ 6 files changed, 105 insertions(+), 152 deletions(-) (limited to 'src/k8splugin') diff --git a/src/k8splugin/api/defhandler.go b/src/k8splugin/api/defhandler.go index 44521dd1..f72247ab 100644 --- a/src/k8splugin/api/defhandler.go +++ b/src/k8splugin/api/defhandler.go @@ -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) diff --git a/src/k8splugin/api/defhandler_test.go b/src/k8splugin/api/defhandler_test.go index 48e2406c..ed5f298b 100644 --- a/src/k8splugin/api/defhandler_test.go +++ b/src/k8splugin/api/defhandler_test.go @@ -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) } }) } 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 } diff --git a/src/k8splugin/api/handler_test.go b/src/k8splugin/api/handler_test.go index fccda3f4..d1e4de07 100644 --- a/src/k8splugin/api/handler_test.go +++ b/src/k8splugin/api/handler_test.go @@ -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 { diff --git a/src/k8splugin/api/profilehandler.go b/src/k8splugin/api/profilehandler.go index 86e0d47f..267dae0e 100644 --- a/src/k8splugin/api/profilehandler.go +++ b/src/k8splugin/api/profilehandler.go @@ -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) diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go index 7594afeb..f6b27fc4 100644 --- a/src/k8splugin/api/profilehandler_test.go +++ b/src/k8splugin/api/profilehandler_test.go @@ -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) } }) } -- cgit 1.2.3-korg