Browse Source

HTTP methods helpers

Volodymyr Tkach 2 years ago
parent
commit
c357129964
2 changed files with 90 additions and 0 deletions
  1. 50 0
      utils/http/apiserv/apiserv_test.go
  2. 40 0
      utils/http/apiserv/helpers.go

+ 50 - 0
utils/http/apiserv/apiserv_test.go

@@ -0,0 +1,50 @@
+package apiserv_test
+
+import (
+	"net/http"
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+	"github.com/vladimirok5959/golang-utils/utils/http/apiserv"
+)
+
+var _ = Describe("apiserv", func() {
+	Context("Methods", func() {
+		It("build correct array", func() {
+			m := apiserv.Methods()
+			Expect(m).To(Equal(apiserv.TMethods{}))
+
+			// Single
+			Expect(m.Delete()).To(Equal(apiserv.TMethods{http.MethodDelete}))
+			Expect(m.Get()).To(Equal(apiserv.TMethods{http.MethodGet}))
+			Expect(m.Options()).To(Equal(apiserv.TMethods{http.MethodOptions}))
+			Expect(m.Patch()).To(Equal(apiserv.TMethods{http.MethodPatch}))
+			Expect(m.Post()).To(Equal(apiserv.TMethods{http.MethodPost}))
+			Expect(m.Put()).To(Equal(apiserv.TMethods{http.MethodPut}))
+
+			// Multiple
+			Expect(m.Get().Put()).To(Equal(apiserv.TMethods{
+				http.MethodGet,
+				http.MethodPut,
+			}))
+
+			Expect(m.Get().Post()).To(Equal(apiserv.TMethods{
+				http.MethodGet,
+				http.MethodPost,
+			}))
+
+			Expect(m.Get().Put().Post().Delete()).To(Equal(apiserv.TMethods{
+				http.MethodGet,
+				http.MethodPut,
+				http.MethodPost,
+				http.MethodDelete,
+			}))
+		})
+	})
+})
+
+func TestSuite(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "apiserv")
+}

+ 40 - 0
utils/http/apiserv/helpers.go

@@ -0,0 +1,40 @@
+package apiserv
+
+import "net/http"
+
+// http.MethodDelete
+// http.MethodGet
+// http.MethodOptions
+// http.MethodPatch
+// http.MethodPost
+// http.MethodPut
+
+type TMethods []string
+
+func Methods() TMethods {
+	return []string{}
+}
+
+func (m TMethods) Delete() TMethods {
+	return append(m, http.MethodDelete)
+}
+
+func (m TMethods) Get() TMethods {
+	return append(m, http.MethodGet)
+}
+
+func (m TMethods) Options() TMethods {
+	return append(m, http.MethodOptions)
+}
+
+func (m TMethods) Patch() TMethods {
+	return append(m, http.MethodPatch)
+}
+
+func (m TMethods) Post() TMethods {
+	return append(m, http.MethodPost)
+}
+
+func (m TMethods) Put() TMethods {
+	return append(m, http.MethodPut)
+}