forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
39 lines (35 loc) · 1.26 KB
/
errors_test.go
File metadata and controls
39 lines (35 loc) · 1.26 KB
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
package telebot
import (
"errors"
"fmt"
"strings"
"testing"
)
// Regression for #770. The stdlib HTTP client embeds the full request
// URL in transport errors, and that URL contains /bot<TOKEN>/<method>.
// wrapError used to surface that token verbatim. It now redacts the
// token before wrapping.
func TestWrapErrorRedactsBotToken(t *testing.T) {
token := "123456789:AAEhBP0av28DKrJB1ULTGYojCZ0HEYJBotk"
rawErr := fmt.Errorf(`Post "https://api.telegram.org/bot%s/getFile": dial: timeout`, token)
wrapped := wrapError(rawErr)
if strings.Contains(wrapped.Error(), token) {
t.Fatalf("wrapped error leaked bot token: %q", wrapped.Error())
}
if !strings.Contains(wrapped.Error(), "/bot<token>") {
t.Fatalf("wrapped error missing /bot<token> sentinel: %q", wrapped.Error())
}
if !errors.Is(wrapped, rawErr) {
t.Fatal("wrapped error broke errors.Is chain to the original error")
}
}
func TestWrapErrorLeavesUnrelatedErrorsAlone(t *testing.T) {
original := errors.New("something unrelated")
wrapped := wrapError(original)
if !errors.Is(wrapped, original) {
t.Fatal("expected wrapped error to keep wrapping the original")
}
if want := "telebot: something unrelated"; wrapped.Error() != want {
t.Fatalf("wrapped error = %q, want %q", wrapped.Error(), want)
}
}