-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttprc_test.go
118 lines (107 loc) · 3.12 KB
/
httprc_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package httprc_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/lestrrat-go/httprc/v3"
"github.com/stretchr/testify/require"
)
func TestClient(t *testing.T) {
type Hello struct {
Hello string `json:"hello"`
}
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/json/helloptr", "/json/hello", "/json/hellomap":
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"hello":"world"}`))
case "/int":
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(`42`))
case "/string":
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(`Lorem ipsum dolor sit amet`))
case "/custom":
}
})
srv := httptest.NewServer(h)
defer srv.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cl := httprc.NewClient()
ctrl, err := cl.Start(ctx)
require.NoError(t, err, `cl.Run should succeed`)
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = ctrl.Shutdown(ctx)
}()
testcases := []struct {
URL string
Create func() (httprc.Resource, error)
Expected any
}{
{
URL: srv.URL + "/json/helloptr",
Create: func() (httprc.Resource, error) {
return httprc.NewResource[*Hello](srv.URL+"/json/helloptr", httprc.JSONTransformer[*Hello]())
},
Expected: &Hello{Hello: "world"},
},
{
URL: srv.URL + "/json/hello",
Create: func() (httprc.Resource, error) {
return httprc.NewResource[Hello](srv.URL+"/json/hello", httprc.JSONTransformer[Hello]())
},
Expected: Hello{Hello: "world"},
},
{
URL: srv.URL + "/json/hellomap",
Create: func() (httprc.Resource, error) {
return httprc.NewResource[map[string]interface{}](srv.URL+"/json/hellomap", httprc.JSONTransformer[map[string]interface{}]())
},
Expected: map[string]interface{}{"hello": "world"},
},
{
URL: srv.URL + "/int",
Create: func() (httprc.Resource, error) {
return httprc.NewResource[int](srv.URL+"/int", httprc.TransformFunc[int](func(_ context.Context, res *http.Response) (int, error) {
buf, err := io.ReadAll(res.Body)
if err != nil {
return 0, err
}
return strconv.Atoi(string(buf))
}))
},
Expected: 42,
},
{
URL: srv.URL + "/string",
Create: func() (httprc.Resource, error) {
return httprc.NewResource[string](srv.URL+"/string", httprc.TransformFunc[string](func(_ context.Context, res *http.Response) (string, error) {
buf, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(buf), nil
}))
},
Expected: "Lorem ipsum dolor sit amet",
},
}
for _, tc := range testcases {
t.Run(tc.URL, func(t *testing.T) {
r, err := tc.Create()
require.NoError(t, err, `NewResource should succeed`)
require.NoError(t, ctrl.AddResource(r), `ctrl.AddResource should succeed`)
require.NoError(t, r.Ready(ctx), `r.Ready should succeed`)
var dst interface{}
require.NoError(t, r.Get(&dst), `r.Get should succeed`)
require.Equal(t, tc.Expected, dst, `r.Get should return expected value`)
})
}
}