Browse Source

Added HTTP redirect handler

Volodymyr Tkach 2 years ago
parent
commit
3aa2a5c9d4
2 changed files with 57 additions and 0 deletions
  1. 11 0
      utils/http/redirect/redirect.go
  2. 46 0
      utils/http/redirect/redirect_test.go

+ 11 - 0
utils/http/redirect/redirect.go

@@ -0,0 +1,11 @@
+package redirect
+
+import (
+	"net/http"
+)
+
+func Handler(url string) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		http.Redirect(w, r, url, http.StatusTemporaryRedirect)
+	})
+}

+ 46 - 0
utils/http/redirect/redirect_test.go

@@ -0,0 +1,46 @@
+package redirect_test
+
+import (
+	"net/http"
+	"net/http/httptest"
+	"testing"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+	"github.com/vladimirok5959/golang-utils/utils/http/redirect"
+)
+
+var _ = Describe("redirect", func() {
+	Context("Handler", func() {
+		var srv *httptest.Server
+		var client *http.Client
+
+		BeforeEach(func() {
+			srv = httptest.NewServer(redirect.Handler("/example.html"))
+			client = srv.Client()
+
+			// Disable HTTP redirects
+			client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
+				return http.ErrUseLastResponse
+			}
+		})
+
+		AfterEach(func() {
+			srv.Close()
+		})
+
+		It("doing redirect", func() {
+			resp, err := client.Get(srv.URL + "/")
+			Expect(err).To(Succeed())
+			defer resp.Body.Close()
+
+			Expect(resp.StatusCode).To(Equal(http.StatusTemporaryRedirect))
+			Expect(resp.Header.Get("Location")).To(Equal("/example.html"))
+		})
+	})
+})
+
+func TestSuite(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "redirect")
+}