github.com/meowstercatel/discordgo@v0.0.0-20250615204139-29b43d676735/restapi_test.go (about) 1 package discordgo 2 3 import ( 4 "context" 5 "errors" 6 "net/http" 7 "testing" 8 ) 9 10 ////////////////////////////////////////////////////////////////////////////// 11 /////////////////////////////////////////////////////////////// START OF TESTS 12 13 // TestChannelMessageSend tests the ChannelMessageSend() function. This should not return an error. 14 func TestChannelMessageSend(t *testing.T) { 15 16 if envChannel == "" { 17 t.Skip("Skipping, DG_CHANNEL not set.") 18 } 19 20 if dg == nil { 21 t.Skip("Skipping, dg not set.") 22 } 23 24 _, err := dg.ChannelMessageSend(envChannel, "Running REST API Tests!") 25 if err != nil { 26 t.Errorf("ChannelMessageSend returned error: %+v", err) 27 } 28 } 29 30 /* 31 // removed for now, only works on BOT accounts now 32 func TestUserAvatar(t *testing.T) { 33 34 if dg == nil { 35 t.Skip("Cannot TestUserAvatar, dg not set.") 36 } 37 38 u, err := dg.User("@me") 39 if err != nil { 40 t.Error("error fetching @me user,", err) 41 } 42 43 a, err := dg.UserAvatar(u.ID) 44 if err != nil { 45 if err.Error() == `HTTP 404 NOT FOUND, {"code": 0, "message": "404: Not Found"}` { 46 t.Skip("Skipped, @me doesn't have an Avatar") 47 } 48 t.Errorf(err.Error()) 49 } 50 51 if a == nil { 52 t.Errorf("a == nil, should be image.Image") 53 } 54 } 55 */ 56 57 /* Running this causes an error due to 2/hour rate limit on username changes 58 func TestUserUpdate(t *testing.T) { 59 if dg == nil { 60 t.Skip("Cannot test logout, dg not set.") 61 } 62 63 u, err := dg.User("@me") 64 if err != nil { 65 t.Errorf(err.Error()) 66 } 67 68 s, err := dg.UserUpdate(envEmail, envPassword, "testname", u.Avatar, "") 69 if err != nil { 70 t.Error(err.Error()) 71 } 72 if s.Username != "testname" { 73 t.Error("Username != testname") 74 } 75 s, err = dg.UserUpdate(envEmail, envPassword, u.Username, u.Avatar, "") 76 if err != nil { 77 t.Error(err.Error()) 78 } 79 if s.Username != u.Username { 80 t.Error("Username != " + u.Username) 81 } 82 } 83 */ 84 85 //func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) { 86 87 func TestUserChannelCreate(t *testing.T) { 88 if dg == nil { 89 t.Skip("Cannot TestUserChannelCreate, dg not set.") 90 } 91 92 if envAdmin == "" { 93 t.Skip("Skipped, DG_ADMIN not set.") 94 } 95 96 _, err := dg.UserChannelCreate(envAdmin) 97 if err != nil { 98 t.Errorf(err.Error()) 99 } 100 101 // TODO make sure the channel was added 102 } 103 104 func TestUserGuilds(t *testing.T) { 105 if dg == nil { 106 t.Skip("Cannot TestUserGuilds, dg not set.") 107 } 108 109 _, err := dg.UserGuilds(10, "", "", false) 110 if err != nil { 111 t.Errorf(err.Error()) 112 } 113 } 114 115 func TestGateway(t *testing.T) { 116 117 if dg == nil { 118 t.Skip("Skipping, dg not set.") 119 } 120 _, err := dg.Gateway() 121 if err != nil { 122 t.Errorf("Gateway() returned error: %+v", err) 123 } 124 } 125 126 func TestGatewayBot(t *testing.T) { 127 128 if dgBot == nil { 129 t.Skip("Skipping, dgBot not set.") 130 } 131 _, err := dgBot.GatewayBot() 132 if err != nil { 133 t.Errorf("GatewayBot() returned error: %+v", err) 134 } 135 } 136 137 func TestVoiceRegions(t *testing.T) { 138 139 if dg == nil { 140 t.Skip("Skipping, dg not set.") 141 } 142 143 _, err := dg.VoiceRegions() 144 if err != nil { 145 t.Errorf("VoiceRegions() returned error: %+v", err) 146 } 147 } 148 func TestGuildRoles(t *testing.T) { 149 150 if envGuild == "" { 151 t.Skip("Skipping, DG_GUILD not set.") 152 } 153 154 if dg == nil { 155 t.Skip("Skipping, dg not set.") 156 } 157 158 _, err := dg.GuildRoles(envGuild) 159 if err != nil { 160 t.Errorf("GuildRoles(envGuild) returned error: %+v", err) 161 } 162 163 } 164 165 func TestGuildMemberNickname(t *testing.T) { 166 167 if envGuild == "" { 168 t.Skip("Skipping, DG_GUILD not set.") 169 } 170 171 if dg == nil { 172 t.Skip("Skipping, dg not set.") 173 } 174 175 err := dg.GuildMemberNickname(envGuild, "@me/nick", "B1nzyRocks") 176 if err != nil { 177 t.Errorf("GuildNickname returned error: %+v", err) 178 } 179 } 180 181 // TestChannelMessageSend2 tests the ChannelMessageSend() function. This should not return an error. 182 func TestChannelMessageSend2(t *testing.T) { 183 184 if envChannel == "" { 185 t.Skip("Skipping, DG_CHANNEL not set.") 186 } 187 188 if dg == nil { 189 t.Skip("Skipping, dg not set.") 190 } 191 192 _, err := dg.ChannelMessageSend(envChannel, "All done running REST API Tests!") 193 if err != nil { 194 t.Errorf("ChannelMessageSend returned error: %+v", err) 195 } 196 } 197 198 // TestGuildPruneCount tests GuildPruneCount() function. This should not return an error. 199 func TestGuildPruneCount(t *testing.T) { 200 201 if envGuild == "" { 202 t.Skip("Skipping, DG_GUILD not set.") 203 } 204 205 if dg == nil { 206 t.Skip("Skipping, dg not set.") 207 } 208 209 _, err := dg.GuildPruneCount(envGuild, 1) 210 if err != nil { 211 t.Errorf("GuildPruneCount returned error: %+v", err) 212 } 213 } 214 215 /* 216 // TestGuildPrune tests GuildPrune() function. This should not return an error. 217 func TestGuildPrune(t *testing.T) { 218 219 if envGuild == "" { 220 t.Skip("Skipping, DG_GUILD not set.") 221 } 222 223 if dg == nil { 224 t.Skip("Skipping, dg not set.") 225 } 226 227 _, err := dg.GuildPrune(envGuild, 1) 228 if err != nil { 229 t.Errorf("GuildPrune returned error: %+v", err) 230 } 231 } 232 */ 233 234 func Test_unmarshal(t *testing.T) { 235 err := unmarshal([]byte{}, &struct{}{}) 236 if !errors.Is(err, ErrJSONUnmarshal) { 237 t.Errorf("Unexpected error type: %T", err) 238 } 239 } 240 241 func TestWithContext(t *testing.T) { 242 // Set up a test context. 243 type key struct{} 244 ctx := context.WithValue(context.Background(), key{}, "value") 245 246 // Set up a test client. 247 session, err := New("") 248 if err != nil { 249 t.Fatal(err) 250 } 251 252 testErr := errors.New("test") 253 254 // Intercept the request to assert the context. 255 session.Client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) { 256 val, _ := r.Context().Value(key{}).(string) 257 if val != "value" { 258 t.Errorf("missing value in context (got %q, wanted %q)", val, "value") 259 } 260 return nil, testErr 261 }) 262 263 // Run any client method using WithContext. 264 _, err = session.User("", WithContext(ctx)) 265 266 // Verify that the assertion code was actually run. 267 if !errors.Is(err, testErr) { 268 t.Errorf("unexpected error %v returned from client", err) 269 } 270 } 271 272 // roundTripperFunc implements http.RoundTripper. 273 type roundTripperFunc func(*http.Request) (*http.Response, error) 274 275 func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { 276 return f(req) 277 }