]> git.giacomolli.uk - onechat/blobdiff - telegram_api_test.go
Show an input prompt while the agent is idle
[onechat] / telegram_api_test.go
index f789101a2bf6a86e15ac226258ef60900fb4c80c..98aa232a9539894007921f177c3622c81a88462d 100644 (file)
@@ -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)
                        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)
                        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)
                        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,
                                _, _ = 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)
                },
                {
                        name: "server error page is retried",
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusBadGateway)
+
                                _, _ = io.WriteString(w, "<html>bad gateway</html>")
                        },
                        retry:     true,
                                _, _ = io.WriteString(w, "<html>bad gateway</html>")
                        },
                        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)
                },
                {
                        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,
                                _, _ = 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)
                        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,
                                _, _ = 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)
                },
                {
                        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,
                                _, _ = 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)
                },
                {
                        name: "not found page of a proxy is not a refusal",
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusNotFound)
+
                                _, _ = io.WriteString(w, "<html>not found</html>")
                        },
                        retry:     false,
                                _, _ = io.WriteString(w, "<html>not found</html>")
                        },
                        retry:     false,
-                       errorText: "answered 404 Not Found",
+                       errorText: "404 Not Found",
                },
                {
                        name: "broken answer is retried",
                },
                {
                        name: "broken answer is retried",
@@ -214,13 +221,13 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                                _, _ = io.WriteString(w, `{"ok":`)
                        },
                        retry:     true,
                                _, _ = 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) {
                },
        }
 
        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)
                                tc.reply(w)
                        }))
                        t.Cleanup(server.Close)