-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_mock_test.go
67 lines (55 loc) · 1.39 KB
/
client_mock_test.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
// Copyright (c) 2015 Serge Gebhardt. All rights reserved.
//
// Use of this source code is governed by the ISC
// license that can be found in the LICENSE file.
package smartcharge
import (
"bytes"
"io/ioutil"
"net/http"
)
// MockResponse is a static HTTP response.
type MockResponse struct {
Code int
Body []byte
}
// NewMockResponseOkString creates a new MockResponse with Code 200 (OK)
// and Body built from string argument
func NewMockResponseOkString(response string) *MockResponse {
return &MockResponse{
Code: 200,
Body: []byte(response),
}
}
// mockTransport is a mocked Transport that always returns the same MockResponse.
type mockTransport struct {
resp MockResponse
}
// Satisfies the RoundTripper interface.
func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
r := http.Response{
StatusCode: t.resp.Code,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
}
if len(t.resp.Body) > 0 {
buf := bytes.NewBuffer(t.resp.Body)
r.Body = ioutil.NopCloser(buf)
}
return &r, nil
}
// MockClient is a mocked Client that is used for tests.
func NewMockClient(response MockResponse) *Client {
t := &mockTransport{resp: response}
c := &http.Client{Transport: t}
auth := &Authentication{
UserId: 123,
CustomerId: 123,
AccessToken: "",
RefreshToken: "",
}
client := NewClient(c)
client.setAuthentication(auth)
return client
}