From 42dc5a3dec6c5aa9ce2bd2553310641d619ed027 Mon Sep 17 00:00:00 2001 From: Lena Fuhrimann <6780471+cloudlena@users.noreply.github.com> Date: Mon, 3 Apr 2017 08:44:09 +0200 Subject: [PATCH] Add unit test for delete object handler --- objects/delete-handler.go | 2 +- objects/delete-handler_test.go | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 objects/delete-handler_test.go diff --git a/objects/delete-handler.go b/objects/delete-handler.go index dd117de..df5f964 100644 --- a/objects/delete-handler.go +++ b/objects/delete-handler.go @@ -20,6 +20,6 @@ func DeleteHandler(s3 datasources.S3Client) http.Handler { return } - w.WriteHeader(http.StatusOK) + w.WriteHeader(http.StatusNoContent) }) } diff --git a/objects/delete-handler_test.go b/objects/delete-handler_test.go new file mode 100644 index 0000000..ada6bdc --- /dev/null +++ b/objects/delete-handler_test.go @@ -0,0 +1,51 @@ +package objects_test + +import ( + "errors" + "net/http" + "net/http/httptest" + "testing" + + "github.com/mastertinner/s3-manager/mock" + "github.com/mastertinner/s3-manager/objects" + "github.com/stretchr/testify/assert" +) + +func TestDeleteHandler(t *testing.T) { + assert := assert.New(t) + + tests := []struct { + description string + s3Client *mock.S3Client + expectedStatusCode int + expectedBody string + }{ + { + description: "success", + s3Client: &mock.S3Client{}, + expectedStatusCode: http.StatusNoContent, + expectedBody: "", + }, + { + description: "s3 error", + s3Client: &mock.S3Client{ + Err: errors.New("internal S3 error"), + }, + expectedStatusCode: http.StatusInternalServerError, + expectedBody: "error removing object\n", + }, + } + + for _, tc := range tests { + req, err := http.NewRequest("DELETE", "/api/buckets/bucketName/objects/objectName", nil) + assert.NoError(err) + + rr := httptest.NewRecorder() + handler := objects.DeleteHandler(tc.s3Client) + + handler.ServeHTTP(rr, req) + + assert.Equal(tc.expectedStatusCode, rr.Code, tc.description) + assert.Equal(tc.expectedBody, rr.Body.String(), tc.description) + } +}