| 1 | package twilio_testscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src . bytes/" |
| 5 | "ioscip-go gomod github.com/golang/go/src . io/" |
| 6 | "net/httpscip-go gomod github.com/golang/go/src . `net/http`/" |
| 7 | "net/urlscip-go gomod github.com/golang/go/src . `net/url`/" |
| 8 | "testingscip-go gomod github.com/golang/go/src . testing/" |
| 9 | |
| 10 | "github.com/stretchr/testify/mockscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/" |
| 11 | "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/" |
| 12 | |
| 13 | "github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilioscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/" |
| 14 | ) |
| 15 | |
| 16 | // MockHTTPClient is a testify mock that implements the HTTPClient interface. |
| 17 | type MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/MockHTTPClient# struct { |
| 18 | mockscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/.Mockscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/MockHTTPClient#Mock. |
| 19 | } |
| 20 | |
| 21 | // mockLogger is a simple logger implementation for testing. |
| 22 | type mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/mockLogger# struct{} |
| 23 | |
| 24 | func (m *mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/mockLogger#) Printscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/mockLogger#Print().(_ ...any) {} |
| 25 | func (m *mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/mockLogger#) Printfscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/mockLogger#Printf().(_ string, _ ...any) {} |
| 26 | func (m *mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/mockLogger#) Printlnscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/mockLogger#Println().(_ ...any) {} |
| 27 | |
| 28 | // createTestService creates a Twilio service instance configured for testing. |
| 29 | func createTestServicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/createTestService().( |
| 30 | t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, |
| 31 | twilioURL string, |
| 32 | httpClients ...twilioscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/HTTPClient#, |
| 33 | ) *twilioscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/.Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/Service# { |
| 34 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 35 | |
| 36 | service := &twilioscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/.Servicescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/Service#{} |
| 37 | |
| 38 | parsedURL, err := urlscip-go gomod github.com/golang/go/src . `net/url`/.Parsescip-go gomod github.com/golang/go/src . `net/url`/Parse().(twilioURL) |
| 39 | requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NoError().(t, err) |
| 40 | |
| 41 | err = service.Initializescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/Service#Initialize().(parsedURL, &mockLoggerscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/mockLogger#{}) |
| 42 | requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/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.HTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/sms/twilio`/Service#HTTPClient. = httpClients[0] |
| 47 | } |
| 48 | |
| 49 | return service |
| 50 | } |
| 51 | |
| 52 | func (m *MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/MockHTTPClient#) Doscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/MockHTTPClient#Do().(req *httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) (*httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#, error) { |
| 53 | // Capture the request body before it's consumed |
| 54 | if req.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Request#Body. != nil { |
| 55 | body, err := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(req.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Request#Body.) |
| 56 | if err == nil { |
| 57 | req.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Request#Body. = ioscip-go gomod github.com/golang/go/src . io/.NopCloserscip-go gomod github.com/golang/go/src . io/NopCloser().(bytesscip-go gomod github.com/golang/go/src . bytes/.NewReaderscip-go gomod github.com/golang/go/src . bytes/NewReader().(body)) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | args := m.Calledscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Called().(req) |
| 62 | |
| 63 | if args.Getscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Arguments#Get().(0) == nil { |
| 64 | return nil, args.Errorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Arguments#Error().(1) |
| 65 | } |
| 66 | |
| 67 | return args.Getscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Arguments#Get().(0).(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#), args.Errorscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Arguments#Error().(1) |
| 68 | } |
| 69 | |
| 70 | // createMockResponse creates a mock HTTP response with the given status code and body. |
| 71 | func createMockResponsescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/createMockResponse().(statusCode int, body string) *httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response# { |
| 72 | return &httpscip-go gomod github.com/golang/go/src . `net/http`/.Responsescip-go gomod github.com/golang/go/src . `net/http`/Response#{ |
| 73 | StatusCodescip-go gomod github.com/golang/go/src . `net/http`/Response#StatusCode.: statusCode, |
| 74 | Bodyscip-go gomod github.com/golang/go/src . `net/http`/Response#Body.: ioscip-go gomod github.com/golang/go/src . io/.NopCloserscip-go gomod github.com/golang/go/src . io/NopCloser().(bytesscip-go gomod github.com/golang/go/src . bytes/.NewReaderscip-go gomod github.com/golang/go/src . bytes/NewReader().([]byte(body))), |
| 75 | Headerscip-go gomod github.com/golang/go/src . `net/http`/Response#Header.: make(httpscip-go gomod github.com/golang/go/src . `net/http`/.Headerscip-go gomod github.com/golang/go/src . `net/http`/Header#), |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // assertRequestContains asserts that the HTTP request body contains the expected content. |
| 80 | func assertRequestContainsscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/assertRequestContains().(t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, mockClient *MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/MockHTTPClient#, expectedContent string) { |
| 81 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 82 | |
| 83 | found := false |
| 84 | |
| 85 | for i := range mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls. { |
| 86 | call := &mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls.[i] |
| 87 | if call.Methodscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Method. != "Do" { |
| 88 | continue |
| 89 | } |
| 90 | |
| 91 | req := call.Argumentsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Arguments.[0].(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) |
| 92 | |
| 93 | body, err := ioscip-go gomod github.com/golang/go/src . io/.ReadAllscip-go gomod github.com/golang/go/src . io/ReadAll().(req.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Request#Body.) |
| 94 | if err != nil { |
| 95 | continue |
| 96 | } |
| 97 | // Reset the body for potential future reads |
| 98 | req.Bodyscip-go gomod github.com/golang/go/src . `net/http`/Request#Body. = ioscip-go gomod github.com/golang/go/src . io/.NopCloserscip-go gomod github.com/golang/go/src . io/NopCloser().(bytesscip-go gomod github.com/golang/go/src . bytes/.NewReaderscip-go gomod github.com/golang/go/src . bytes/NewReader().(body)) |
| 99 | |
| 100 | if bytesscip-go gomod github.com/golang/go/src . bytes/.Containsscip-go gomod github.com/golang/go/src . bytes/Contains().(body, []byte(expectedContent)) { |
| 101 | found = true |
| 102 | |
| 103 | break |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if !found { |
| 108 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#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 assertRequestMatchesscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/assertRequestMatches().( |
| 114 | t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, |
| 115 | mockClient *MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/MockHTTPClient#, |
| 116 | predicate func(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) bool, |
| 117 | description string, |
| 118 | ) { |
| 119 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 120 | |
| 121 | found := false |
| 122 | |
| 123 | for i := range mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls. { |
| 124 | call := &mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls.[i] |
| 125 | if call.Methodscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Method. != "Do" { |
| 126 | continue |
| 127 | } |
| 128 | |
| 129 | req := call.Argumentsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Arguments.[0].(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) |
| 130 | if predicate(req) { |
| 131 | found = true |
| 132 | |
| 133 | break |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if !found { |
| 138 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#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 assertRequestMadescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/assertRequestMade().( |
| 144 | t *testingscip-go gomod github.com/golang/go/src . testing/.Tscip-go gomod github.com/golang/go/src . testing/T#, |
| 145 | mockClient *MockHTTPClientscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/testing/integration/twilio_test`/MockHTTPClient#, |
| 146 | expectedURL string, |
| 147 | ) { |
| 148 | t.Helperscip-go gomod github.com/golang/go/src . testing/common#Helper().() |
| 149 | |
| 150 | found := false |
| 151 | |
| 152 | for i := range mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls. { |
| 153 | call := &mockClient.Callsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Mock#Calls.[i] |
| 154 | if call.Methodscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Method. != "Do" { |
| 155 | continue |
| 156 | } |
| 157 | |
| 158 | req := call.Argumentsscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/mock`/Call#Arguments.[0].(*httpscip-go gomod github.com/golang/go/src . `net/http`/.Requestscip-go gomod github.com/golang/go/src . `net/http`/Request#) |
| 159 | if req.Methodscip-go gomod github.com/golang/go/src . `net/http`/Request#Method. == httpscip-go gomod github.com/golang/go/src . `net/http`/.MethodPostscip-go gomod github.com/golang/go/src . `net/http`/MethodPost. && req.URLscip-go gomod github.com/golang/go/src . `net/http`/Request#URL..Stringscip-go gomod github.com/golang/go/src . `net/url`/URL#String().() == expectedURL { |
| 160 | found = true |
| 161 | |
| 162 | break |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if !found { |
| 167 | t.Errorfscip-go gomod github.com/golang/go/src . testing/common#Errorf().("Expected POST request to %s, but no matching call found", expectedURL) |
| 168 | } |
| 169 | } |