-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
60 lines (51 loc) · 1.63 KB
/
http.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
package testutils
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/bradenrayhorn/beans/beans"
"github.com/bradenrayhorn/beans/http/httpcontext"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/require"
)
func HTTP(t testing.TB, f http.HandlerFunc, user *beans.User, budget *beans.Budget, body any, status int) string {
return HTTPWithOptions(t, f, nil, user, budget, body, status)
}
type HTTPOptions struct {
URLParams map[string]string
}
func HTTPWithOptions(t testing.TB, f http.HandlerFunc, options *HTTPOptions, user *beans.User, budget *beans.Budget, body any, status int) string {
var reqBody io.Reader
switch body := body.(type) {
case string:
reqBody = bytes.NewReader([]byte(body))
default:
reqBody = nil
}
req := httptest.NewRequest("", "/", reqBody)
req = req.WithContext(context.WithValue(req.Context(), httpcontext.Budget, budget))
auth := beans.NewAuthContext(user.ID, beans.SessionID("1234"))
req = req.WithContext(context.WithValue(req.Context(), httpcontext.Auth, auth))
if budget != nil {
budgetAuth, err := beans.NewBudgetAuthContext(auth, budget)
require.Nil(t, err)
req = req.WithContext(context.WithValue(req.Context(), httpcontext.BudgetAuth, budgetAuth))
}
if options != nil {
rctx := chi.NewRouteContext()
for k, v := range options.URLParams {
rctx.URLParams.Add(k, v)
}
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
}
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
res := w.Result()
require.Equal(t, status, res.StatusCode)
data, err := io.ReadAll(res.Body)
require.Nil(t, err)
return string(data)
}