X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/1e2df788bbb17653a6df44acd41da2edb109d403..3b2bf881d484ba1a33ff77a342564b8195a75c80:/telegram_api_test.go?ds=inline diff --git a/telegram_api_test.go b/telegram_api_test.go index f789101..98aa232 100644 --- a/telegram_api_test.go +++ b/telegram_api_test.go @@ -123,6 +123,7 @@ func TestSendCallsTimeOutOnAStalledConnection(t *testing.T) { api.sendTimeout = time.Hour ctx, cancel := context.WithCancel(t.Context()) time.AfterFunc(50*time.Millisecond, cancel) + start = time.Now() err = tc.send(ctx, api) require.Error(t, err) @@ -155,25 +156,28 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) { name: "rate limit waits as asked", reply: func(w http.ResponseWriter) { w.WriteHeader(http.StatusTooManyRequests) + _, _ = io.WriteString(w, `{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 3","parameters":{"retry_after":3}}`) }, retry: true, wait: 3 * time.Second, - errorText: "answered 429", + errorText: "429", }, { name: "server error page is retried", reply: func(w http.ResponseWriter) { w.WriteHeader(http.StatusBadGateway) + _, _ = io.WriteString(w, "bad gateway") }, retry: true, - errorText: "Telegram answered 502 Bad Gateway", + errorText: "502 Bad Gateway", }, { name: "token echoed in a description is hidden", reply: func(w http.ResponseWriter) { w.WriteHeader(http.StatusUnauthorized) + _, _ = io.WriteString(w, `{"ok":false,"error_code":401,"description":"Unauthorized: bot`+telegramTestToken+`"}`) }, retry: false, @@ -183,30 +187,33 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) { name: "bad request is a refusal", reply: func(w http.ResponseWriter) { w.WriteHeader(http.StatusBadRequest) + _, _ = io.WriteString(w, `{"ok":false,"error_code":400,"description":"Bad Request: can't parse"}`) }, retry: false, rejected: true, - errorText: "answered 400 Bad Request: can't parse", + errorText: "400 Bad Request: can't parse", }, { name: "unknown method is a refusal", reply: func(w http.ResponseWriter) { w.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(w, `{"ok":false,"error_code":404,"description":"Not Found"}`) }, retry: false, rejected: true, - errorText: "answered 404 Not Found", + errorText: "404 Not Found", }, { name: "not found page of a proxy is not a refusal", reply: func(w http.ResponseWriter) { w.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(w, "not found") }, retry: false, - errorText: "answered 404 Not Found", + errorText: "404 Not Found", }, { name: "broken answer is retried", @@ -214,13 +221,13 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) { _, _ = io.WriteString(w, `{"ok":`) }, retry: true, - errorText: "not valid JSON", + errorText: "invalid JSON", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { tc.reply(w) })) t.Cleanup(server.Close)