diff options
author | Victor Morales <victor.morales@intel.com> | 2019-03-22 23:31:56 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-03-22 23:31:56 +0000 |
commit | 8cdd50b6a06aef5cb0541e74a07b10bd4b01b589 (patch) | |
tree | 51d4392b09671e7e0f4f02862401dac4f74eef16 /src/k8splugin/api/profilehandler_test.go | |
parent | 79b27040152a7c4ce3145b15d726645d90ea79d9 (diff) | |
parent | 4cd4539c71919a322180713c225fe23edf0eb12e (diff) |
Merge "Use httptest instead of http in unit tests"
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) } }) } |