-
Notifications
You must be signed in to change notification settings - Fork 16
/
request_test.go
157 lines (135 loc) · 4.43 KB
/
request_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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package profitbricks
import (
"bytes"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
const requestTestDataPath = "testdata"
type SuiteRequest struct {
suite.Suite
client *Client
apiUrl string
}
func (s *SuiteRequest) SetupTest() {
s.client = NewClient("", "")
s.apiUrl = s.client.CloudApiUrl
httpmock.ActivateNonDefault(s.client.CoreSdk.GetConfig().HTTPClient)
}
func (s *SuiteRequest) TearDownTest() {
httpmock.DeactivateAndReset()
}
type SuiteWaitTillRequests struct {
SuiteRequest
}
func Test_WaitTillRequests(t *testing.T) {
suite.Run(t, new(SuiteWaitTillRequests))
}
func loadTestData(t *testing.T, filename string) []byte {
data, err := ioutil.ReadFile(filepath.Join(requestTestDataPath, filename))
assert.NoError(t, err)
return data
}
func (s *SuiteWaitTillRequests) Test_OK_NoSelector() {
listResponses := []*http.Response{
makeJsonResponse(200, loadTestData(s.T(), "request_request_till_no_request_matches_01.json")),
makeJsonResponse(200, loadTestData(s.T(), "request_request_till_no_request_matches_02.json")),
}
statusResponses := []*http.Response{
makeJsonResponse(200, loadTestData(s.T(), "request_queued.json")),
makeJsonResponse(200, loadTestData(s.T(), "request_done.json")),
}
query := url.Values{
"depth": []string{"10"},
}
listCalled := 0
statusCalled := 0
var lr httpmock.Responder = func(req *http.Request) (*http.Response, error) {
rs := listResponses[listCalled]
listCalled++
return rs, nil
}
var sr httpmock.Responder = func(req *http.Request) (*http.Response, error) {
rs := statusResponses[statusCalled]
statusCalled++
return rs, nil
}
httpmock.RegisterResponderWithQuery(http.MethodGet, s.apiUrl+"/requests", query, lr.Times(2))
httpmock.RegisterResponder(http.MethodGet, "=~/requests/.*/status.*", sr.Times(2))
ctx, cancel := s.client.GetContext()
if cancel != nil {
defer cancel()
}
err := s.client.WaitTillRequestsFinished(ctx, NewRequestListFilter())
s.NoError(err)
s.Equal(4, httpmock.GetTotalCallCount())
}
func (s *SuiteWaitTillRequests) Test_Err_ListError() {
httpmock.RegisterResponder(http.MethodGet, "=~/requests",
httpmock.NewStringResponder(401, "{}"))
ctx, cancel := s.client.GetContext()
if cancel != nil {
defer cancel()
}
err := s.client.WaitTillMatchingRequestsFinished(ctx, nil, nil)
s.Error(err)
s.Equal(1, httpmock.GetTotalCallCount())
}
func makeJsonResponse(status int, data []byte) *http.Response {
body := ioutil.NopCloser(bytes.NewReader(data))
rsp := http.Response{Body: body, Header: http.Header{}, StatusCode: status, Status: http.StatusText(status)}
rsp.Header.Set("Content-Type", "application/json")
return &rsp
}
func (s *SuiteWaitTillRequests) Test_Err_GetStatusError() {
rsp := loadTestData(s.T(), "request_request_till_no_request_matches_01.json")
listResponse := makeJsonResponse(http.StatusOK, rsp)
httpmock.RegisterResponder(http.MethodGet, `=~/requests\?.*`,
httpmock.ResponderFromResponse(listResponse))
statusResponse := makeJsonResponse(http.StatusUnauthorized, []byte("{}"))
httpmock.RegisterResponder(http.MethodGet, "=~/requests/.*/status.*",
httpmock.ResponderFromResponse(statusResponse))
ctx, cancel := s.client.GetContext()
if cancel != nil {
defer cancel()
}
err := s.client.WaitTillRequestsFinished(ctx, nil)
s.Error(err)
s.Equal(2, httpmock.GetTotalCallCount())
}
func TestRequestListFilter_AddCreatedAfter(t *testing.T) {
r := NewRequestListFilter()
tm, err := time.Parse(timeFormat, "2019-03-25 10:40:00")
assert.NoError(t, err)
r.AddCreatedAfter(tm)
assert.Equal(t, "2019-03-25 10:40:00", r.Get("filter.createdAfter"))
}
func TestRequestListFilter_AddCreatedBefore(t *testing.T) {
r := NewRequestListFilter()
tm, err := time.Parse(timeFormat, "2019-03-25 10:40:00")
assert.NoError(t, err)
r.AddCreatedBefore(tm)
assert.Equal(t, "2019-03-25 10:40:00", r.Get("filter.createdBefore"))
}
func TestRequestListFilter_AddRequestStatus(t *testing.T) {
r := NewRequestListFilter()
r.AddRequestStatus(RequestStatusDone)
assert.Equal(t, RequestStatusDone, r.Get("filter.status"))
}
func TestRequestListFilter_Clone(t *testing.T) {
r := NewRequestListFilter()
r.AddMethod(http.MethodPost)
r.AddUrl("foo/bar")
c := r.Clone()
assert.Equal(t, r, c)
r.AddBody("should not be in copy")
assert.NotEqual(t, r, c)
assert.Contains(t, r.Values, "filter.body")
assert.NotContains(t, c.Values, "filter.body")
}