-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils_test.go
75 lines (63 loc) · 1.84 KB
/
utils_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
package stream_test
import (
"io"
"math/rand"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
stream "github.com/GetStream/stream-go2/v8"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func newClient(t *testing.T) (*stream.Client, *mockRequester) {
requester := &mockRequester{}
client, err := stream.New("key", "secret", stream.WithHTTPRequester(requester))
require.NoError(t, err)
return client, requester
}
type mockRequester struct {
req *http.Request
resp string
}
func (m *mockRequester) Do(req *http.Request) (*http.Response, error) {
m.req = req
body := "{}"
if m.resp != "" {
body = m.resp
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(body)),
}, nil
}
func testRequest(t *testing.T, req *http.Request, method, url, body string) {
assert.Equal(t, url, req.URL.String())
assert.Equal(t, method, req.Method)
if req.Method == http.MethodPost {
reqBody, err := io.ReadAll(req.Body)
require.NoError(t, err)
assert.JSONEq(t, body, string(reqBody))
}
headers := req.Header
if headers.Get("X-API-Key") == "" {
assert.NotEmpty(t, headers.Get("Stream-Auth-Type"))
assert.NotEmpty(t, headers.Get("Authorization"))
}
}
func getTime(t time.Time) stream.Time {
st, _ := time.Parse(stream.TimeLayout, t.Truncate(time.Second).Format(stream.TimeLayout))
return stream.Time{Time: st}
}
func newFlatFeedWithUserID(c *stream.Client, userID string) (*stream.FlatFeed, error) {
return c.FlatFeed("flat", userID)
}
func newAggregatedFeedWithUserID(c *stream.Client, userID string) (*stream.AggregatedFeed, error) {
return c.AggregatedFeed("aggregated", userID)
}
func newNotificationFeedWithUserID(c *stream.Client, userID string) (*stream.NotificationFeed, error) {
return c.NotificationFeed("notification", userID)
}