63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package clientmock
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
type DiscordClientMock struct {
|
|
mock.Mock
|
|
|
|
BotToken string
|
|
ClientID string
|
|
ClientSecret string
|
|
}
|
|
|
|
func NewDiscordClientMock() *DiscordClientMock {
|
|
dcm := &DiscordClientMock{
|
|
BotToken: "bot-token",
|
|
ClientID: "client-id",
|
|
ClientSecret: "client-secret",
|
|
}
|
|
|
|
dcm.On("BotAuth", mock.AnythingOfType("*http.Request"))
|
|
dcm.On("GetClientID").Return(dcm.ClientID)
|
|
|
|
return dcm
|
|
}
|
|
|
|
func (c *DiscordClientMock) Do(req *http.Request) (*http.Response, error) {
|
|
args := c.Called(req)
|
|
|
|
return args.Get(0).(*http.Response), args.Error(1)
|
|
}
|
|
|
|
func (c *DiscordClientMock) BotAuth(req *http.Request) {
|
|
c.Called(req)
|
|
}
|
|
|
|
func (c *DiscordClientMock) MockResponse(method, path string, statusCode int, data any) {
|
|
body := bytes.Buffer{}
|
|
json.NewEncoder(&body).Encode(data)
|
|
|
|
r := &http.Response{
|
|
StatusCode: statusCode,
|
|
Body: io.NopCloser(&body),
|
|
}
|
|
|
|
pathMatcher := regexp.MustCompile(strings.ReplaceAll(path, "*", "[a-z0-9_-]+"))
|
|
|
|
c.On("Do", mock.MatchedBy(func(req *http.Request) bool {
|
|
return req.Method == method && pathMatcher.MatchString(req.URL.Path)
|
|
})).Return(r, nil)
|
|
}
|
|
|
|
func (c *DiscordClientMock) GetClientID() string {
|
|
return c.Called().String(0)
|
|
}
|