summaryrefslogtreecommitdiffstats
path: root/src/k8splugin/api/profilehandler_test.go
diff options
context:
space:
mode:
authorKiran Kamineni <kiran.k.kamineni@intel.com>2019-03-14 15:38:13 -0700
committerKiran Kamineni <kiran.k.kamineni@intel.com>2019-03-22 13:44:09 -0700
commit4cd4539c71919a322180713c225fe23edf0eb12e (patch)
treed4dadcf9b98f83dd7edb552b460c6ec177b924cf /src/k8splugin/api/profilehandler_test.go
parent838ddaa50041ac4c33adeb2c8a33340fdfe2c952 (diff)
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 <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src/k8splugin/api/profilehandler_test.go')
-rw-r--r--src/k8splugin/api/profilehandler_test.go84
1 files changed, 31 insertions, 53 deletions
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)
}
})
}