Skip to content

Commit

Permalink
add option UseTestEnvironment (#109)
Browse files Browse the repository at this point in the history
* add option `UseTestEnvironment` #89

* add test
  • Loading branch information
negasus authored Aug 22, 2024
1 parent b3b73e9 commit addc1c9
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Bot struct {
pollTimeout time.Duration
skipGetMe bool
webhookSecretToken string
testEnvironment bool

defaultHandlerFunc HandlerFunc

Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 5 additions & 1 deletion raw_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 57 additions & 0 deletions raw_request_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit addc1c9

Please sign in to comment.