-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfux_test.go
43 lines (37 loc) · 1.05 KB
/
fux_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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")
})
}