-
Notifications
You must be signed in to change notification settings - Fork 16
/
http_mock.go
148 lines (124 loc) · 3.96 KB
/
http_mock.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package mockhttp
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
. "github.com/onsi/gomega"
)
type MockHttp struct {
expectedMethod string
expectedUrl string
expectedContentType string
expectedBody string
expectedAuthorizationHeader string
expectedFormValues map[string]string
responseRedirectToUrl string
responseStatus int
responseBody string
responseCallback func(rawbody []byte)
authenticated bool
}
func (i *MockHttp) Fails(message string) *MockHttp {
i.responseBody = message
i.responseStatus = http.StatusInternalServerError
return i
}
func (i *MockHttp) RedirectsTo(uri string) *MockHttp {
i.responseStatus = http.StatusFound
i.responseRedirectToUrl = uri
return i
}
func (i *MockHttp) NotFound() *MockHttp {
i.responseBody = ""
i.responseStatus = http.StatusNotFound
return i
}
func (i *MockHttp) RespondsWithUnauthorized(body string) *MockHttp {
i.responseBody = body
i.responseStatus = http.StatusUnauthorized
return i
}
func (i *MockHttp) RespondsWithJson(obj interface{}) *MockHttp {
data, err := json.Marshal(obj)
Expect(err).NotTo(HaveOccurred())
i.RespondsWith(string(data))
return i
}
func (i *MockHttp) RespondsWith(body string) *MockHttp {
i.responseStatus = http.StatusOK
i.responseBody = body
return i
}
func (i *MockHttp) SkipAuthentication() *MockHttp {
i.authenticated = false
return i
}
func (i *MockHttp) RespondsEmpty() *MockHttp {
i.responseBody = ""
i.responseStatus = http.StatusNoContent
return i
}
func (i *MockHttp) WithContentType(contentType string) *MockHttp {
i.expectedContentType = contentType
return i
}
func (i *MockHttp) WithBody(body string) *MockHttp {
i.expectedBody = body
return i
}
func (i *MockHttp) WithAuthorizationHeader(auth string) *MockHttp {
i.expectedAuthorizationHeader = auth
return i
}
func NewMockedHttpRequest(method, url string) *MockHttp {
return &MockHttp{expectedMethod: method, expectedUrl: url, authenticated: true}
}
func (i *MockHttp) Verify(req *http.Request, d *Server) {
Expect(req.Method+" "+req.URL.String()).To(Equal(i.expectedMethod+" "+i.expectedUrl), fmt.Sprintf("Received:\n\t%s\nCompleted mocks:\n%s\nPending mocks:\n%s\n", req.Method+" "+req.URL.String(), strings.Join(d.completedMocks(), "\n"), strings.Join(d.pendingMocks(), "\n")))
if i.authenticated {
if d.expectedAuthorizationHeader != "" {
Expect(req.Header.Get("Authorization")).To(Equal(d.expectedAuthorizationHeader))
}
if i.expectedAuthorizationHeader != "" {
Expect(req.Header.Get("Authorization")).To(Equal(i.expectedAuthorizationHeader))
}
}
if i.expectedContentType != "" {
Expect(req.Header.Get("Content-Type")).To(Equal(i.expectedContentType))
}
rawBody, err := io.ReadAll(req.Body)
Expect(err).NotTo(HaveOccurred())
if i.expectedBody != "" {
Expect(string(rawBody)).To(Equal(i.expectedBody))
}
if i.responseCallback != nil {
i.responseCallback(rawBody)
}
if i.expectedFormValues != nil {
for key, value := range i.expectedFormValues {
Expect(req.FormValue(key)).To(Equal(value))
}
}
Expect(req.Method+" "+req.URL.String()).To(Equal(i.expectedMethod+" "+i.expectedUrl), fmt.Sprintf("Received:\n\t%s\nCompleted mocks:\n%s\nPending mocks:\n%s\n", req.Method+" "+req.URL.String(), strings.Join(d.completedMocks(), "\n"), strings.Join(d.pendingMocks(), "\n")))
}
func (i *MockHttp) Respond(writer http.ResponseWriter, logger *log.Logger) {
if len(i.responseRedirectToUrl) != 0 {
logger.Printf("Redirecting to %s\n", i.responseRedirectToUrl)
writer.Header().Set("Location", i.responseRedirectToUrl)
}
logger.Printf("Reponding with code(%d)\n%s\n", i.responseStatus, i.responseBody)
writer.WriteHeader(i.responseStatus)
io.WriteString(writer, i.responseBody)
}
func (i *MockHttp) For(comment string) *MockHttp {
return i
}
func (i *MockHttp) Url() string {
return i.expectedMethod + " " + i.expectedUrl
}
func (i *MockHttp) SetResponseCallback(b func(rawbody []byte)) {
i.responseCallback = b
}