42 lines
882 B
Go
42 lines
882 B
Go
package clientmock
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
type DiscordClientMock struct {
|
|
mock.Mock
|
|
}
|
|
|
|
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)
|
|
}
|