@@ -25,6 +25,8 @@ func TestNew(t *testing.T) {
2525 config : Config {
2626 HTTPListenAddr : "http://localhost" ,
2727 HTTPListenPort : 8080 ,
28+ UnAuthorizedHTTPListenAddr : "localhost" ,
29+ UnAuthorizedHTTPListenPort : 1111 ,
2830 ServerGracefulShutdownTimeout : time .Second * 5 ,
2931 HTTPServerReadTimeout : time .Second * 10 ,
3032 HTTPServerWriteTimeout : time .Second * 10 ,
@@ -35,8 +37,10 @@ func TestNew(t *testing.T) {
3537 {
3638 name : "invalid address for unauth" ,
3739 config : Config {
38- UnAuthorizedHTTPListenAddr : "http:// localhost" ,
40+ HTTPListenAddr : " localhost" ,
3941 HTTPListenPort : 8080 ,
42+ UnAuthorizedHTTPListenAddr : "http://localhost" ,
43+ UnAuthorizedHTTPListenPort : 8081 ,
4044 ServerGracefulShutdownTimeout : time .Second * 5 ,
4145 HTTPServerReadTimeout : time .Second * 10 ,
4246 HTTPServerWriteTimeout : time .Second * 10 ,
@@ -49,6 +53,8 @@ func TestNew(t *testing.T) {
4953 config : Config {
5054 HTTPListenAddr : "localhost" ,
5155 HTTPListenPort : 8080 ,
56+ UnAuthorizedHTTPListenAddr : "localhost" ,
57+ UnAuthorizedHTTPListenPort : 8081 ,
5258 ServerGracefulShutdownTimeout : time .Second * 5 ,
5359 HTTPServerReadTimeout : time .Second * 10 ,
5460 HTTPServerWriteTimeout : time .Second * 10 ,
@@ -61,6 +67,8 @@ func TestNew(t *testing.T) {
6167 config : Config {
6268 HTTPListenAddr : "localhost" ,
6369 HTTPListenPort : 8080 ,
70+ UnAuthorizedHTTPListenAddr : "localhost" ,
71+ UnAuthorizedHTTPListenPort : 8081 ,
6472 ServerGracefulShutdownTimeout : time .Second * 5 ,
6573 HTTPServerReadTimeout : time .Second * 10 ,
6674 HTTPServerWriteTimeout : time .Second * 10 ,
@@ -94,6 +102,7 @@ func TestNew(t *testing.T) {
94102 t .Errorf ("Expected server address to be %s:%d, but got %s" , tc .config .HTTPListenAddr , tc .config .HTTPListenPort , server .authServer .httpServer .Addr )
95103 }
96104 }
105+ server .Shutdown ()
97106 })
98107 }
99108}
@@ -115,7 +124,6 @@ func TestServer_RegisterTo(t *testing.T) {
115124 s .RegisterTo ("/test_auth" , testHandler , AUTH )
116125 s .RegisterTo ("/test_unauth" , testHandler , UNAUTH )
117126
118- // Test authorized server.
119127 req := httptest .NewRequest (http .MethodGet , "/test_auth" , nil )
120128 w := httptest .NewRecorder ()
121129
@@ -126,7 +134,6 @@ func TestServer_RegisterTo(t *testing.T) {
126134 t .Errorf ("Expected status code %d for AUTH server, but got %d" , http .StatusOK , resp .StatusCode )
127135 }
128136
129- // Test unauthorized server.
130137 req = httptest .NewRequest (http .MethodGet , "/test_unauth" , nil )
131138 w = httptest .NewRecorder ()
132139
@@ -227,6 +234,10 @@ func TestRun(t *testing.T) {
227234
228235func TestReadyHandler (t * testing.T ) {
229236 cfg := Config {
237+ HTTPListenAddr : "localhost" ,
238+ HTTPListenPort : 1234 ,
239+ UnAuthorizedHTTPListenAddr : "localhost" ,
240+ UnAuthorizedHTTPListenPort : 1235 ,
230241 HTTPServerReadTimeout : 5 * time .Second ,
231242 HTTPServerWriteTimeout : 5 * time .Second ,
232243 HTTPServerIdleTimeout : 5 * time .Second ,
@@ -278,4 +289,42 @@ func TestReadyHandler(t *testing.T) {
278289 }
279290 })
280291 }
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+ }
281330}
0 commit comments