-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhttp_client_mock.go
96 lines (82 loc) · 2.43 KB
/
http_client_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
package mailjet
import (
"errors"
"fmt"
"log"
"net/http"
"github.com/mailjet/mailjet-apiv3-go/v4/fixtures"
)
// HTTPClientMock definition
type HTTPClientMock struct {
client *http.Client
apiKeyPublic string
apiKeyPrivate string
headers map[string]string
request *http.Request
response interface{}
validCreds bool
fx *fixtures.Fixtures
CallFunc func() (int, int, error)
SendMailV31Func func(req *http.Request) (*http.Response, error)
}
// NewhttpClientMock instanciate new httpClientMock
func NewhttpClientMock(valid bool) *HTTPClientMock {
return &HTTPClientMock{
apiKeyPublic: "apiKeyPublic",
apiKeyPrivate: "apiKeyPrivate",
client: http.DefaultClient,
validCreds: valid,
fx: fixtures.New(),
CallFunc: func() (int, int, error) {
if valid {
return 1, 1, nil
}
return 0, 0, errors.New("Unexpected error: Unexpected server response code: 401: EOF")
},
SendMailV31Func: func(req *http.Request) (*http.Response, error) {
return nil, errors.New("mock send mail function not implemented yet")
},
}
}
// APIKeyPublic returns the public key.
func (c *HTTPClientMock) APIKeyPublic() string {
return c.apiKeyPublic
}
// APIKeyPrivate returns the secret key.
func (c *HTTPClientMock) APIKeyPrivate() string {
return c.apiKeyPrivate
}
// Client returns the underlying http client
func (c *HTTPClientMock) Client() *http.Client {
return c.client
}
// SetClient allow to set the underlying http client
func (c *HTTPClientMock) SetClient(client *http.Client) {
c.client = client
}
// Send data through HTTP with the current configuration
func (c *HTTPClientMock) Send(req *http.Request) HTTPClientInterface {
c.request = req
return c
}
// With lets you set the http header and returns the httpClientMock with the header modified
func (c *HTTPClientMock) With(headers map[string]string) HTTPClientInterface {
c.headers = headers
return c
}
// Read allow you to bind the response received through the underlying http client
func (c *HTTPClientMock) Read(response interface{}) HTTPClientInterface {
err := c.fx.Read(response)
if err != nil {
log.Println(fmt.Errorf("c.fx.Read: %w", err))
}
return c
}
// SendMailV31 mock function
func (c *HTTPClientMock) SendMailV31(req *http.Request) (*http.Response, error) {
return c.SendMailV31Func(req)
}
// Call the mailjet API
func (c *HTTPClientMock) Call() (int, int, error) {
return c.CallFunc()
}