1
bot/raw_request_test.go
Andrew Privalov addc1c9e3a
add option UseTestEnvironment (#109)
* add option `UseTestEnvironment` #89

* add test
2024-08-22 11:45:37 +03:00

58 lines
1.0 KiB
Go

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)
}
}