@@ -25,6 +25,8 @@ func TestNew(t *testing.T) {
25
25
config : Config {
26
26
HTTPListenAddr : "http://localhost" ,
27
27
HTTPListenPort : 8080 ,
28
+ UnAuthorizedHTTPListenAddr : "localhost" ,
29
+ UnAuthorizedHTTPListenPort : 1111 ,
28
30
ServerGracefulShutdownTimeout : time .Second * 5 ,
29
31
HTTPServerReadTimeout : time .Second * 10 ,
30
32
HTTPServerWriteTimeout : time .Second * 10 ,
@@ -35,8 +37,10 @@ func TestNew(t *testing.T) {
35
37
{
36
38
name : "invalid address for unauth" ,
37
39
config : Config {
38
- UnAuthorizedHTTPListenAddr : "http:// localhost" ,
40
+ HTTPListenAddr : " localhost" ,
39
41
HTTPListenPort : 8080 ,
42
+ UnAuthorizedHTTPListenAddr : "http://localhost" ,
43
+ UnAuthorizedHTTPListenPort : 8081 ,
40
44
ServerGracefulShutdownTimeout : time .Second * 5 ,
41
45
HTTPServerReadTimeout : time .Second * 10 ,
42
46
HTTPServerWriteTimeout : time .Second * 10 ,
@@ -49,6 +53,8 @@ func TestNew(t *testing.T) {
49
53
config : Config {
50
54
HTTPListenAddr : "localhost" ,
51
55
HTTPListenPort : 8080 ,
56
+ UnAuthorizedHTTPListenAddr : "localhost" ,
57
+ UnAuthorizedHTTPListenPort : 8081 ,
52
58
ServerGracefulShutdownTimeout : time .Second * 5 ,
53
59
HTTPServerReadTimeout : time .Second * 10 ,
54
60
HTTPServerWriteTimeout : time .Second * 10 ,
@@ -61,6 +67,8 @@ func TestNew(t *testing.T) {
61
67
config : Config {
62
68
HTTPListenAddr : "localhost" ,
63
69
HTTPListenPort : 8080 ,
70
+ UnAuthorizedHTTPListenAddr : "localhost" ,
71
+ UnAuthorizedHTTPListenPort : 8081 ,
64
72
ServerGracefulShutdownTimeout : time .Second * 5 ,
65
73
HTTPServerReadTimeout : time .Second * 10 ,
66
74
HTTPServerWriteTimeout : time .Second * 10 ,
@@ -94,6 +102,7 @@ func TestNew(t *testing.T) {
94
102
t .Errorf ("Expected server address to be %s:%d, but got %s" , tc .config .HTTPListenAddr , tc .config .HTTPListenPort , server .authServer .httpServer .Addr )
95
103
}
96
104
}
105
+ server .Shutdown ()
97
106
})
98
107
}
99
108
}
@@ -115,7 +124,6 @@ func TestServer_RegisterTo(t *testing.T) {
115
124
s .RegisterTo ("/test_auth" , testHandler , AUTH )
116
125
s .RegisterTo ("/test_unauth" , testHandler , UNAUTH )
117
126
118
- // Test authorized server.
119
127
req := httptest .NewRequest (http .MethodGet , "/test_auth" , nil )
120
128
w := httptest .NewRecorder ()
121
129
@@ -126,7 +134,6 @@ func TestServer_RegisterTo(t *testing.T) {
126
134
t .Errorf ("Expected status code %d for AUTH server, but got %d" , http .StatusOK , resp .StatusCode )
127
135
}
128
136
129
- // Test unauthorized server.
130
137
req = httptest .NewRequest (http .MethodGet , "/test_unauth" , nil )
131
138
w = httptest .NewRecorder ()
132
139
@@ -227,6 +234,10 @@ func TestRun(t *testing.T) {
227
234
228
235
func TestReadyHandler (t * testing.T ) {
229
236
cfg := Config {
237
+ HTTPListenAddr : "localhost" ,
238
+ HTTPListenPort : 1234 ,
239
+ UnAuthorizedHTTPListenAddr : "localhost" ,
240
+ UnAuthorizedHTTPListenPort : 1235 ,
230
241
HTTPServerReadTimeout : 5 * time .Second ,
231
242
HTTPServerWriteTimeout : 5 * time .Second ,
232
243
HTTPServerIdleTimeout : 5 * time .Second ,
@@ -278,4 +289,42 @@ func TestReadyHandler(t *testing.T) {
278
289
}
279
290
})
280
291
}
292
+ s .Shutdown ()
293
+ }
294
+
295
+ func TestCheckPortAvailable (t * testing.T ) {
296
+ tests := []struct {
297
+ name string
298
+ listenAddr string
299
+ listenPort int
300
+ wantAvailable bool
301
+ }{
302
+ {
303
+ name : "port available" ,
304
+ listenAddr : "localhost" ,
305
+ listenPort : 8080 ,
306
+ wantAvailable : true ,
307
+ },
308
+ {
309
+ name : "port unavailable" ,
310
+ listenAddr : "localhost" ,
311
+ listenPort : 1234 ,
312
+ wantAvailable : false ,
313
+ },
314
+ }
315
+
316
+ listener , err := net .Listen (DefaultNetwork , fmt .Sprintf ("%s:%d" , "localhost" , 1234 ))
317
+ if err != nil {
318
+ t .Fatalf ("Failed to create a listener: %v" , err )
319
+ }
320
+ defer listener .Close ()
321
+ for _ , tt := range tests {
322
+ t .Run (tt .name , func (t * testing.T ) {
323
+
324
+ available := checkPortAvailable (tt .listenAddr , tt .listenPort , DefaultNetwork )
325
+ if available != tt .wantAvailable {
326
+ t .Errorf ("Expected port %d to be available: %v" , tt .listenPort , tt .wantAvailable )
327
+ }
328
+ })
329
+ }
281
330
}
0 commit comments