From ccc2db5c94626f09f361cde5cfd34fd0ee0b2aa6 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Sun, 8 Oct 2023 14:33:56 +0200 Subject: [PATCH] Add test for handling pipeline error --- server/api/helper_test.go | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 server/api/helper_test.go diff --git a/server/api/helper_test.go b/server/api/helper_test.go new file mode 100644 index 0000000000..7beb9693f7 --- /dev/null +++ b/server/api/helper_test.go @@ -0,0 +1,41 @@ +package api + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gin-gonic/gin" + + "github.com/woodpecker-ci/woodpecker/server/pipeline" +) + +func TestHandlePipelineError(t *testing.T) { + tests := []struct { + err error + code int + }{ + { + err: pipeline.ErrFiltered, + code: http.StatusNoContent, + }, + { + err: &pipeline.ErrNotFound{Msg: "pipeline not found"}, + code: http.StatusNotFound, + }, + { + err: &pipeline.ErrBadRequest{Msg: "bad request error"}, + code: http.StatusBadRequest, + }, + } + + for _, tt := range tests { + r := httptest.NewRecorder() + c, _ := gin.CreateTestContext(r) + handlePipelineErr(c, tt.err) + c.Writer.WriteHeaderNow() // require written header + if r.Code != tt.code { + t.Errorf("status code: %d - expected: %d", r.Code, tt.code) + } + } +}