-
Notifications
You must be signed in to change notification settings - Fork 0
/
stubs.go
64 lines (56 loc) · 1.83 KB
/
stubs.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
package mockhttp
import (
"encoding/json"
"log"
"net/http"
"time"
)
// StubBuilder is a helper to build stubs for a specific HTTP call
type StubBuilder struct {
api *APIMock
call *HTTPCall
delay time.Duration
}
// With creates a new stub for the HTTP call with the specified handler
func (stub *StubBuilder) With(handler http.HandlerFunc) *APIMock {
if stub.delay > 0 {
stub.api.calls[*stub.call] = func(writer http.ResponseWriter, request *http.Request) {
time.Sleep(stub.delay)
handler(writer, request)
}
} else {
stub.api.calls[*stub.call] = handler
}
return stub.api
}
// WithDelay adds a delay to the stub when called before it executes the corresponding handler
func (stub *StubBuilder) WithDelay(delay time.Duration) *StubBuilder {
stub.delay = delay
return stub
}
// WithStatusCode creates a new stub handler returning the specified status code
func (stub *StubBuilder) WithStatusCode(statusCode int) *APIMock {
return stub.With(func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(statusCode)
})
}
// WithJSON creates a new stub handler returning the specified status code and JSON content.
// The response header "Content-Type" is set to "application/json".
func (stub *StubBuilder) WithJSON(statusCode int, content interface{}) *APIMock {
body, err := json.Marshal(content)
if err != nil {
log.Fatal(err)
}
return stub.WithBody(statusCode, body, "application/json")
}
// WithBody creates a new stub handler returning the specified status code and body content.
func (stub *StubBuilder) WithBody(statusCode int, body []byte, contentType string) *APIMock {
return stub.With(func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Add("Content-Type", contentType)
writer.WriteHeader(statusCode)
_, err := writer.Write(body)
if err != nil {
log.Fatal(err)
}
})
}