-
Notifications
You must be signed in to change notification settings - Fork 31
/
http_test.go
57 lines (49 loc) · 1.4 KB
/
http_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
package stream_chat
import (
"context"
"errors"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestRateLimit asserts that rate limit headers are correctly decoded into the error object.
// We use DeleteUsers endpoint, it requires a very low number of requests (6/min).
func TestRateLimit(t *testing.T) {
c := initClient(t)
ctx := context.Background()
users := make([]*User, 0, 8)
for i := 0; i < 8; i++ {
users = append(users, randomUser(t, c))
}
for _, u := range users {
_, err := c.DeleteUsers(ctx, []string{u.ID}, DeleteUserOptions{
User: SoftDelete,
Messages: HardDelete,
})
if err != nil {
var apiErr Error
ok := errors.As(err, &apiErr)
require.True(t, ok)
require.Equal(t, http.StatusTooManyRequests, apiErr.StatusCode)
require.NotZero(t, apiErr.RateLimit.Limit)
require.NotZero(t, apiErr.RateLimit.Reset)
require.EqualValues(t, 0, apiErr.RateLimit.Remaining)
return
}
}
}
// TestContextExceeded asserts that the context error is correctly returned.
func TestContextExceeded(t *testing.T) {
c := initClient(t)
user := randomUser(t, c)
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Millisecond)
defer cancel()
_, err := c.DeleteUsers(ctx, []string{user.ID}, DeleteUserOptions{
User: SoftDelete,
Messages: HardDelete,
})
require.Error(t, err)
require.ErrorIs(t, err, context.DeadlineExceeded)
}