Skip to content

Commit

Permalink
Refactoring: hosts constants
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 21, 2023
1 parent f6529c6 commit d8b85ee
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 191 deletions.
56 changes: 24 additions & 32 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
package config_test

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/testing/hosts"
"github.com/evg4b/uncors/testing/testconstants"
"github.com/evg4b/uncors/testing/testutils"
"github.com/evg4b/uncors/testing/testutils/params"
Expand Down Expand Up @@ -86,14 +86,6 @@ func TestLoadConfiguration(t *testing.T) {
})

t.Run("correctly parse config", func(t *testing.T) {
HTTPf := func(host string, port int) string {
return fmt.Sprintf("http://%s:%d", host, port)
}

HTTPSf := func(host string, port int) string {
return fmt.Sprintf("https://%s:%d", host, port)
}

tests := []struct {
name string
args []string
Expand All @@ -120,7 +112,7 @@ func TestLoadConfiguration(t *testing.T) {
HTTPPort: 8080,
HTTPSPort: 443,
Mappings: config.Mappings{
{From: testconstants.HTTPLocalhostWithPort(8080), To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTPPort(8080), To: hosts.Github.HTTPS()},
},
CacheConfig: config.CacheConfig{
ExpirationTime: config.DefaultExpirationTime,
Expand All @@ -135,10 +127,10 @@ func TestLoadConfiguration(t *testing.T) {
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: config.Mappings{
{From: testconstants.HTTPLocalhostWithPort(8080), To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTPPort(8080), To: hosts.Github.HTTPS()},
{
From: testconstants.HTTPLocalhost2WithPort(8080),
To: testconstants.HTTPSStackoverflow,
From: hosts.Localhost2.HTTPPort(8080),
To: hosts.Stackoverflow.HTTPS(),
Mocks: config.Mocks{
{
Path: "/demo",
Expand All @@ -161,7 +153,7 @@ func TestLoadConfiguration(t *testing.T) {
},
},
},
Proxy: "localhost:8080",
Proxy: hosts.Localhost.Port(8080),
Debug: true,
HTTPSPort: 8081,
CertFile: testconstants.CertFilePath,
Expand All @@ -180,17 +172,17 @@ func TestLoadConfiguration(t *testing.T) {
name: "read all fields from config file config is set",
args: []string{
params.Config, fullConfigPath,
params.From, testconstants.SourceHost1, params.To, testconstants.TargetHost1,
params.From, testconstants.SourceHost2, params.To, testconstants.TargetHost2,
params.From, testconstants.SourceHost3, params.To, testconstants.TargetHost3,
params.From, hosts.Localhost1.Host(), params.To, hosts.Github.Host(),
params.From, hosts.Localhost2.Host(), params.To, hosts.Stackoverflow.Host(),
params.From, hosts.Localhost3.Host(), params.To, hosts.APIGithub.Host(),
},
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: config.Mappings{
{From: testconstants.HTTPLocalhostWithPort(8080), To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTPPort(8080), To: hosts.Github.HTTPS()},
{
From: testconstants.HTTPLocalhost2WithPort(8080),
To: testconstants.HTTPSStackoverflow,
From: hosts.Localhost2.HTTPPort(8080),
To: hosts.Stackoverflow.HTTPS(),
Mocks: config.Mocks{
{
Path: "/demo",
Expand All @@ -212,14 +204,14 @@ func TestLoadConfiguration(t *testing.T) {
},
},
},
{From: HTTPf(testconstants.SourceHost1, 8080), To: testconstants.TargetHost1},
{From: HTTPSf(testconstants.SourceHost1, 8081), To: testconstants.TargetHost1},
{From: HTTPf(testconstants.SourceHost2, 8080), To: testconstants.TargetHost2},
{From: HTTPSf(testconstants.SourceHost2, 8081), To: testconstants.TargetHost2},
{From: HTTPf(testconstants.SourceHost3, 8080), To: testconstants.TargetHost3},
{From: HTTPSf(testconstants.SourceHost3, 8081), To: testconstants.TargetHost3},
{From: hosts.Localhost1.HTTPPort(8080), To: hosts.Github.Host()},
{From: hosts.Localhost1.HTTPSPort(8081), To: hosts.Github.Host()},
{From: hosts.Localhost2.HTTPPort(8080), To: hosts.Stackoverflow.Host()},
{From: hosts.Localhost2.HTTPSPort(8081), To: hosts.Stackoverflow.Host()},
{From: hosts.Localhost3.HTTPPort(8080), To: hosts.APIGithub.Host()},
{From: hosts.Localhost3.HTTPSPort(8081), To: hosts.APIGithub.Host()},
},
Proxy: "localhost:8080",
Proxy: hosts.Localhost.Port(8080),
Debug: true,
HTTPSPort: 8081,
CertFile: testconstants.CertFilePath,
Expand Down Expand Up @@ -267,7 +259,7 @@ func TestLoadConfiguration(t *testing.T) {
{
name: "return default config",
args: []string{
params.To, testconstants.TargetHost1,
params.To, hosts.Github.Host(),
},
expected: []string{
"`from` values are not set for every `to`",
Expand All @@ -276,8 +268,8 @@ func TestLoadConfiguration(t *testing.T) {
{
name: "count of from values great then count of to",
args: []string{
params.From, testconstants.SourceHost1, params.To, testconstants.TargetHost1,
params.From, testconstants.SourceHost2,
params.From, hosts.Localhost1.Host(), params.To, hosts.Github.Host(),
params.From, hosts.Localhost2.Host(),
},
expected: []string{
"`to` values are not set for every `from`",
Expand All @@ -286,8 +278,8 @@ func TestLoadConfiguration(t *testing.T) {
{
name: "count of to values great then count of from",
args: []string{
params.From, testconstants.SourceHost1, params.To, testconstants.TargetHost1,
params.To, testconstants.TargetHost2,
params.From, hosts.Localhost1.Host(), params.To, hosts.Github.Host(),
params.To, hosts.Stackoverflow.Host(),
},
expected: []string{
"`from` values are not set for every `to`",
Expand Down
74 changes: 37 additions & 37 deletions internal/config/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/testing/testconstants"
"github.com/evg4b/uncors/testing/hosts"
"github.com/stretchr/testify/assert"
)

Expand All @@ -25,49 +25,49 @@ func TestNormaliseMappings(t *testing.T) {
{
name: "correctly set http and https ports",
mappings: config.Mappings{
{From: testconstants.Localhost, To: testconstants.Github},
{From: hosts.Localhost.Host(), To: hosts.Github.Host()},
},
expected: config.Mappings{
{From: testconstants.HTTPLocalhostWithPort(httpPort), To: testconstants.Github},
{From: testconstants.HTTPSLocalhostWithPort(httpsPort), To: testconstants.Github},
{From: hosts.Localhost.HTTPPort(httpPort), To: hosts.Github.Host()},
{From: hosts.Localhost.HTTPSPort(httpsPort), To: hosts.Github.Host()},
},
useHTTPS: true,
},
{
name: "correctly set http port",
mappings: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTP(), To: hosts.Github.HTTPS()},
},
expected: config.Mappings{
{From: testconstants.HTTPLocalhostWithPort(httpPort), To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTPPort(httpPort), To: hosts.Github.HTTPS()},
},
useHTTPS: true,
},
{
name: "correctly set https port",
mappings: config.Mappings{
{From: testconstants.HTTPSLocalhost, To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTPS(), To: hosts.Github.HTTPS()},
},
expected: config.Mappings{
{From: testconstants.HTTPSLocalhostWithPort(httpsPort), To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTPSPort(httpsPort), To: hosts.Github.HTTPS()},
},
useHTTPS: true,
},
{
name: "correctly set mixed schemes",
mappings: config.Mappings{
{From: testconstants.Localhost1, To: testconstants.HTTPSGithub},
{From: testconstants.Localhost2, To: testconstants.HTTPGithub},
{From: testconstants.HTTPLocalhost3, To: testconstants.HTTPAPIGithub},
{From: testconstants.HTTPSLocalhost4, To: testconstants.HTTPSAPIGithub},
{From: hosts.Localhost1.Host(), To: hosts.Github.HTTPS()},
{From: hosts.Localhost2.Host(), To: hosts.Github.HTTP()},
{From: hosts.Localhost3.HTTP(), To: hosts.APIGithub.HTTP()},
{From: hosts.Localhost4.HTTPS(), To: hosts.APIGithub.HTTPS()},
},
expected: config.Mappings{
{From: testconstants.HTTPLocalhost1WithPort(httpPort), To: testconstants.HTTPSGithub},
{From: testconstants.HTTPSLocalhost1WithPort(httpsPort), To: testconstants.HTTPSGithub},
{From: testconstants.HTTPLocalhost2WithPort(httpPort), To: testconstants.HTTPGithub},
{From: testconstants.HTTPSLocalhost2WithPort(httpsPort), To: testconstants.HTTPGithub},
{From: testconstants.HTTPLocalhost3WithPort(httpPort), To: testconstants.HTTPAPIGithub},
{From: testconstants.HTTPSLocalhost4WithPort(httpsPort), To: testconstants.HTTPSAPIGithub},
{From: hosts.Localhost1.HTTPPort(httpPort), To: hosts.Github.HTTPS()},
{From: hosts.Localhost1.HTTPSPort(httpsPort), To: hosts.Github.HTTPS()},
{From: hosts.Localhost2.HTTPPort(httpPort), To: hosts.Github.HTTP()},
{From: hosts.Localhost2.HTTPSPort(httpsPort), To: hosts.Github.HTTP()},
{From: hosts.Localhost3.HTTPPort(httpPort), To: hosts.APIGithub.HTTP()},
{From: hosts.Localhost4.HTTPSPort(httpsPort), To: hosts.APIGithub.HTTPS()},
},
useHTTPS: true,
},
Expand Down Expand Up @@ -97,49 +97,49 @@ func TestNormaliseMappings(t *testing.T) {
{
name: "correctly set http and https ports",
mappings: config.Mappings{
{From: testconstants.Localhost, To: testconstants.Github},
{From: hosts.Localhost.Host(), To: hosts.Github.Host()},
},
expected: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.Github},
{From: testconstants.HTTPSLocalhost, To: testconstants.Github},
{From: hosts.Localhost.HTTP(), To: hosts.Github.Host()},
{From: hosts.Localhost.HTTPS(), To: hosts.Github.Host()},
},
useHTTPS: true,
},
{
name: "correctly set http port",
mappings: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTP(), To: hosts.Github.HTTPS()},
},
expected: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTP(), To: hosts.Github.HTTPS()},
},
useHTTPS: true,
},
{
name: "correctly set https port",
mappings: config.Mappings{
{From: testconstants.HTTPSLocalhost, To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTPS(), To: hosts.Github.HTTPS()},
},
expected: config.Mappings{
{From: testconstants.HTTPSLocalhost, To: testconstants.HTTPSGithub},
{From: hosts.Localhost.HTTPS(), To: hosts.Github.HTTPS()},
},
useHTTPS: true,
},
{
name: "correctly set mixed schemes",
mappings: config.Mappings{
{From: testconstants.Localhost1, To: testconstants.HTTPSGithub},
{From: testconstants.Localhost2, To: testconstants.HTTPGithub},
{From: testconstants.HTTPLocalhost3, To: testconstants.HTTPAPIGithub},
{From: testconstants.HTTPSLocalhost4, To: testconstants.HTTPSAPIGithub},
{From: hosts.Localhost1.Host(), To: hosts.Github.HTTPS()},
{From: hosts.Localhost2.Host(), To: hosts.Github.HTTP()},
{From: hosts.Localhost3.HTTP(), To: hosts.APIGithub.HTTP()},
{From: hosts.Localhost4.HTTPS(), To: hosts.APIGithub.HTTPS()},
},
expected: config.Mappings{
{From: testconstants.HTTPLocalhost1, To: testconstants.HTTPSGithub},
{From: testconstants.HTTPSLocalhost1, To: testconstants.HTTPSGithub},
{From: testconstants.HTTPLocalhost2, To: testconstants.HTTPGithub},
{From: testconstants.HTTPSLocalhost2, To: testconstants.HTTPGithub},
{From: testconstants.HTTPLocalhost3, To: testconstants.HTTPAPIGithub},
{From: testconstants.HTTPSLocalhost4, To: testconstants.HTTPSAPIGithub},
{From: hosts.Localhost1.HTTP(), To: hosts.Github.HTTPS()},
{From: hosts.Localhost1.HTTPS(), To: hosts.Github.HTTPS()},
{From: hosts.Localhost2.HTTP(), To: hosts.Github.HTTP()},
{From: hosts.Localhost2.HTTPS(), To: hosts.Github.HTTP()},
{From: hosts.Localhost3.HTTP(), To: hosts.APIGithub.HTTP()},
{From: hosts.Localhost4.HTTPS(), To: hosts.APIGithub.HTTPS()},
},
useHTTPS: true,
},
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestNormaliseMappings(t *testing.T) {
{
name: "incorrect source url",
mappings: config.Mappings{
{From: "loca^host", To: testconstants.Github},
{From: "loca^host", To: hosts.Github.Host()},
},
httpPort: httpPort,
httpsPort: httpsPort,
Expand All @@ -180,7 +180,7 @@ func TestNormaliseMappings(t *testing.T) {
{
name: "incorrect port in source url",
mappings: config.Mappings{
{From: "localhost:", To: testconstants.Github},
{From: "localhost:", To: hosts.Github.Host()},
},
httpPort: -1,
httpsPort: httpsPort,
Expand Down
16 changes: 8 additions & 8 deletions internal/config/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/testing/testconstants"
"github.com/evg4b/uncors/testing/hosts"
"github.com/evg4b/uncors/testing/testutils"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand All @@ -25,16 +25,16 @@ func TestURLMappingHookFunc(t *testing.T) {
name: "simple key-value mapping",
config: "http://localhost:4200: https://github.com",
expected: config.Mapping{
From: testconstants.HTTPLocalhostWithPort(4200),
To: testconstants.HTTPSGithub,
From: hosts.Localhost.HTTPPort(4200),
To: hosts.Github.HTTPS(),
},
},
{
name: "full object mapping",
config: "{ from: http://localhost:3000, to: https://api.github.com }",
expected: config.Mapping{
From: testconstants.HTTPLocalhostWithPort(3000),
To: testconstants.HTTPSAPIGithub,
From: hosts.Localhost.HTTPPort(3000),
To: hosts.APIGithub.HTTPS(),
},
},
}
Expand Down Expand Up @@ -73,20 +73,20 @@ func TestURLMappingClone(t *testing.T) {
{
name: "structure with 1 field",
expected: config.Mapping{
From: testconstants.HTTPLocalhost,
From: hosts.Localhost.HTTP(),
},
},
{
name: "structure with 2 field",
expected: config.Mapping{
From: testconstants.HTTPLocalhost,
From: hosts.Localhost.HTTP(),
To: localhostSecure,
},
},
{
name: "structure with inner collections",
expected: config.Mapping{
From: testconstants.HTTPLocalhost,
From: hosts.Localhost.HTTP(),
To: localhostSecure,
Statics: []config.StaticDirectory{
{Path: "/cc", Dir: "cc"},
Expand Down
Loading

0 comments on commit d8b85ee

Please sign in to comment.