-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
57 lines (49 loc) · 1.17 KB
/
request.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
package testool
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi"
"github.com/ispec-inc/monorepo/go/pkg/applog"
)
type request struct {
method string
path string
urlparam map[string]string
body interface{}
}
func NewRequest(method, path string, body interface{}) *request {
return &request{
method: method,
path: path,
urlparam: make(map[string]string),
body: body,
}
}
func (r *request) AddURLParam(key, val string) {
r.urlparam[key] = val
}
func (r *request) Parse(t *testing.T) *http.Request {
req := httptest.NewRequest(r.method, r.path, r.encodeBody(t))
ctx := r.withURLParamContext(req.Context())
ctx = applog.WithTestContext(ctx)
return req.WithContext(ctx)
}
func (r *request) encodeBody(t *testing.T) io.Reader {
body := new(bytes.Buffer)
if err := json.NewEncoder(body).Encode(r.body); err != nil {
t.Fatal(err)
}
return body
}
func (r *request) withURLParamContext(ctx context.Context) context.Context {
rctx := chi.NewRouteContext()
for key, val := range r.urlparam {
rctx.URLParams.Add(key, val)
}
return context.WithValue(ctx, chi.RouteCtxKey, rctx)
}