Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: modified the Route.Run() to use APP_PORT #58

Merged
merged 8 commits into from
Feb 28, 2023
14 changes: 12 additions & 2 deletions route/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great Bro, can you also change the RunTLS method like this? Then we also need to change the config/route.go file in goravel/goravel.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I have pushed the changes, I am just try to fix failing test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hwbrzzl can you help me with testcases, I am not able to resolve them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hwbrzzl There is some issue with testcases, I am trying to debug it, will push it by Wednesday.

}

outputRoutes(r.instance.Routes())
Expand All @@ -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")
Expand Down
72 changes: 58 additions & 14 deletions route/gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -48,23 +49,40 @@ 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())
}()

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() {
Expand All @@ -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()

Expand All @@ -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() {
Expand All @@ -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()

Expand All @@ -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()
Expand All @@ -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()

Expand Down