/
mock.go
45 lines (37 loc) · 899 Bytes
/
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
package requests
import (
"io"
"math/rand"
"net/http"
"strings"
"time"
"github.com/runar-rkmedia/gabyoall/logger"
)
func NewMockedEndpoint(l logger.AppLogger, url string) Endpoint {
ts := NewTimeSeriesWithLabel(time.Now())
return NewEndpointWithClient(l, url, &ts, MockHttpClient{})
}
type MockHttpClient struct{}
func (m MockHttpClient) Do(req *http.Request) (*http.Response, error) {
// TODO: replace with a mocked http-client-interface
time.Sleep(time.Millisecond * time.Duration(rand.Int63n(80)+1))
errorType := Unknwon
n := rand.Intn(7)
switch n {
case 1:
errorType = NonOK
case 2:
errorType = ServerTestError
case 3:
errorType = "RandomErr"
case 4:
errorType = "OtherErr"
case 6:
errorType = "MadeUpError"
}
res := http.Response{
StatusCode: rand.Intn(500) + 100,
Body: io.NopCloser(strings.NewReader(string(errorType))),
}
return &res, nil
}