-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathhttp_mock.go
79 lines (71 loc) · 1.81 KB
/
http_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
package mock
import (
"bytes"
"errors"
"io"
"net/http"
"strconv"
"strings"
"time"
)
type HTTP struct {
Req http.Request
RateLimit bool
HasBeenCalled bool
EndRatelimit time.Time
}
func (m *HTTP) Do(req *http.Request) (*http.Response, error) {
m.HasBeenCalled = true
m.Req = *req
success := &http.Response{
Status: "OK",
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader([]byte(`test-flag:
variations:
true_var: true
false_var: false
targeting:
- query: key eq "random-key"
percentage:
true_var: 0
false_var: 100
defaultRule:
variation: false_var
`))),
}
error := &http.Response{
Status: "KO",
StatusCode: http.StatusInternalServerError,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
}
ratelimitReset := m.EndRatelimit
if ratelimitReset.IsZero() {
ratelimitReset = time.Now().Add(1 * time.Hour)
}
rateLimit := &http.Response{
Status: "Rate Limit",
StatusCode: http.StatusTooManyRequests,
Body: io.NopCloser(bytes.NewReader([]byte(""))),
Header: map[string][]string{
"X-Content-Type-Options": {"nosniff"},
"X-Frame-Options": {"deny"},
"X-Github-Media-Type": {"github.v3; format=json"},
"X-Github-Request-Id": {"F82D:37B98C:232EF263:235C93BD:6650BDC6"},
"X-Ratelimit-Limit": {"60"},
"X-Ratelimit-Remaining": {"0"},
"X-Ratelimit-Reset": {strconv.FormatInt(ratelimitReset.Unix(), 10)},
"X-Ratelimit-Resource": {"core"},
"X-Ratelimit-Used": {"60"},
"X-Xss-Protection": {"1; mode=block"},
},
}
if m.RateLimit {
return rateLimit, nil
}
if strings.Contains(req.URL.String(), "error") {
return nil, errors.New("http error")
} else if strings.HasSuffix(req.URL.String(), "httpError") {
return error, nil
}
return success, nil
}