-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoint.go
82 lines (68 loc) · 1.7 KB
/
endpoint.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
package httpstub
import (
"net/http"
"time"
)
// An Endpoint is added to a stub server with server.Path(), and implements a handler that will be matched against a path prefix.
type Endpoint struct {
path string
status int
contentType string
body []byte
delay time.Duration
forMethod map[string]*Endpoint
}
// WithMethod creates a method-specific endpoint within a path.
func (e *Endpoint) WithMethod(m string) *Endpoint {
me := &Endpoint{
status: e.status,
contentType: e.contentType,
body: e.body,
delay: e.delay,
}
if e.forMethod == nil {
e.forMethod = make(map[string]*Endpoint)
}
e.forMethod[m] = me
return me
}
// WithStatus sets the response status code for the endpoint.
func (e *Endpoint) WithStatus(s int) *Endpoint {
e.status = s
return e
}
// WithContentType overrides the server's default content type for the endpoint.
func (e *Endpoint) WithContentType(t string) *Endpoint {
e.contentType = t
return e
}
// WithBody sets the body the endpoint should return.
func (e *Endpoint) WithBody(b string) *Endpoint {
e.body = []byte(b)
return e
}
// WithDelay sets a delay before the endpoint responds
func (e *Endpoint) WithDelay(d time.Duration) *Endpoint {
e.delay = d
return e
}
// ServeHTTP lets Endpoint implement http.Handler.
func (e *Endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// use the method-specific handler if available
if me, ok := e.forMethod[r.Method]; ok {
me.ServeHTTP(w, r)
return
}
if e.delay > 0 {
time.Sleep(e.delay)
}
if len(e.contentType) > 0 {
w.Header().Set("Content-Type", e.contentType)
}
if e.status > 0 {
w.WriteHeader(e.status)
}
if len(e.body) > 0 {
w.Write(e.body)
}
}