| 1 | package testutils |
| 2 | |
| 3 | import ( |
| 4 | "log" |
| 5 | "net/url" |
| 6 | |
| 7 | "github.com/jarcoal/httpmock" |
| 8 | "github.com/onsi/ginkgo/v2" |
| 9 | "github.com/onsi/gomega" |
| 10 | |
| 11 | "github.com/nicholas-fedor/shoutrrr/pkg/types" |
| 12 | ) |
| 13 | |
| 14 | // TestLogger returns a log.Logger that writes to ginkgo.GinkgoWriter for use in tests. |
| 15 | func TestLogger() *log.Logger { |
| 16 | return log.New(ginkgo.GinkgoWriter, "[Test] ", 0) |
| 17 | } |
| 18 | |
| 19 | // URLMust creates a url.URL from the given serviceURL and fails the test if it cannot be parsed. |
| 20 | func URLMust(serviceURL string) *url.URL { |
| 21 | parsed, err := url.Parse(serviceURL) |
| 22 | gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred()) |
| 23 | |
| 24 | return parsed |
| 25 | } |
| 26 | |
| 27 | // JSONRespondMust creates a httpmock.Responder with the given response |
| 28 | // as the body, and fails the test if it cannot be created. |
| 29 | func JSONRespondMust(code int, response any) httpmock.Responder { |
| 30 | responder, err := httpmock.NewJsonResponder(code, response) |
| 31 | gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred(), "invalid test response struct") |
| 32 | |
| 33 | return responder |
| 34 | } |
| 35 | |
| 36 | // TestServiceSetInvalidParamValue tests whether the service returns an error |
| 37 | // when an invalid param key/value is passed through Send. |
| 38 | func TestServiceSetInvalidParamValue(service types.Service, key, value string) { |
| 39 | err := service.Send("TestMessage", &types.Params{key: value}) |
| 40 | gomega.ExpectWithOffset(1, err).To(gomega.HaveOccurred()) |
| 41 | } |