X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/6b21fcf78372995c6d8130c63f2f8aa2609bf7fe..88581c0765be50dcabcc2913c38ae6786c426a3a:/telegram_fake_helper_test.go diff --git a/telegram_fake_helper_test.go b/telegram_fake_helper_test.go index a70a199..3056aa1 100644 --- a/telegram_fake_helper_test.go +++ b/telegram_fake_helper_test.go @@ -50,6 +50,7 @@ type TelegramFake struct { // Add makes update available to getUpdates. func (f *TelegramFake) Add(update TelegramUpdate) { f.mutex.Lock() + f.updates = append(f.updates, update) f.mutex.Unlock() @@ -71,6 +72,7 @@ func (f *TelegramFake) Calls() []TelegramFakeCall { // sendRichMessage calls that succeeded, in order. func (f *TelegramFake) Sent() []string { var texts []string + for _, call := range f.Calls() { switch { case !call.ok: @@ -88,13 +90,16 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { method, found := strings.CutPrefix(r.URL.Path, "/bot"+telegramTestToken+"/") if !found { http.Error(w, `{"ok":false,"error_code":404,"description":"Not Found"}`, http.StatusNotFound) + return } var params map[string]any + err := json.NewDecoder(r.Body).Decode(¶ms) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) + return } @@ -104,6 +109,7 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { } f.mutex.Lock() + status, description := 0, "" if f.fail != nil { status, description = f.fail(call) @@ -114,6 +120,7 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { if stall { f.record(call) + select { case <-r.Context().Done(): case <-f.stopped: @@ -125,6 +132,7 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { if status != 0 { f.record(call) w.WriteHeader(status) + wait := 0 if status == http.StatusTooManyRequests { wait = 1 @@ -137,6 +145,7 @@ func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) { call.ok = true f.record(call) + if method != "getUpdates" { _, _ = io.WriteString(w, `{"ok":true,"result":true}`) return @@ -175,6 +184,7 @@ func (f *TelegramFake) from(offset int64) []TelegramUpdate { defer f.mutex.Unlock() var updates []TelegramUpdate + kept := f.updates[:0] for _, update := range f.updates { if update.UpdateID >= offset {