-
Notifications
You must be signed in to change notification settings - Fork 4
/
reporter_test.go
127 lines (109 loc) · 2.68 KB
/
reporter_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
package usage
import (
"bytes"
"context"
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/catalinc/hashcash"
)
func TestReport(t *testing.T) {
done := make(chan struct{}, 10)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
hasher := hashcash.New(hashBits, saltChars, defaultExtension)
totReport := int32(5)
var reports int32
reportEndpoint := "/r"
sessionEndpoint := "/s"
expectedExtraPayload := make([]byte, 1024*1024)
rand.Read(expectedExtraPayload)
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
if req.URL.String() == sessionEndpoint {
rw.Header().Set("Content-Type", "application/json")
fmt.Fprintf(rw, `{"token":"some_token_value"}`)
return
}
if req.URL.String() != reportEndpoint {
t.Errorf("unknown endpoint %s", req.URL.String())
http.Error(rw, "not found", 404)
done <- struct{}{}
return
}
tot := atomic.AddInt32(&reports, 1)
if tot >= totReport+1 {
done <- struct{}{}
return
}
var msg ReportRequest
if err := json.NewDecoder(req.Body).Decode(&msg); err != nil {
t.Error(err)
}
req.Body.Close()
if !hasher.Check(msg.Pow) {
t.Errorf("wrong pow: %s", msg.Pow)
}
d, err := msg.Data.Hash()
if err != nil {
t.Error(err)
}
if !strings.Contains(msg.Pow, d) {
t.Errorf("pow with unexpected hash. have: %s want: %s", msg.Pow, d)
}
if msg.Data.Expired() {
t.Errorf("expired pow. have: %s", time.Unix(msg.Data.Time, 0))
}
if tot == 1 {
if msg.Data.Extra == nil || len(msg.Data.Extra) == 0 {
t.Error("nil extra")
http.Error(rw, "crash", 500)
return
}
if !bytes.Equal(msg.Data.Extra, expectedExtraPayload) {
t.Errorf("unexpected extra payload. have %s", string(msg.Data.Extra))
http.Error(rw, "crash", 500)
return
}
} else {
if msg.Data.Extra != nil && len(msg.Data.Extra) != 0 {
t.Error("unexpected extra payload")
http.Error(rw, "crash", 500)
return
}
}
rw.Header().Set("Content-Type", "application/json")
fmt.Fprintf(rw, `{"status":200}`)
}))
defer s.Close()
<-time.After(100 * time.Millisecond)
if err := Report(
ctx,
Options{
ClusterID: "clusterId",
ServerID: "serverId",
URL: s.URL,
ExtraPayload: expectedExtraPayload,
ReportLapse: 2 * time.Second,
UserAgent: "foo bar",
ReportEndpoint: reportEndpoint,
SessionEndpoint: sessionEndpoint,
},
nil,
); err != nil {
t.Error(err)
return
}
select {
case <-ctx.Done():
case <-done:
}
if reports != totReport+1 {
t.Errorf("unexpected number of reports: %d", reports)
}
}