]> git.giacomolli.uk - onechat/blobdiff - botapi_test.go
Talk to the agent through a Telegram bot
[onechat] / botapi_test.go
index 565f27d061acfbe5d1444b4dab21a4b27c599aa1..b571c6ae0e625fc1d6b1704ad40f9f60be901a82 100644 (file)
@@ -141,7 +141,9 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
 
 // fakeBot is a fake Bot API server for testBotToken. getUpdates returns the
 // added updates from the offset on, or waits a short time when there are none.
-// The other methods succeed, unless fail gives an error answer for a call.
+// The other methods succeed, unless fail gives an error answer for a call. An
+// answer with HTTP 429 asks for a wait of one second. Fail runs with the mutex
+// held.
 type fakeBot struct {
        *httptest.Server
 
@@ -226,18 +228,22 @@ func (f *fakeBot) serve(w http.ResponseWriter, r *http.Request) {
        }
 
        f.mutex.Lock()
-       fail := f.fail
-       f.mutex.Unlock()
-
        status, description := 0, ""
-       if fail != nil {
-               status, description = fail(call)
+       if f.fail != nil {
+               status, description = f.fail(call)
        }
 
+       f.mutex.Unlock()
+
        if status != 0 {
                f.record(call)
                w.WriteHeader(status)
-               _, _ = fmt.Fprintf(w, `{"ok":false,"error_code":%d,"description":%q,"parameters":{"retry_after":1}}`, status, description)
+               wait := 0
+               if status == http.StatusTooManyRequests {
+                       wait = 1
+               }
+
+               _, _ = fmt.Fprintf(w, `{"ok":false,"error_code":%d,"description":%q,"parameters":{"retry_after":%d}}`, status, description, wait)
 
                return
        }