-
Notifications
You must be signed in to change notification settings - Fork 13
/
mockhttpclient.go
132 lines (112 loc) · 2.86 KB
/
mockhttpclient.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
package api
import (
"fmt"
"io"
"net/http"
"os"
"sync"
log "github.com/Axway/agent-sdk/pkg/util/log"
)
// MockHTTPClient - use for mocking the HTTP client
type MockHTTPClient struct {
Client
Response *Response // this for if you want to set your own dummy response
ResponseCode int // this for if you only care about a particular response code
ResponseError error
RespCount int
Responses []MockResponse
Requests []Request // lists all requests the client has received
sync.Mutex
}
// MockResponse - use for mocking the MockHTTPClient responses
type MockResponse struct {
FileName string
RespData string
RespCode int
ErrString string
}
// SetResponse -
// if you care about the response content and the code, pass both in
// if you only care about the code, pass "" for the filepath
func (c *MockHTTPClient) SetResponse(filepath string, code int) {
var dat []byte
if filepath != "" {
responseFile, err := os.Open(filepath)
if err != nil {
c.ResponseCode = http.StatusInternalServerError
return
}
dat, err = io.ReadAll(responseFile)
if err != nil {
c.ResponseCode = http.StatusInternalServerError
return
}
}
c.Response = &Response{
Code: code,
Body: dat,
Headers: map[string][]string{},
}
}
// SetResponses -
// if you care about the response content and the code, pass both in
// if you only care about the code, pass "" for the filepath
func (c *MockHTTPClient) SetResponses(responses []MockResponse) {
c.Lock()
defer c.Unlock()
c.RespCount = 0
c.Responses = responses
}
// Send -
func (c *MockHTTPClient) Send(request Request) (*Response, error) {
c.Lock()
defer c.Unlock()
c.Requests = append(c.Requests, request)
fmt.Printf("%v - %v\n", request.Method, request.URL)
if c.Responses != nil && len(c.Responses) > 0 {
return c.sendMultiple(request)
}
if c.Response != nil {
return c.Response, nil
}
if c.ResponseError != nil {
return nil, c.ResponseError
}
if c.ResponseCode != 0 {
return &Response{
Code: c.ResponseCode,
}, nil
}
return nil, nil
}
func (c *MockHTTPClient) sendMultiple(request Request) (*Response, error) {
var err error
if c.RespCount >= len(c.Responses) {
err := fmt.Errorf("error: received more requests than saved responses. failed on request: %s", request.URL)
log.Error(err)
return nil, err
}
fileName := c.Responses[c.RespCount].FileName
dat := []byte(c.Responses[c.RespCount].RespData)
var responseFile *os.File
if fileName != "" {
responseFile, err = os.Open(fileName)
if err != nil {
return nil, err
}
dat, err = io.ReadAll(responseFile)
if err != nil {
return nil, err
}
}
response := Response{
Code: c.Responses[c.RespCount].RespCode,
Body: dat,
Headers: map[string][]string{},
}
if c.Responses[c.RespCount].ErrString != "" {
err = fmt.Errorf(c.Responses[c.RespCount].ErrString)
}
c.RespCount++
return &response, err
}