From addc1c9e3a47dc9b7779c85a98e815160ce1d147 Mon Sep 17 00:00:00 2001 From: Andrew Privalov Date: Thu, 22 Aug 2024 11:45:37 +0300 Subject: [PATCH] add option `UseTestEnvironment` (#109) * add option `UseTestEnvironment` #89 * add test --- CHANGELOG.md | 4 ++++ README.md | 1 + bot.go | 1 + options.go | 7 ++++++ raw_request.go | 6 ++++- raw_request_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 raw_request_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index b6adebe..6f9c5bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.7.1 (2024-08-22) + +- add option `UseTestEnvironment` for use test environment in API requests + ## v1.7.0 (2024-08-14) - support API v7.9 diff --git a/README.md b/README.md index 1055b52..12db0c6 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...) - `WithAllowedUpdates(params AllowedUpdates)` - set [allowed_updates](https://core.telegram.org/bots/api#getupdates) for getUpdates method - `WithUpdatesChannelCap(cap int)` - set updates channel capacity, by default 1024 - `WithWebhookSecretToken(webhookSecretToken string)` - set X-Telegram-Bot-Api-Secret-Token header sent from telegram servers to confirm validity of update +- `UseTestEnvironment()` - use test environment ## Message.Text and CallbackQuery.Data handlers diff --git a/bot.go b/bot.go index b9b8247..3e5b0ba 100644 --- a/bot.go +++ b/bot.go @@ -35,6 +35,7 @@ type Bot struct { pollTimeout time.Duration skipGetMe bool webhookSecretToken string + testEnvironment bool defaultHandlerFunc HandlerFunc diff --git a/options.go b/options.go index 667c96d..eb7ca5d 100644 --- a/options.go +++ b/options.go @@ -109,3 +109,10 @@ func WithWebhookSecretToken(webhookSecretToken string) Option { b.webhookSecretToken = webhookSecretToken } } + +// UseTestEnvironment allows to use test environment +func UseTestEnvironment() Option { + return func(b *Bot) { + b.testEnvironment = true + } +} diff --git a/raw_request.go b/raw_request.go index edb401c..ee1e2e0 100644 --- a/raw_request.go +++ b/raw_request.go @@ -48,7 +48,11 @@ func (b *Bot) rawRequest(ctx context.Context, method string, params any, dest an } } - u := b.url + "/bot" + b.token + "/" + method + u := b.url + "/bot" + b.token + "/" + if b.testEnvironment { + u += "test/" + } + u += method if b.isDebug && strings.ToLower(method) != "getupdates" { requestDebugData, _ := json.Marshal(params) diff --git a/raw_request_test.go b/raw_request_test.go new file mode 100644 index 0000000..0322563 --- /dev/null +++ b/raw_request_test.go @@ -0,0 +1,57 @@ +package bot + +import ( + "context" + "io" + "net/http" + "strings" + "testing" +) + +type clientMock struct { + requestURI string +} + +func (c *clientMock) Do(req *http.Request) (*http.Response, error) { + c.requestURI = req.URL.RequestURI() + resp := http.Response{ + StatusCode: 200, + Body: io.NopCloser(strings.NewReader(`{"ok":true}`)), + } + return &resp, nil +} + +func Test_rawRequest_url(t *testing.T) { + cm := &clientMock{} + b := &Bot{ + token: "XXX", + client: cm, + } + + err := b.rawRequest(context.Background(), "foo", nil, nil) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if cm.requestURI != "/botXXX/foo" { + t.Fatalf("unexpected requestURI: %s", cm.requestURI) + } +} + +func Test_rawRequest_url_testEnv(t *testing.T) { + cm := &clientMock{} + b := &Bot{ + token: "XXX", + client: cm, + testEnvironment: true, + } + + err := b.rawRequest(context.Background(), "foo", nil, nil) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if cm.requestURI != "/botXXX/test/foo" { + t.Fatalf("unexpected requestURI: %s", cm.requestURI) + } +}