Skip to content

Commit

Permalink
fix: invalid service URL causes unhandled exception (#145)
Browse files Browse the repository at this point in the history
* fix: invalid service URL causes unhandled exception

* fix lint complains

* fix: addapt commands to validate URL

* feat: enforce name without URL pattern

* tests: adapt integration tests

* show correct url for services

* fixes linter complaint

* test: adds better description
  • Loading branch information
cristianoliveira authored Aug 7, 2023
1 parent 8979e9b commit e484c4c
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .ergo
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ bla http://localhost:5000
withspaces http://localhost:8080
one.domain http://localhost:8081
two.domain http://localhost:8082
redis://redislocal redis://localhost:6543
redislocal redis://localhost:6543
*.wildcard http://localhost:3030
16 changes: 5 additions & 11 deletions commands/add_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestAddServiceAllreadyThere(t *testing.T) {
config := buildConfig([]proxy.Service{
proxy.Service{Name: "test", URL: "localhost:9999"},
proxy.UnsafeNewService("test", "http://localhost:9999"),
})

service := proxy.Service{Name: "test"}
Expand All @@ -22,13 +22,10 @@ func TestAddServiceAllreadyThere(t *testing.T) {

func TestAddServiceAddOK(t *testing.T) {
config := buildConfig([]proxy.Service{
proxy.Service{Name: "test.dev", URL: "localhost:9999"},
proxy.UnsafeNewService("test.dev", "http://localhost:9999"),
})

service := proxy.Service{
Name: "newtest.dev",
URL: "http://localhost:3333",
}
service := proxy.UnsafeNewService("newtest.dev", "http://localhost:3333")

command := AddServiceCommand{Service: service}
result, err := command.Execute(config)
Expand All @@ -43,13 +40,10 @@ func TestAddServiceAddOK(t *testing.T) {

func TestAddServiceAddFileNotFound(t *testing.T) {
config := buildConfig([]proxy.Service{
proxy.Service{Name: "test.dev", URL: "localhost:9999"},
proxy.UnsafeNewService("test.dev", "localhost:9999"),
})

service := proxy.Service{
Name: "newtest.dev",
URL: "http://localhost:3333",
}
service := proxy.UnsafeNewService("newtest.dev", "http://localhost:3333")

config.ConfigFile = "anyfilethatdoesnotexist.here"

Expand Down
2 changes: 1 addition & 1 deletion commands/list_names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestListNames(t *testing.T) {

config := buildConfig([]proxy.Service{
{Name: "test.dev", URL: "localhost:9999"},
proxy.UnsafeNewService("test.dev", "localhost:9999"),
})

out, _ := ListNameCommand{}.Execute(config)
Expand Down
2 changes: 1 addition & 1 deletion commands/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestList(t *testing.T) {

config := buildConfig([]proxy.Service{
{Name: "test", URL: "localhost:9999"},
proxy.UnsafeNewService("test", "localhost:9999"),
})

out, _ := ListCommand{}.Execute(config)
Expand Down
10 changes: 5 additions & 5 deletions commands/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
// USAGE:
// ergo remove myservicename
type RemoveServiceCommand struct {
Service proxy.Service
SearchTerm string
}

func findService(service proxy.Service, services map[string]proxy.Service) (*proxy.Service, bool) {
func findService(searchTerm string, services map[string]proxy.Service) (*proxy.Service, bool) {
for _, srv := range services {
if srv.URL == service.URL || srv.Name == service.Name {
if srv.URL.String() == searchTerm || srv.Name == searchTerm {
return &srv, true
}
}
Expand All @@ -26,10 +26,10 @@ func findService(service proxy.Service, services map[string]proxy.Service) (*pro

// Execute apply the RemoveServiceCommand
func (c RemoveServiceCommand) Execute(config *proxy.Config) (string, error) {
srv, isPresent := findService(c.Service, config.Services)
srv, isPresent := findService(c.SearchTerm, config.Services)

if !isPresent {
return "", fmt.Errorf("Service %s not found", c.Service.Name)
return "", fmt.Errorf("Service %s not found", c.SearchTerm)
}

err := proxy.RemoveService(config.ConfigFile, *srv)
Expand Down
16 changes: 8 additions & 8 deletions commands/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

func TestRemove(t *testing.T) {
config := buildConfig([]proxy.Service{
{Name: "test.dev", URL: "localhost:999"},
{Name: "test2.dev", URL: "localhost:9292"},
proxy.UnsafeNewService("test.dev", "http://localhost:999"),
proxy.UnsafeNewService("test2.dev", "http://localhost:9292"),
})

t.Run("when remove service", func(tt *testing.T) {
service := proxy.Service{Name: "test.dev"}
service := "test.dev"

command := RemoveServiceCommand{Service: service}
command := RemoveServiceCommand{SearchTerm: service}
out, err := command.Execute(config)
if err != nil {
t.Fatalf("Expected no error got: %s", err)
Expand All @@ -28,20 +28,20 @@ func TestRemove(t *testing.T) {
})

t.Run("when service not found", func(tt *testing.T) {
service := proxy.Service{Name: "doesntexist.dev"}
service := "doesntexist.dev"

command := RemoveServiceCommand{Service: service}
command := RemoveServiceCommand{SearchTerm: service}
_, err := command.Execute(config)
if err == nil {
t.Fatalf("Expected error got: %s", err)
}
})

t.Run("when config file not found", func(tt *testing.T) {
service := proxy.Service{Name: "test.dev"}
service := "test.dev"
config.ConfigFile = "undefined"

command := RemoveServiceCommand{Service: service}
command := RemoveServiceCommand{SearchTerm: service}
_, err := command.Execute(config)
if err == nil {
t.Fatalf("Expected error got: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion commands/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestRunCommand(t *testing.T) {

t.Run("when domain config has wrong domain format", func(tt *testing.T) {
config := buildConfig([]proxy.Service{
{Name: "test.dev", URL: "localhost:9999"},
proxy.UnsafeNewService("test.dev", "localhost:9999"),
})

config.Domain = "foobar"
Expand Down
8 changes: 4 additions & 4 deletions commands/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func TestSetup(t *testing.T) {
config := buildConfig([]proxy.Service{
{Name: "test.dev", URL: "localhost:999"},
proxy.UnsafeNewService("test.dev", "localhost:9999"),
})

command := SetupCommand{System: "inexistent-os", Remove: false}
Expand Down Expand Up @@ -73,7 +73,7 @@ func (r *TestRunnerWithOutput) Run(command string, args ...string) ([]byte, erro

func TestSetupLinuxGnome(t *testing.T) {
config := buildConfig([]proxy.Service{
{Name: "test.dev", URL: "localhost:999"},
proxy.UnsafeNewService("test.dev", "localhost:9999"),
})

t.Run("when setting up", func(t *testing.T) {
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestSetupLinuxGnome(t *testing.T) {

func TestSetupOSX(t *testing.T) {
config := buildConfig([]proxy.Service{
{Name: "test.dev", URL: "localhost:999"},
proxy.UnsafeNewService("test.dev", "localhost:999"),
})

t.Run("when setting up", func(t *testing.T) {
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestSetupOSX(t *testing.T) {

func TestSetupWindows(t *testing.T) {
config := buildConfig([]proxy.Service{
{Name: "test.dev", URL: "localhost:999"},
proxy.UnsafeNewService("test.dev", "localhost:999"),
})

t.Run("when setting up", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion commands/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestURLCommand(t *testing.T) {
config := buildConfig([]proxy.Service{
{Name: "test.dev", URL: "localhost:9999"},
proxy.UnsafeNewService("test.dev", "localhost:9999"),
})

t.Run("when found the service", func(tt *testing.T) {
Expand Down
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ func prepareSubCommand(args []string) (commands.Command, *proxy.Config) {
}

name := args[2]
url := args[3]
service := proxy.Service{Name: name, URL: url}
rawURL := args[3]
service, err := proxy.NewService(name, rawURL)
if err != nil {
log.Fatal(err)
}

command.Parse(args[4:])

Expand All @@ -111,11 +114,10 @@ func prepareSubCommand(args []string) (commands.Command, *proxy.Config) {
}

nameOrURL := args[2]
service := proxy.Service{Name: nameOrURL, URL: nameOrURL}

command.Parse(args[3:])

return commands.RemoveServiceCommand{Service: service}, config
return commands.RemoveServiceCommand{SearchTerm: nameOrURL}, config
}

return nil, nil
Expand Down
28 changes: 14 additions & 14 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func (r *TestRunner) Run(command string, args ...string) ([]byte, error) {

func TestListCommand(t *testing.T) {
services := map[string]proxy.Service{
"foo": {Name: "foo", URL: "http://localhost:3000"},
"bar": {Name: "bar", URL: "http://localhost:5000"},
"foo": proxy.UnsafeNewService("foo", "http://localhost:3000"),
"bar": proxy.UnsafeNewService("bar", "http://localhost:5000"),
}

t.Run("list services", func(tt *testing.T) {
Expand All @@ -126,8 +126,8 @@ func TestListCommand(t *testing.T) {

func TestListNamesCommand(t *testing.T) {
services := map[string]proxy.Service{
"foo": {Name: "foo", URL: "http://localhost:3000"},
"bar": {Name: "bar", URL: "http://localhost:5000"},
"foo": proxy.UnsafeNewService("foo", "http://localhost:3000"),
"bar": proxy.UnsafeNewService("bar", "http://localhost:5000"),
}

t.Run("list names services", func(tt *testing.T) {
Expand Down Expand Up @@ -276,8 +276,8 @@ func TestUrlCommand(t *testing.T) {
commandName := "URLCommand"

services := map[string]proxy.Service{
"foo": {Name: "foo", URL: "http://localhost:3000"},
"bar": {Name: "bar", URL: "http://localhost:5000"},
"foo": proxy.UnsafeNewService("foo", "http://localhost:3000"),
"bar": proxy.UnsafeNewService("bar", "http://localhost:5000"),
}

t.Run("when there is the service", func(tt *testing.T) {
Expand Down Expand Up @@ -356,8 +356,8 @@ func TestRunCommand(t *testing.T) {
func TestAddCommand(t *testing.T) {
t.Run("success", func(tt *testing.T) {
services := map[string]proxy.Service{
"foo": {Name: "foo", URL: "http://localhost:3000"},
"bar": {Name: "bar", URL: "http://localhost:5000"},
"foo": proxy.UnsafeNewService("foo", "http://localhost:3000"),
"bar": proxy.UnsafeNewService("bar", "http://localhost:5000"),
}

tmpfile, err := ioutil.TempFile("", "testaddservice")
Expand Down Expand Up @@ -444,8 +444,8 @@ func TestAddCommand(t *testing.T) {
func TestRemoveCommand(t *testing.T) {
t.Run("success", func(tt *testing.T) {
services := map[string]proxy.Service{
"foo": {Name: "foo", URL: "http://localhost:3000"},
"bar": {Name: "bar", URL: "http://localhost:5000"},
"foo": proxy.UnsafeNewService("foo", "http://localhost:3000"),
"bar": proxy.UnsafeNewService("bar", "http://localhost:5000"),
}

tmpfile, err := ioutil.TempFile("", "testaddservice")
Expand Down Expand Up @@ -679,28 +679,28 @@ func TestSubCommandFlags(t *testing.T) {
}{
{
title: "when -domain flag is passed for " + cmd,
args: []string{"ergo", cmd, "foo", "bla", "-domain", ".foo"},
args: []string{"ergo", cmd, "foo", "http://bla", "-domain", ".foo"},
config: &proxy.Config{
Domain: ".foo",
},
},
{
title: "when -config flag is passed for " + cmd,
args: []string{"ergo", cmd, "foo", "bla", "-config", ".file"},
args: []string{"ergo", cmd, "foo", "http://bla", "-config", ".file"},
config: &proxy.Config{
ConfigFile: ".file",
},
},
{
title: "when -p flag is passed for " + cmd,
args: []string{"ergo", cmd, "foo", "bla", "-p", "2002"},
args: []string{"ergo", cmd, "foo", "http://bla", "-p", "2002"},
config: &proxy.Config{
Port: "2002",
},
},
{
title: "when -V flag is passed for " + cmd,
args: []string{"ergo", cmd, "foo", "bla", "-V"},
args: []string{"ergo", cmd, "foo", "http://bla", "-V"},
config: &proxy.Config{
Verbose: true,
},
Expand Down
Loading

0 comments on commit e484c4c

Please sign in to comment.