Skip to content

Commit

Permalink
Add test for scheme matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamandlou committed Aug 4, 2023
1 parent a3a7854 commit 363b3fa
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions fux_test.go
Original file line number Diff line number Diff line change
@@ -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")
})
}

0 comments on commit 363b3fa

Please sign in to comment.