Skip to content

Commit

Permalink
feat: enforce name without URL pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianoliveira committed Aug 6, 2023
1 parent 2381042 commit e4bf785
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 65 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
70 changes: 34 additions & 36 deletions proxy/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package proxy

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
Expand All @@ -27,88 +26,87 @@ func TestWhenHasErgoFile(t *testing.T) {

for _, c := range cases {
t.Run("It matches the service host with wildcard "+c.host, func(t *testing.T) {
result, err := config.GetService(c.host)
service, err := config.GetService(c.host)
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}

if result.Empty() {
t.Errorf("Expected result to not be nil")
if service.Empty() {
t.Errorf("Expected service to not be nil")
}
})
}
})

t.Run("It loads the services redirections", func(t *testing.T) {
expected := 9
result := len(config.Services)
service := len(config.Services)

if expected != result {
if expected != service {
t.Errorf("Expected to get %d services, but got %d\r\n%q",
expected, result, config.Services)
expected, service, config.Services)
}
})

t.Run("It match the service host mysite", func(t *testing.T) {
result, err := config.GetService("mysite.dev")
service, err := config.GetService("mysite.dev")
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}

if result.Empty() {
t.Errorf("Expected result to not be nil")
if service.Empty() {
t.Errorf("Expected service to not be nil")
}
})

t.Run("It match the service host mylocalsite", func(t *testing.T) {
result, err := config.GetService("mylocalsite.dev")
service, err := config.GetService("mylocalsite.dev")
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}

if result.Empty() {
t.Errorf("Expected result to not be nil")
if service.Empty() {
t.Errorf("Expected service to not be nil")
}
})

t.Run("It match the service host bla.dev", func(t *testing.T) {
result, err := config.GetService("bla.dev")
service, err := config.GetService("bla.dev")
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}

if result.Empty() {
t.Errorf("Expected result to not be nil")
if service.Empty() {
t.Errorf("Expected service to not be nil")
}
})

t.Run("It match the service host foo.dev", func(t *testing.T) {
result, err := config.GetService("foo.dev")
service, err := config.GetService("foo.dev")
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}

if result.Empty() {
t.Errorf("Expected result to not be nil")
if service.Empty() {
t.Errorf("Expected service to not be nil")
}
})

t.Run("It match the service host withspaces.dev", func(t *testing.T) {
result, err := config.GetService("withspaces.dev")
service, err := config.GetService("withspaces.dev")
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}

if result.Empty() {
t.Errorf("Expected result to not be nil")
if service.Empty() {
t.Errorf("Expected service to not be nil")
}
})

t.Run("It does not match the service host", func(t *testing.T) {
result, err := config.GetService("undefined.dev")
fmt.Println("result", result)
if result != nil {
t.Errorf("Expected result to be nil got: %#v", result)
service, err := config.GetService("undefined.dev")
if service != nil {
t.Errorf("Expected service to be nil got: %#v", service)
}

if err == nil {
Expand All @@ -117,35 +115,35 @@ func TestWhenHasErgoFile(t *testing.T) {
})

t.Run("It does match other protocols than http", func(t *testing.T) {
result, err := config.GetService("redis://redislocal.dev")
service, err := config.GetService("redislocal.dev")
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}

if result.Empty() {
t.Errorf("Expected result to not be nil")
if service.Empty() {
t.Errorf("Expected service to not be nil")
}
})

t.Run("It match subdomains", func(tt *testing.T) {
tt.Run("for one.domain.dev", func(tt *testing.T) {
result, err := config.GetService("one.domain.dev")
service, err := config.GetService("one.domain.dev")
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}
if result.Empty() {
tt.Errorf("Expected result to not be nil")
if service.Empty() {
tt.Errorf("Expected service to not be nil")
}
})

tt.Run("for two.domain.dev", func(tt *testing.T) {
result, err := config.GetService("two.domain.dev")
service, err := config.GetService("two.domain.dev")
if err != nil {
t.Errorf("Expected no error. Got: %s", err)
}

if result.Empty() {
tt.Errorf("Expected result to not be nil")
if service.Empty() {
tt.Errorf("Expected service to not be nil")
}
})
})
Expand Down Expand Up @@ -201,7 +199,7 @@ func TestWhenHasErgoFile(t *testing.T) {
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:4000
`)

Expand Down
2 changes: 1 addition & 1 deletion proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestWhenHasCollectionFile(t *testing.T) {
config.ConfigFile = "../.ergo"
err := config.LoadServices()
if err != nil {
t.Fatal("could not load requied configuration file for tests")
t.Fatalf("could not load requied configuration file for tests. %s", err)
}

proxy := NewErgoProxy(config)
Expand Down
4 changes: 4 additions & 0 deletions proxy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func NewService(name string, rawURL string) (Service, error) {
return Service{}, errors.New("Name and URL are required")
}

if strings.Contains(name, "://") || strings.Contains(name, ":") {
return Service{}, fmt.Errorf("Name '%v' is invalid, it can't be an URL", name)
}

url, err := url.ParseRequestURI(rawURL)
isInvalidHostname := len(url.Hostname()) == 0 || strings.Contains(url.Hostname(), ":")
if err != nil || isInvalidHostname {
Expand Down
64 changes: 37 additions & 27 deletions proxy/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"testing"
)


func TestNewService(t *testing.T) {
// Test cases
testCases := []struct {
name string;

Expand All @@ -15,39 +13,51 @@ func TestNewService(t *testing.T) {

expectError bool
}{
// {
// name: "empty name",
// serviceName: "",
// serviceURL: "http://localhost:8080",
// expectError: true,
// },
// {
// name: "empty url",
// serviceName: "test",
// serviceURL: "",
// expectError: true,
// },
// {
// name: "valid",
// serviceName: "test",
// serviceURL: "http://localhost:8080",
// expectError: false,
// },
{
name: "empty name",
serviceName: "",
serviceURL: "http://localhost:8080",
expectError: true,
},
{
name: "empty url",
serviceName: "test",
serviceURL: "",
expectError: true,
},
{
name: "a valid service with name and an valid URL",
serviceName: "test",
serviceURL: "http://localhost:8080",
expectError: false,
},
{
name: "invalid url - double port",
serviceName: "test",
serviceURL: "http://localhost:8080:8888",
expectError: true,
},
// {
// name: "invalid url - missing scheme",
// serviceName: "test",
// serviceURL: "localhost:8080",
// expectError: true,
// },
{
name: "invalid url - missing scheme",
serviceName: "test",
serviceURL: "localhost:8080",
expectError: true,
},
{
name: "invalid name - name contains an URL",
serviceName: "http://localhost:3333",
serviceURL: "http://localhost:8080",
expectError: true,
},
{
name: "invalid name - name contains an URL port",
serviceName: "localhost:3333",
serviceURL: "http://localhost:8080",
expectError: true,
},

}

// Run test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := NewService(tc.serviceName, tc.serviceURL)
Expand Down

0 comments on commit e4bf785

Please sign in to comment.