diff --git a/route/gin.go b/route/gin.go index 4cd2bb3ec..00dfa5ae1 100644 --- a/route/gin.go +++ b/route/gin.go @@ -40,7 +40,12 @@ func (r *Gin) Run(host ...string) error { return errors.New("host can't be empty") } - host = append(host, defaultHost) + defaultPort := facades.Config.GetString("route.port") + if defaultPort == "" { + return errors.New("port can't be empty") + } + completeHost := defaultHost + ":" + defaultPort + host = append(host, completeHost) } outputRoutes(r.instance.Routes()) @@ -56,7 +61,12 @@ func (r *Gin) RunTLS(host ...string) error { return errors.New("host can't be empty") } - host = append(host, defaultHost) + defaultPort := facades.Config.GetString("route.tls.port") + if defaultPort == "" { + return errors.New("port can't be empty") + } + completeHost := defaultHost + ":" + defaultPort + host = append(host, completeHost) } certFile := facades.Config.GetString("route.tls.ssl.cert") diff --git a/route/gin_test.go b/route/gin_test.go index 9e7fe212c..0360272af 100644 --- a/route/gin_test.go +++ b/route/gin_test.go @@ -31,13 +31,14 @@ func TestRun(t *testing.T) { tests := []struct { name string - setup func(host string) error + setup func(host string, port string) error host string + port string expectError error }{ { name: "error when default host is empty", - setup: func(host string) error { + setup: func(host string, port string) error { mockConfig.On("GetString", "route.host").Return(host).Once() go func() { @@ -48,11 +49,27 @@ func TestRun(t *testing.T) { return errors.New("error") }, }, + { + name: "error when default port is empty", + setup: func(host string, port string) error { + mockConfig.On("GetString", "route.host").Return(host).Once() + mockConfig.On("GetString", "route.port").Return(port).Once() + + go func() { + assert.EqualError(t, route.Run(), "port can't be empty") + }() + time.Sleep(1 * time.Second) + + return errors.New("error") + }, + host: "127.0.0.1", + }, { name: "use default host", - setup: func(host string) error { + setup: func(host string, port string) error { mockConfig.On("GetBool", "app.debug").Return(true).Once() mockConfig.On("GetString", "route.host").Return(host).Once() + mockConfig.On("GetString", "route.port").Return(port).Once() go func() { assert.Nil(t, route.Run()) @@ -60,11 +77,12 @@ func TestRun(t *testing.T) { return nil }, - host: "127.0.0.1:3001", + host: "127.0.0.1", + port: "3001", }, { name: "use custom host", - setup: func(host string) error { + setup: func(host string, port string) error { mockConfig.On("GetBool", "app.debug").Return(true).Once() go func() { @@ -87,9 +105,13 @@ func TestRun(t *testing.T) { "Hello": "Goravel", }) }) - if err := test.setup(test.host); err == nil { + if err := test.setup(test.host, test.port); err == nil { time.Sleep(1 * time.Second) - resp, err := http.Get("http://" + test.host) + hostUrl := "http://" + test.host + if test.port != "" { + hostUrl = hostUrl + ":" + test.port + } + resp, err := http.Get(hostUrl) assert.Nil(t, err) defer resp.Body.Close() @@ -108,13 +130,14 @@ func TestRunTLS(t *testing.T) { tests := []struct { name string - setup func(host string) error + setup func(host string, port string) error host string + port string expectError error }{ { name: "error when default host is empty", - setup: func(host string) error { + setup: func(host string, port string) error { mockConfig.On("GetString", "route.tls.host").Return(host).Once() go func() { @@ -125,11 +148,27 @@ func TestRunTLS(t *testing.T) { return errors.New("error") }, }, + { + name: "error when default port is empty", + setup: func(host string, port string) error { + mockConfig.On("GetString", "route.tls.host").Return(host).Once() + mockConfig.On("GetString", "route.tls.port").Return(port).Once() + + go func() { + assert.EqualError(t, route.RunTLS(), "port can't be empty") + }() + time.Sleep(1 * time.Second) + + return errors.New("error") + }, + host: "127.0.0.1", + }, { name: "use default host", - setup: func(host string) error { + setup: func(host string, port string) error { mockConfig.On("GetBool", "app.debug").Return(true).Once() mockConfig.On("GetString", "route.tls.host").Return(host).Once() + mockConfig.On("GetString", "route.tls.port").Return(port).Once() mockConfig.On("GetString", "route.tls.ssl.cert").Return("test_ca.crt").Once() mockConfig.On("GetString", "route.tls.ssl.key").Return("test_ca.key").Once() @@ -139,11 +178,12 @@ func TestRunTLS(t *testing.T) { return nil }, - host: "127.0.0.1:3003", + host: "127.0.0.1", + port: "3003", }, { name: "use custom host", - setup: func(host string) error { + setup: func(host string, port string) error { mockConfig.On("GetBool", "app.debug").Return(true).Once() mockConfig.On("GetString", "route.tls.ssl.cert").Return("test_ca.crt").Once() mockConfig.On("GetString", "route.tls.ssl.key").Return("test_ca.key").Once() @@ -168,13 +208,17 @@ func TestRunTLS(t *testing.T) { "Hello": "Goravel", }) }) - if err := test.setup(test.host); err == nil { + if err := test.setup(test.host, test.port); err == nil { time.Sleep(1 * time.Second) tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: tr} - resp, err := client.Get("https://" + test.host) + hostUrl := "https://" + test.host + if test.port != "" { + hostUrl = hostUrl + ":" + test.port + } + resp, err := client.Get(hostUrl) assert.Nil(t, err) defer resp.Body.Close()