-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsearch_test.go
92 lines (80 loc) · 1.95 KB
/
search_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
package wavefront
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
)
type MockSearchClient struct {
Client
Response []byte
T *testing.T
isDeleted bool
}
func (m MockSearchClient) Do(req *http.Request) (io.ReadCloser, error) {
p := SearchParams{}
b, _ := ioutil.ReadAll(req.Body)
err := json.Unmarshal(b, &p)
if err != nil {
m.T.Fatal(err)
}
// check defaults
if p.Offset != 0 || p.Limit != 100 {
m.T.Errorf("default offset and limit, expected 0, 100; got %d, %d", p.Offset, p.Limit)
}
if m.isDeleted == true && req.URL.Path != "/api/v2/search/alert/deleted" {
m.T.Errorf("deleted search path expected /api/v2/search/alert/deleted, got %s", req.URL.Path)
}
return ioutil.NopCloser(bytes.NewReader(m.Response)), nil
}
func TestSearch(t *testing.T) {
sc := &SearchCondition{
Key: "tags",
Value: "myTag",
MatchingMethod: "EXACT",
}
sp := &SearchParams{
Conditions: []*SearchCondition{sc},
}
response, err := ioutil.ReadFile("./fixtures/search-alert-response.json")
if err != nil {
t.Fatal(err)
}
baseurl, _ := url.Parse("http://testing.wavefront.com")
s := &Search{
Params: sp,
Type: "alert",
client: &MockSearchClient{
Response: response,
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
},
}
resp, err := s.Execute()
if err != nil {
t.Fatal("error executing query:", err)
}
raw, err := ioutil.ReadAll(resp.RawResponse)
if err != nil {
t.Error(err)
}
if err := json.Unmarshal(raw, new(map[string]interface{})); err != nil {
t.Error("raw response is invalid JSON", err)
}
// check offset of next page in paginated response
if resp.NextOffset != 100 {
t.Errorf("next offset, expected 100, got %d", resp.NextOffset)
}
// check deleted path appended
s.Deleted = true
((s.client).(*MockSearchClient)).isDeleted = true
s.Execute()
}