-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpath_test.go
81 lines (70 loc) · 2.02 KB
/
path_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package torque_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
. "github.com/onsi/gomega"
"github.com/tylermmorton/torque"
)
func TestPath_GetPathParam_TorqueHandler(t *testing.T) {
h := torque.MustNew[any](&struct {
MockRouterProvider
}{
MockRouterProvider: MockRouterProvider{
RouterFunc: func(r torque.Router) {
r.Handle("/users/{id}", torque.MustNew[string](&struct {
MockLoader[string]
MockRenderer[string]
}{
MockLoader: MockLoader[string]{
LoadFunc: func(req *http.Request) (string, error) {
return torque.GetPathParam(req, "id"), nil
},
},
MockRenderer: MockRenderer[string]{
RenderFunc: func(wr http.ResponseWriter, req *http.Request, vm string) error {
_, err := wr.Write([]byte(fmt.Sprintf("hello, %s!", vm)))
return err
},
},
}))
},
},
})
RegisterTestingT(t)
wr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/users/tommy", nil)
h.ServeHTTP(wr, req)
res := wr.Result()
defer Expect(res.Body.Close()).To(BeNil())
byt, err := io.ReadAll(res.Body)
Expect(err).NotTo(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))
Expect(string(byt)).To(Equal("hello, tommy!"))
}
func TestPath_GetPathParam_VanillaHandler(t *testing.T) {
h := torque.MustNew[any](&struct {
MockRouterProvider
}{
MockRouterProvider: MockRouterProvider{
RouterFunc: func(r torque.Router) {
r.Handle("/users/{id}", http.HandlerFunc(func(wr http.ResponseWriter, req *http.Request) {
_, err := wr.Write([]byte(fmt.Sprintf("hello, %s!", torque.GetPathParam(req, "id"))))
Expect(err).NotTo(HaveOccurred())
}))
},
},
})
RegisterTestingT(t)
wr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/users/tommy?foo=bar", nil)
h.ServeHTTP(wr, req)
res := wr.Result()
defer Expect(res.Body.Close()).To(BeNil())
byt, err := io.ReadAll(res.Body)
Expect(err).NotTo(HaveOccurred())
Expect(res.StatusCode).To(Equal(http.StatusOK))
Expect(string(byt)).To(Equal("hello, tommy!"))
}