2020-04-05 18:29:05 +00:00
|
|
|
package telebot
|
|
|
|
|
2020-04-05 18:32:38 +00:00
|
|
|
import (
|
2020-04-05 19:42:07 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
2020-04-05 18:32:38 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2020-04-05 18:29:05 +00:00
|
|
|
|
2020-04-05 19:42:07 +00:00
|
|
|
// testPayload implements json.Marshaler
|
|
|
|
// to test json encoding error behaviour.
|
|
|
|
type testPayload struct{}
|
|
|
|
|
|
|
|
func (testPayload) MarshalJSON() ([]byte, error) {
|
|
|
|
return nil, errors.New("test error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRawServer(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch {
|
|
|
|
// causes EOF error on ioutil.ReadAll
|
|
|
|
case strings.HasSuffix(r.URL.Path, "/testReadError"):
|
|
|
|
// tells the body is 1 byte length but actually it's 0
|
|
|
|
w.Header().Set("Content-Length", "1")
|
2020-04-25 14:21:19 +00:00
|
|
|
|
2020-04-05 19:42:07 +00:00
|
|
|
// returns unknown telegram error
|
|
|
|
case strings.HasSuffix(r.URL.Path, "/testUnknownError"):
|
|
|
|
data, _ := json.Marshal(struct {
|
|
|
|
Ok bool `json:"ok"`
|
|
|
|
Code int `json:"error_code"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}{
|
|
|
|
Ok: false,
|
|
|
|
Code: 400,
|
|
|
|
Description: "unknown error",
|
|
|
|
})
|
|
|
|
|
|
|
|
w.WriteHeader(400)
|
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 18:29:05 +00:00
|
|
|
func TestRaw(t *testing.T) {
|
2020-04-25 14:21:19 +00:00
|
|
|
if token == "" {
|
|
|
|
t.Skip("TELEBOT_SECRET is required")
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:52:15 +00:00
|
|
|
b, err := newTestBot()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = b.Raw("BAD METHOD", nil)
|
2020-04-05 18:32:38 +00:00
|
|
|
assert.EqualError(t, err, ErrNotFound.Error())
|
2020-04-05 19:42:07 +00:00
|
|
|
|
|
|
|
_, err = b.Raw("", &testPayload{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(testRawServer))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
b.URL = srv.URL
|
|
|
|
b.client = srv.Client()
|
|
|
|
|
|
|
|
_, err = b.Raw("testReadError", nil)
|
|
|
|
assert.EqualError(t, err, "telebot: "+io.ErrUnexpectedEOF.Error())
|
|
|
|
|
|
|
|
_, err = b.Raw("testUnknownError", nil)
|
2020-04-06 13:04:25 +00:00
|
|
|
assert.EqualError(t, err, "telegram unknown: unknown error (400)")
|
2020-04-05 18:29:05 +00:00
|
|
|
}
|