redo discord mocks
This commit is contained in:
parent
f4718f979a
commit
dfbb9e2bca
19 changed files with 223 additions and 105 deletions
42
discord/clientmock/discord_client_mock.go
Normal file
42
discord/clientmock/discord_client_mock.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue