-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.go
108 lines (93 loc) · 2.29 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package esresponse_test
import (
"bytes"
"context"
"fmt"
"os"
"testing"
esresponse "github.com/Bofry/elasticsearch-esresponse"
"github.com/elastic/go-elasticsearch/esapi"
"github.com/elastic/go-elasticsearch/v7"
)
func TestSearch(t *testing.T) {
setupSearchTestCase()
cfg := elasticsearch.Config{
Addresses: []string{
os.Getenv("ELASTICSEARCH_ADDRESS"),
},
}
client, err := elasticsearch.NewClient(cfg)
if err != nil {
fmt.Printf("%+v\n", err)
t.Error("should not be error")
}
req := esapi.SearchRequest{
Index: []string{"esresponse-test-event-log-20201228-724001"},
}
resp, err := req.Do(context.Background(), client)
if err != nil {
fmt.Printf("%+v\n", err)
t.Error("should not be error")
}
result, err := esresponse.AsSearchResult(resp.Body)
if err != nil {
fmt.Printf("%+v\n", err)
t.Error("should not be error")
}
// fmt.Printf("%+v\n", resp)
fmt.Printf("%+v\n", result)
resp.Body.Close()
}
func setupSearchTestCase() {
cfg := elasticsearch.Config{
Addresses: []string{
os.Getenv("ELASTICSEARCH_ADDRESS"),
},
}
client, err := elasticsearch.NewClient(cfg)
if err != nil {
panic(err)
}
req := esapi.IndexRequest{
Index: "esresponse-test-event-log-20201228-724001",
DocumentID: "192.168.56.54#0001",
Body: bytes.NewReader(getSearchTestCase()),
Refresh: "true",
}
resp, err := req.Do(context.Background(), client)
if err != nil {
panic(err)
}
if resp.IsError() {
panic(fmt.Errorf("fail get status %s", resp.Status()))
}
}
func getSearchTestCase() []byte {
return []byte(`{
"timestamp": 1560973500123,
"event_id" : "192.168.56.54#0001",
"category" : "WalletService",
"source" : "192.168.56.54",
"type" : "PASS",
"version" : "v0.1.0b",
"message" : "\"GET /downloads/product_2 HTTP/1.1\" 304 0 \"-\" \"Debian APT-HTTP/1.3 (0.9.7.9)\"",
"details" : {
"request": {
"method": "GET",
"path": "/downloads/product_2",
"query_string": "arg1=one&arg2=2&arg3=drei",
"header": "Content-Type: application/json\r\nUser-Agent: Debian APT-HTTP/1.3 (0.9.7.9)\r\n",
"body": "some request body"
},
"response" :{
"status_code": 304,
"header": "X-Response-Header: f/twXyy",
"body": "some response body"
}
},
"metric": {
"elapsed_time": 122,
"response_body_bytes": 138
}
}`)
}