diff options
Diffstat (limited to 'src/k8splugin/api/profilehandler_test.go')
-rw-r--r-- | src/k8splugin/api/profilehandler_test.go | 84 |
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) } }) } |