From 363b3fac4de76a0d860073fac9cf127a4dace0ff Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 4 Aug 2023 12:51:03 +0330 Subject: [PATCH] Add test for scheme matchers --- fux_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 fux_test.go diff --git a/fux_test.go b/fux_test.go new file mode 100644 index 0000000..661ad60 --- /dev/null +++ b/fux_test.go @@ -0,0 +1,43 @@ +package fux + +import ( + "github.com/stretchr/testify/assert" + "io" + "net/http" + "net/http/httptest" + "testing" +) + +func TestSchemeMatchers(t *testing.T) { + f := New() + + f.Get("/", func(w http.ResponseWriter, request *http.Request) { + _, err := w.Write([]byte("http response")) + assert.Nil(t, err) + }).Schemes("http") + + f.Get("/", func(w http.ResponseWriter, request *http.Request) { + _, err := w.Write([]byte("https response")) + assert.Nil(t, err) + }).Schemes("https") + + assertResponseBody := func(t *testing.T, s *httptest.Server, expectedResponse string) { + response, err := s.Client().Get(s.URL) + assert.Nil(t, err) + assert.Equal(t, 200, response.StatusCode) + body, err := io.ReadAll(response.Body) + assert.Nil(t, err) + assert.Equal(t, expectedResponse, string(body)) + } + + t.Run("httpServer", func(t *testing.T) { + s := httptest.NewServer(f.Router) + defer s.Close() + assertResponseBody(t, s, "http response") + }) + t.Run("httpsServer", func(t *testing.T) { + s := httptest.NewTLSServer(f.Router) + defer s.Close() + assertResponseBody(t, s, "https response") + }) +}