-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtester.go
45 lines (39 loc) · 1.01 KB
/
tester.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
package web
import (
"context"
"net/http"
)
// Request is a request put on the TestHandler queue that records eacn request sent through
type Request struct {
Ctx context.Context
Rw http.ResponseWriter
Req *http.Request
}
// Recorder stores into Queue each request that comes across the recorder
type Recorder struct {
Queue chan Request
}
// ServeHTTPC stores the req into Queue and calls next
func (t *Recorder) ServeHTTPC(ctx context.Context, rw http.ResponseWriter, r *http.Request, next ContextHandler) {
t.Queue <- Request{
Ctx: ctx,
Rw: rw,
Req: r,
}
if next != nil {
next.ServeHTTPC(ctx, rw, r)
}
}
// AsHandler returns a Recoder that can be a destination handler
func (t *Recorder) AsHandler() ContextHandler {
return HandlerFunc(func(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
t.ServeHTTPC(ctx, rw, r, nil)
})
}
// ServeHTTP stores the request into the Queue
func (t *Recorder) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
t.Queue <- Request{
Rw: rw,
Req: r,
}
}