@@ -2,11 +2,14 @@ package main
22
33import (
44 "bytes"
5+ "context"
56 "errors"
67 "net"
78 "strings"
89 "testing"
910 "time"
11+
12+ "github.com/stretchr/testify/mock"
1013)
1114
1215// Mock connection for testing
@@ -90,23 +93,23 @@ func TestSocketmapAdapter_handleAlias(t *testing.T) {
9093 name : "existing alias" ,
9194 key : "alias@example.com" ,
9295 setup : func (m * MockUserliService ) {
93- m .On ("GetAliases" , "alias@example.com" ).Return ([]string {"user1@example.com" , "user2@example.com" }, nil )
96+ m .On ("GetAliases" , mock . Anything , "alias@example.com" ).Return ([]string {"user1@example.com" , "user2@example.com" }, nil )
9497 },
9598 expected : SocketmapResponse {Status : "OK" , Data : "user1@example.com,user2@example.com" },
9699 },
97100 {
98101 name : "non-existing alias" ,
99102 key : "nonexistent@example.com" ,
100103 setup : func (m * MockUserliService ) {
101- m .On ("GetAliases" , "nonexistent@example.com" ).Return ([]string {}, nil )
104+ m .On ("GetAliases" , mock . Anything , "nonexistent@example.com" ).Return ([]string {}, nil )
102105 },
103106 expected : SocketmapResponse {Status : "NOTFOUND" },
104107 },
105108 {
106109 name : "service error" ,
107110 key : "error@example.com" ,
108111 setup : func (m * MockUserliService ) {
109- m .On ("GetAliases" , "error@example.com" ).Return ([]string {}, errors .New ("service error" ))
112+ m .On ("GetAliases" , mock . Anything , "error@example.com" ).Return ([]string {}, errors .New ("service error" ))
110113 },
111114 expected : SocketmapResponse {Status : "TEMP" , Data : "Error fetching aliases" },
112115 },
@@ -118,7 +121,8 @@ func TestSocketmapAdapter_handleAlias(t *testing.T) {
118121 tt .setup (mock )
119122 adapter := NewSocketmapAdapter (mock )
120123
121- result := adapter .handleAlias (tt .key )
124+ ctx := context .Background ()
125+ result := adapter .handleAlias (ctx , tt .key )
122126
123127 if result .Status != tt .expected .Status {
124128 t .Errorf ("handleAlias() status = %q, want %q" , result .Status , tt .expected .Status )
@@ -142,23 +146,23 @@ func TestSocketmapAdapter_handleDomain(t *testing.T) {
142146 name : "existing domain" ,
143147 key : "example.com" ,
144148 setup : func (m * MockUserliService ) {
145- m .On ("GetDomain" , "example.com" ).Return (true , nil )
149+ m .On ("GetDomain" , mock . Anything , "example.com" ).Return (true , nil )
146150 },
147151 expected : SocketmapResponse {Status : "OK" , Data : "1" },
148152 },
149153 {
150154 name : "non-existing domain" ,
151155 key : "nonexistent.com" ,
152156 setup : func (m * MockUserliService ) {
153- m .On ("GetDomain" , "nonexistent.com" ).Return (false , nil )
157+ m .On ("GetDomain" , mock . Anything , "nonexistent.com" ).Return (false , nil )
154158 },
155159 expected : SocketmapResponse {Status : "NOTFOUND" },
156160 },
157161 {
158162 name : "service error" ,
159163 key : "error.com" ,
160164 setup : func (m * MockUserliService ) {
161- m .On ("GetDomain" , "error.com" ).Return (false , errors .New ("service error" ))
165+ m .On ("GetDomain" , mock . Anything , "error.com" ).Return (false , errors .New ("service error" ))
162166 },
163167 expected : SocketmapResponse {Status : "TEMP" , Data : "Error fetching domain" },
164168 },
@@ -170,7 +174,8 @@ func TestSocketmapAdapter_handleDomain(t *testing.T) {
170174 tt .setup (mock )
171175 adapter := NewSocketmapAdapter (mock )
172176
173- result := adapter .handleDomain (tt .key )
177+ ctx := context .Background ()
178+ result := adapter .handleDomain (ctx , tt .key )
174179
175180 if result .Status != tt .expected .Status {
176181 t .Errorf ("handleDomain() status = %q, want %q" , result .Status , tt .expected .Status )
@@ -194,23 +199,23 @@ func TestSocketmapAdapter_handleMailbox(t *testing.T) {
194199 name : "existing mailbox" ,
195200 key : "user@example.com" ,
196201 setup : func (m * MockUserliService ) {
197- m .On ("GetMailbox" , "user@example.com" ).Return (true , nil )
202+ m .On ("GetMailbox" , mock . Anything , "user@example.com" ).Return (true , nil )
198203 },
199204 expected : SocketmapResponse {Status : "OK" , Data : "1" },
200205 },
201206 {
202207 name : "non-existing mailbox" ,
203208 key : "nonexistent@example.com" ,
204209 setup : func (m * MockUserliService ) {
205- m .On ("GetMailbox" , "nonexistent@example.com" ).Return (false , nil )
210+ m .On ("GetMailbox" , mock . Anything , "nonexistent@example.com" ).Return (false , nil )
206211 },
207212 expected : SocketmapResponse {Status : "NOTFOUND" },
208213 },
209214 {
210215 name : "service error" ,
211216 key : "error@example.com" ,
212217 setup : func (m * MockUserliService ) {
213- m .On ("GetMailbox" , "error@example.com" ).Return (false , errors .New ("service error" ))
218+ m .On ("GetMailbox" , mock . Anything , "error@example.com" ).Return (false , errors .New ("service error" ))
214219 },
215220 expected : SocketmapResponse {Status : "TEMP" , Data : "Error fetching mailbox" },
216221 },
@@ -222,7 +227,8 @@ func TestSocketmapAdapter_handleMailbox(t *testing.T) {
222227 tt .setup (mock )
223228 adapter := NewSocketmapAdapter (mock )
224229
225- result := adapter .handleMailbox (tt .key )
230+ ctx := context .Background ()
231+ result := adapter .handleMailbox (ctx , tt .key )
226232
227233 if result .Status != tt .expected .Status {
228234 t .Errorf ("handleMailbox() status = %q, want %q" , result .Status , tt .expected .Status )
@@ -246,23 +252,23 @@ func TestSocketmapAdapter_handleSenders(t *testing.T) {
246252 name : "existing senders" ,
247253 key : "user@example.com" ,
248254 setup : func (m * MockUserliService ) {
249- m .On ("GetSenders" , "user@example.com" ).Return ([]string {"sender1@example.com" , "sender2@example.com" }, nil )
255+ m .On ("GetSenders" , mock . Anything , "user@example.com" ).Return ([]string {"sender1@example.com" , "sender2@example.com" }, nil )
250256 },
251257 expected : SocketmapResponse {Status : "OK" , Data : "sender1@example.com,sender2@example.com" },
252258 },
253259 {
254260 name : "no senders" ,
255261 key : "nosenders@example.com" ,
256262 setup : func (m * MockUserliService ) {
257- m .On ("GetSenders" , "nosenders@example.com" ).Return ([]string {}, nil )
263+ m .On ("GetSenders" , mock . Anything , "nosenders@example.com" ).Return ([]string {}, nil )
258264 },
259265 expected : SocketmapResponse {Status : "NOTFOUND" },
260266 },
261267 {
262268 name : "service error" ,
263269 key : "error@example.com" ,
264270 setup : func (m * MockUserliService ) {
265- m .On ("GetSenders" , "error@example.com" ).Return ([]string {}, errors .New ("service error" ))
271+ m .On ("GetSenders" , mock . Anything , "error@example.com" ).Return ([]string {}, errors .New ("service error" ))
266272 },
267273 expected : SocketmapResponse {Status : "TEMP" , Data : "Error fetching senders" },
268274 },
@@ -274,7 +280,8 @@ func TestSocketmapAdapter_handleSenders(t *testing.T) {
274280 tt .setup (mock )
275281 adapter := NewSocketmapAdapter (mock )
276282
277- result := adapter .handleSenders (tt .key )
283+ ctx := context .Background ()
284+ result := adapter .handleSenders (ctx , tt .key )
278285
279286 if result .Status != tt .expected .Status {
280287 t .Errorf ("handleSenders() status = %q, want %q" , result .Status , tt .expected .Status )
@@ -299,7 +306,7 @@ func TestSocketmapAdapter_HandleConnection(t *testing.T) {
299306 name : "single alias request" ,
300307 requests : []string {"22:alias test@example.com," },
301308 setup : func (m * MockUserliService ) {
302- m .On ("GetAliases" , "test@example.com" ).Return ([]string {"dest@example.com" }, nil )
309+ m .On ("GetAliases" , mock . Anything , "test@example.com" ).Return ([]string {"dest@example.com" }, nil )
303310 },
304311 expectedCount : 1 ,
305312 expectedOutput : []string {"19:OK dest@example.com," },
@@ -308,7 +315,7 @@ func TestSocketmapAdapter_HandleConnection(t *testing.T) {
308315 name : "single domain request" ,
309316 requests : []string {"18:domain example.com," },
310317 setup : func (m * MockUserliService ) {
311- m .On ("GetDomain" , "example.com" ).Return (true , nil )
318+ m .On ("GetDomain" , mock . Anything , "example.com" ).Return (true , nil )
312319 },
313320 expectedCount : 1 ,
314321 expectedOutput : []string {"4:OK 1," },
@@ -334,8 +341,8 @@ func TestSocketmapAdapter_HandleConnection(t *testing.T) {
334341 "18:domain example.com," ,
335342 },
336343 setup : func (m * MockUserliService ) {
337- m .On ("GetAliases" , "test@example.com" ).Return ([]string {"dest@example.com" }, nil )
338- m .On ("GetDomain" , "example.com" ).Return (true , nil )
344+ m .On ("GetAliases" , mock . Anything , "test@example.com" ).Return ([]string {"dest@example.com" }, nil )
345+ m .On ("GetDomain" , mock . Anything , "example.com" ).Return (true , nil )
339346 },
340347 expectedCount : 2 ,
341348 expectedOutput : []string {
0 commit comments