GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — wumpkit/requester_test.go at master · wumpkit/wumpkit

Expand file tree

/

requester_test.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
168 lines (143 loc) · 4.5 KB
/

requester_test.go

Copy path

File metadata and controls

168 lines (143 loc) · 4.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package wumpkit
import (
"context"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRequester_DoRequest_Success(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bot test_token" {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
if r.Header.Get("X-Audit-Log-Reason") != "test reason" {
http.Error(w, "bad reason", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"id":"123","username":"wumpkit_bot"}`))
}))
defer ts.Close()
cfg := DefaultRequesterConfig()
cfg.BaseURL = ts.URL
req := newRequester("test_token", cfg, slog.Default())
body, err := req.DoRequest(context.Background(), Request{
Method: http.MethodGet,
URL: "/users/@me",
Reason: "test reason",
})
require.NoError(t, err)
defer body.Close()
data, err := io.ReadAll(body)
require.NoError(t, err)
var u User
err = json.Unmarshal(data, &u)
require.NoError(t, err)
assert.Equal(t, Snowflake(123), u.ID)
assert.Equal(t, "wumpkit_bot", u.Username)
}
func TestRequester_NonRetryableError(t *testing.T) {
var attempts atomic.Int32
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempts.Add(1)
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"code":10003,"message":"Unknown Channel"}`))
}))
defer ts.Close()
cfg := DefaultRequesterConfig()
cfg.BaseURL = ts.URL
cfg.MaxRetries = 3
req := newRequester("test_token", cfg, slog.Default())
_, err := req.DoRequest(context.Background(), Request{
Method: http.MethodGet,
URL: "/channels/999",
})
require.Error(t, err)
assert.Equal(t, int32(1), attempts.Load(), "non-retryable errors (404) must not be retried")
assert.False(t, errors.Is(err, ErrMaxRetriesExceeded))
assert.Contains(t, err.Error(), "404")
assert.Contains(t, err.Error(), "Unknown Channel")
}
func TestRequester_RetryableStatus_ExceedsRetries(t *testing.T) {
var attempts atomic.Int32
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
attempts.Add(1)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"code":0,"message":"Internal Server Error"}`))
}))
defer ts.Close()
cfg := DefaultRequesterConfig()
cfg.BaseURL = ts.URL
cfg.MaxRetries = 2
req := newRequester("test_token", cfg, slog.Default())
_, err := req.DoRequest(context.Background(), Request{
Method: http.MethodGet,
URL: "/guilds/123",
})
require.Error(t, err)
assert.Equal(t, int32(3), attempts.Load(), "initial attempt (1) + 2 retries = 3 total attempts")
assert.True(
t,
errors.Is(err, ErrMaxRetriesExceeded),
"must return sentinel ErrMaxRetriesExceeded",
)
}
func TestRequester_RateLimit_RetriesAndSucceeds(t *testing.T) {
var attempts atomic.Int32
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
count := attempts.Add(1)
if count == 1 {
w.Header().Set("Retry-After", "0.05")
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte(`{"message":"You are being rate limited."}`))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"success":true}`))
}))
defer ts.Close()
cfg := DefaultRequesterConfig()
cfg.BaseURL = ts.URL
cfg.MaxRetries = 3
req := newRequester("test_token", cfg, slog.Default())
body, err := req.DoRequest(context.Background(), Request{
Method: http.MethodGet,
URL: "/gateway",
})
require.NoError(t, err)
defer body.Close()
assert.Equal(t, int32(2), attempts.Load())
}
func TestRequester_ContextCancelled_AbortsImmediately(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer ts.Close()
ctx, cancel := context.WithCancel(context.Background())
cancel() // pre-cancel context
cfg := DefaultRequesterConfig()
cfg.BaseURL = ts.URL
cfg.MaxRetries = 3
req := newRequester("test_token", cfg, slog.Default())
_, err := req.DoRequest(ctx, Request{
Method: http.MethodGet,
URL: "/guilds/123",
})
require.ErrorIs(t, err, context.Canceled)
}
func TestComputeBackoff_Jitter(t *testing.T) {
for attempt := range 5 {
b := computeBackoff(attempt, 50*time.Millisecond, 2*time.Second)
assert.GreaterOrEqual(t, b, 25*time.Millisecond)
assert.LessOrEqual(t, b, 2*time.Second)
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.