summaryrefslogtreecommitdiffstats
path: root/client/go/mock
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2022-02-28 09:44:09 +0100
committerMartin Polden <mpolden@mpolden.no>2022-02-28 14:36:58 +0100
commita3196bae50a7943111d522bf495c3b2380347a2b (patch)
tree6e697e82ce89be70f8b9df6c3653b182a2d4f752 /client/go/mock
parent5405aeffdb6d77fadc2ef196cfc4cd033560c08a (diff)
Extract mock package
Diffstat (limited to 'client/go/mock')
-rw-r--r--client/go/mock/mock.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/client/go/mock/mock.go b/client/go/mock/mock.go
new file mode 100644
index 00000000000..f2fcf9c5960
--- /dev/null
+++ b/client/go/mock/mock.go
@@ -0,0 +1,55 @@
+package mock
+
+import (
+ "bytes"
+ "crypto/tls"
+ "io/ioutil"
+ "net/http"
+ "strconv"
+ "time"
+)
+
+type HTTPClient struct {
+ // The responses to return for future requests. Once a response is consumed, it's removed from this slice
+ nextResponses []httpResponse
+
+ // LastRequest is the last HTTP request made through this
+ LastRequest *http.Request
+
+ // Requests contains all requests made through this
+ Requests []*http.Request
+}
+
+type httpResponse struct {
+ status int
+ body []byte
+}
+
+func (c *HTTPClient) NextStatus(status int) { c.NextResponseBytes(status, nil) }
+
+func (c *HTTPClient) NextResponse(status int, body string) {
+ c.NextResponseBytes(status, []byte(body))
+}
+
+func (c *HTTPClient) NextResponseBytes(status int, body []byte) {
+ c.nextResponses = append(c.nextResponses, httpResponse{status: status, body: body})
+}
+
+func (c *HTTPClient) Do(request *http.Request, timeout time.Duration) (*http.Response, error) {
+ response := httpResponse{status: 200}
+ if len(c.nextResponses) > 0 {
+ response = c.nextResponses[0]
+ c.nextResponses = c.nextResponses[1:]
+ }
+ c.LastRequest = request
+ c.Requests = append(c.Requests, request)
+ return &http.Response{
+ Status: "Status " + strconv.Itoa(response.status),
+ StatusCode: response.status,
+ Body: ioutil.NopCloser(bytes.NewBuffer(response.body)),
+ Header: make(http.Header),
+ },
+ nil
+}
+
+func (c *HTTPClient) UseCertificate(certificates []tls.Certificate) {}