-
Notifications
You must be signed in to change notification settings - Fork 3
/
activity_test.go
85 lines (65 loc) · 1.65 KB
/
activity_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
package gosnowth
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestRebuildActivity(t *testing.T) {
t.Parallel()
ms := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request,
) {
if r.RequestURI == "/state" {
_, _ = w.Write([]byte(stateTestData))
return
}
if r.RequestURI == "/stats.json" {
_, _ = w.Write([]byte(statsTestData))
return
}
if strings.HasPrefix(r.RequestURI, "/surrogate/activity_rebuild") {
b, err := io.ReadAll(r.Body)
if err != nil {
t.Error("Unable to read request body")
}
if string(b) == "[]\n" {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{ "records": 0, "updated": 0, "misdirected": 0, "errors": 0 }`))
return
}
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte("invalid request body"))
return
}
t.Errorf("Unexpected request: %v", r)
w.WriteHeader(http.StatusInternalServerError)
}))
defer ms.Close()
sc, err := NewClient(context.Background(),
&Config{Servers: []string{ms.URL}})
if err != nil {
t.Fatal("Unable to create snowth client", err)
}
u, err := url.Parse(ms.URL)
if err != nil {
t.Fatal("Invalid test URL")
}
node := &SnowthNode{url: u}
_, err = sc.RebuildActivity(node, []RebuildActivityRequest{})
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err = sc.RebuildActivityContext(ctx, node, []RebuildActivityRequest{})
if err == nil {
t.Fatal("Expected error response")
}
if !strings.Contains(err.Error(), "context") {
t.Errorf("Expected context error, got: %v", err.Error())
}
}