| 1 |
package transport |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/base64" |
| 5 |
"fmt" |
| 6 |
"io/ioutil" |
| 7 |
"net/http" |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
// MockHTTPTransport implements a http.RoundTripper. |
| 12 |
// It can be used to mock http.Client details. |
| 13 |
type MockHTTPTransport struct { |
| 14 |
requests []*http.Request |
| 15 |
responses []*http.Response |
| 16 |
errors []error |
| 17 |
callCount int |
| 18 |
} |
| 19 |
|
| 20 |
// RoundTrip satisties the http.RoundTripper interface. |
| 21 |
// |
| 22 |
// it will store the request to later retrospects and return the stored response |
| 23 |
// or the stored error respectively. |
| 24 |
// callCount will be incremented on each call. |
| 25 |
func (m *MockHTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 26 |
m.checkAndAdaptBoundaries(req) |
| 27 |
m.requests = append(m.requests, req) |
| 28 |
response, err := m.responses[m.callCount], m.errors[m.callCount] |
| 29 |
m.callCount++ |
| 30 |
return response, err |
| 31 |
} |
| 32 |
|
| 33 |
// SetResponsePayload registers a given payload in the transport. |
| 34 |
// The payload will be encoded as Base64 and wrapped into a io.Reader. This also |
| 35 |
// sets a status code of 200 and the error for the given call will be nil. |
| 36 |
// The index of the response will be returned. |
| 37 |
func (m *MockHTTPTransport) SetResponsePayload(payload []byte) int { |
| 38 |
m.init() |
| 39 |
encodedMessage := base64.StdEncoding.EncodeToString(payload) |
| 40 |
reader := strings.NewReader(encodedMessage) |
| 41 |
m.responses = append(m.responses, &http.Response{ |
| 42 |
Body: ioutil.NopCloser(reader), |
| 43 |
ContentLength: int64(len(payload)), |
| 44 |
Status: "200 OK", |
| 45 |
StatusCode: 200, |
| 46 |
Proto: "HTTP/1.0", |
| 47 |
ProtoMajor: 1, |
| 48 |
ProtoMinor: 0, |
| 49 |
}) |
| 50 |
m.errors = append(m.errors, nil) |
| 51 |
return len(m.responses) - 1 |
| 52 |
} |
| 53 |
|
| 54 |
// SetResponsePayloads registers multiple payloads in the transport. |
| 55 |
// The payloads will be encoded as Base64 and wrapped into a io.Reader. This also |
| 56 |
// sets a status code of 200 and the errors for the given calls will be nil. |
| 57 |
// This method will reset any responses or errors previously registered. |
| 58 |
func (m *MockHTTPTransport) SetResponsePayloads(payloads [][]byte) { |
| 59 |
m.init() |
| 60 |
m.responses = make([]*http.Response, len(payloads)) |
| 61 |
m.errors = make([]error, len(payloads)) |
| 62 |
for i, payload := range payloads { |
| 63 |
encodedMessage := base64.StdEncoding.EncodeToString(payload) |
| 64 |
reader := strings.NewReader(encodedMessage) |
| 65 |
m.responses[i] = &http.Response{ |
| 66 |
Body: ioutil.NopCloser(reader), |
| 67 |
ContentLength: int64(len(payload)), |
| 68 |
Status: "200 OK", |
| 69 |
StatusCode: 200, |
| 70 |
Proto: "HTTP/1.0", |
| 71 |
ProtoMajor: 1, |
| 72 |
ProtoMinor: 0, |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// CallCount returns the number of calls received by the transport. |
| 78 |
func (m *MockHTTPTransport) CallCount() int { |
| 79 |
return m.callCount |
| 80 |
} |
| 81 |
|
| 82 |
// Request returns the http.Request for the current index. If there are less |
| 83 |
// requests than the index nil is returned. |
| 84 |
func (m *MockHTTPTransport) Request(index int) *http.Request { |
| 85 |
if len(m.requests) < index { |
| 86 |
return nil |
| 87 |
} |
| 88 |
return m.requests[index] |
| 89 |
} |
| 90 |
|
| 91 |
// Requests returns all http.Requests the transport has received. If there are |
| 92 |
// no requests, an empty slice is returned. |
| 93 |
func (m *MockHTTPTransport) Requests() []*http.Request { |
| 94 |
if m.requests == nil { |
| 95 |
return make([]*http.Request, 0) |
| 96 |
} |
| 97 |
return m.requests |
| 98 |
} |
| 99 |
|
| 100 |
// Error returns the error for the given index. If there are less errors than |
| 101 |
// the index nil is returned. |
| 102 |
func (m *MockHTTPTransport) Error(index int) error { |
| 103 |
if len(m.errors) < index { |
| 104 |
return nil |
| 105 |
} |
| 106 |
return m.errors[index] |
| 107 |
} |
| 108 |
|
| 109 |
// Errors returns all errors registered in the transport. If there is no |
| 110 |
// registered error, an empty slice is returned. |
| 111 |
func (m *MockHTTPTransport) Errors() []error { |
| 112 |
if m.errors == nil { |
| 113 |
return make([]error, 0) |
| 114 |
} |
| 115 |
return m.errors |
| 116 |
} |
| 117 |
|
| 118 |
// Reset resets the registered responses and the received requests. It also sets |
| 119 |
// the callCount to zero. |
| 120 |
func (m *MockHTTPTransport) Reset() { |
| 121 |
m.requests = make([]*http.Request, 0) |
| 122 |
m.responses = make([]*http.Response, 0) |
| 123 |
m.errors = make([]error, 0) |
| 124 |
m.callCount = 0 |
| 125 |
} |
| 126 |
|
| 127 |
func (m *MockHTTPTransport) init() { |
| 128 |
if m.requests == nil { |
| 129 |
m.requests = make([]*http.Request, 0) |
| 130 |
} |
| 131 |
if m.responses == nil { |
| 132 |
m.responses = make([]*http.Response, 0) |
| 133 |
} |
| 134 |
if m.errors == nil { |
| 135 |
m.errors = make([]error, 0) |
| 136 |
} |
| 137 |
m.callCount = 0 |
| 138 |
} |
| 139 |
|
| 140 |
func (m *MockHTTPTransport) checkAndAdaptBoundaries(req *http.Request) { |
| 141 |
if m.requests == nil { |
| 142 |
m.requests = make([]*http.Request, m.callCount) |
| 143 |
} |
| 144 |
if len(m.responses) <= m.callCount { |
| 145 |
if m.responses == nil { |
| 146 |
m.responses = make([]*http.Response, m.callCount) |
| 147 |
} else { |
| 148 |
m.responses = append(m.responses, nil) |
| 149 |
} |
| 150 |
} |
| 151 |
if len(m.errors) <= m.callCount { |
| 152 |
if m.errors == nil { |
| 153 |
m.errors = make([]error, m.callCount) |
| 154 |
} else { |
| 155 |
bodyBytes, _ := ioutil.ReadAll(req.Body) |
| 156 |
m.errors = append(m.errors, fmt.Errorf("Unexpected request: %+#v\nBody: %q", req, bodyBytes)) |
| 157 |
} |
| 158 |
} |
| 159 |
} |