| 1 |
package twilio_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io" |
| 6 |
"net/http" |
| 7 |
"net/url" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/mock" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio" |
| 14 |
) |
| 15 |
|
| 16 |
// MockHTTPClient is a testify mock that implements the HTTPClient interface. |
| 17 |
type MockHTTPClient struct { |
| 18 |
mock.Mock |
| 19 |
} |
| 20 |
|
| 21 |
// mockLogger is a simple logger implementation for testing. |
| 22 |
type mockLogger struct{} |
| 23 |
|
| 24 |
func (m *mockLogger) Print(_ ...any) {} |
| 25 |
func (m *mockLogger) Printf(_ string, _ ...any) {} |
| 26 |
func (m *mockLogger) Println(_ ...any) {} |
| 27 |
|
| 28 |
// createTestService creates a Twilio service instance configured for testing. |
| 29 |
func createTestService( |
| 30 |
t *testing.T, |
| 31 |
twilioURL string, |
| 32 |
httpClients ...twilio.HTTPClient, |
| 33 |
) *twilio.Service { |
| 34 |
t.Helper() |
| 35 |
|
| 36 |
service := &twilio.Service{} |
| 37 |
|
| 38 |
parsedURL, err := url.Parse(twilioURL) |
| 39 |
require.NoError(t, err) |
| 40 |
|
| 41 |
err = service.Initialize(parsedURL, &mockLogger{}) |
| 42 |
require.NoError(t, err) |
| 43 |
|
| 44 |
// Override the HTTPClient if provided (after Initialize sets the default) |
| 45 |
if len(httpClients) > 0 && httpClients[0] != nil { |
| 46 |
service.HTTPClient = httpClients[0] |
| 47 |
} |
| 48 |
|
| 49 |
return service |
| 50 |
} |
| 51 |
|
| 52 |
func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) { |
| 53 |
// Capture the request body before it's consumed |
| 54 |
if req.Body != nil { |
| 55 |
body, err := io.ReadAll(req.Body) |
| 56 |
if err == nil { |
| 57 |
req.Body = io.NopCloser(bytes.NewReader(body)) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
args := m.Called(req) |
| 62 |
|
| 63 |
if args.Get(0) == nil { |
| 64 |
return nil, args.Error(1) |
| 65 |
} |
| 66 |
|
| 67 |
return args.Get(0).(*http.Response), args.Error(1) |
| 68 |
} |
| 69 |
|
| 70 |
// createMockResponse creates a mock HTTP response with the given status code and body. |
| 71 |
func createMockResponse(statusCode int, body string) *http.Response { |
| 72 |
return &http.Response{ |
| 73 |
StatusCode: statusCode, |
| 74 |
Body: io.NopCloser(bytes.NewReader([]byte(body))), |
| 75 |
Header: make(http.Header), |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// assertRequestContains asserts that the HTTP request body contains the expected content. |
| 80 |
func assertRequestContains(t *testing.T, mockClient *MockHTTPClient, expectedContent string) { |
| 81 |
t.Helper() |
| 82 |
|
| 83 |
found := false |
| 84 |
|
| 85 |
for i := range mockClient.Calls { |
| 86 |
call := &mockClient.Calls[i] |
| 87 |
if call.Method != "Do" { |
| 88 |
continue |
| 89 |
} |
| 90 |
|
| 91 |
req := call.Arguments[0].(*http.Request) |
| 92 |
|
| 93 |
body, err := io.ReadAll(req.Body) |
| 94 |
if err != nil { |
| 95 |
continue |
| 96 |
} |
| 97 |
// Reset the body for potential future reads |
| 98 |
req.Body = io.NopCloser(bytes.NewReader(body)) |
| 99 |
|
| 100 |
if bytes.Contains(body, []byte(expectedContent)) { |
| 101 |
found = true |
| 102 |
|
| 103 |
break |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if !found { |
| 108 |
t.Errorf("Expected request body to contain %q, but no matching call found", expectedContent) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// assertRequestMatches asserts that at least one HTTP request matches the given predicate. |
| 113 |
func assertRequestMatches( |
| 114 |
t *testing.T, |
| 115 |
mockClient *MockHTTPClient, |
| 116 |
predicate func(*http.Request) bool, |
| 117 |
description string, |
| 118 |
) { |
| 119 |
t.Helper() |
| 120 |
|
| 121 |
found := false |
| 122 |
|
| 123 |
for i := range mockClient.Calls { |
| 124 |
call := &mockClient.Calls[i] |
| 125 |
if call.Method != "Do" { |
| 126 |
continue |
| 127 |
} |
| 128 |
|
| 129 |
req := call.Arguments[0].(*http.Request) |
| 130 |
if predicate(req) { |
| 131 |
found = true |
| 132 |
|
| 133 |
break |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if !found { |
| 138 |
t.Errorf("Expected request to match %s, but no matching call found", description) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// assertRequestMade asserts that an HTTP POST request was made to the expected URL. |
| 143 |
func assertRequestMade( |
| 144 |
t *testing.T, |
| 145 |
mockClient *MockHTTPClient, |
| 146 |
expectedURL string, |
| 147 |
) { |
| 148 |
t.Helper() |
| 149 |
|
| 150 |
found := false |
| 151 |
|
| 152 |
for i := range mockClient.Calls { |
| 153 |
call := &mockClient.Calls[i] |
| 154 |
if call.Method != "Do" { |
| 155 |
continue |
| 156 |
} |
| 157 |
|
| 158 |
req := call.Arguments[0].(*http.Request) |
| 159 |
if req.Method == http.MethodPost && req.URL.String() == expectedURL { |
| 160 |
found = true |
| 161 |
|
| 162 |
break |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if !found { |
| 167 |
t.Errorf("Expected POST request to %s, but no matching call found", expectedURL) |
| 168 |
} |
| 169 |
} |