stall func(call TelegramFakeCall) bool
added chan struct{}
stopped chan struct{}
+ images map[string][]byte
}
// Add makes update available to getUpdates.
func (f *TelegramFake) Add(update TelegramUpdate) {
f.mutex.Lock()
+
f.updates = append(f.updates, update)
f.mutex.Unlock()
// sendRichMessage calls that succeeded, in order.
func (f *TelegramFake) Sent() []string {
var texts []string
+
for _, call := range f.Calls() {
switch {
case !call.ok:
return texts
}
+func (f *TelegramFake) Image(id string, data []byte) {
+ f.mutex.Lock()
+ defer f.mutex.Unlock()
+
+ if f.images == nil {
+ f.images = make(map[string][]byte)
+ }
+
+ f.images[id] = data
+}
+
func (f *TelegramFake) serve(w http.ResponseWriter, r *http.Request) {
+ id, file := strings.CutPrefix(r.URL.Path, "/file/bot"+telegramTestToken+"/")
+ if file {
+ f.mutex.Lock()
+
+ data, exists := f.images[id]
+ f.mutex.Unlock()
+
+ if !exists {
+ http.NotFound(w, r)
+
+ return
+ }
+
+ _, _ = w.Write(data)
+
+ return
+ }
+
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
}
}
f.mutex.Lock()
+
status, description := 0, ""
if f.fail != nil {
status, description = f.fail(call)
if stall {
f.record(call)
+
select {
case <-r.Context().Done():
case <-f.stopped:
if status != 0 {
f.record(call)
w.WriteHeader(status)
+
wait := 0
if status == http.StatusTooManyRequests {
wait = 1
call.ok = true
f.record(call)
+
+ if method == "getFile" {
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "ok": true,
+ "result": map[string]any{"file_path": params["file_id"]},
+ })
+
+ return
+ }
+
if method != "getUpdates" {
_, _ = io.WriteString(w, `{"ok":true,"result":true}`)
return
defer f.mutex.Unlock()
var updates []TelegramUpdate
+
kept := f.updates[:0]
for _, update := range f.updates {
if update.UpdateID >= offset {