Skip to content

Commit

Permalink
Cleanup code: WIP 3
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Jun 3, 2023
1 parent 1e8d2af commit fe447f2
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 129 deletions.
109 changes: 99 additions & 10 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,67 @@ import (

const acceptEncoding = "accept-encoding"

const (
corruptedConfigPath = "/corrupted-config.yaml"
corruptedConfig = `http-port: 8080
mappings&
- http://demo: https://demo.com
`
)

const (
fullConfigPath = "/full-config.yaml"
fullConfig = `
http-port: 8080
mappings:
- http://demo1: https://demo1.com
- from: http://other-demo2
to: https://demo2.io
mocks:
- path: /demo
method: POST
queries:
foo: bar
headers:
Accept-Encoding: deflate
response:
code: 201
headers:
Accept-Encoding: deflate
raw: demo
file: /demo.txt
proxy: localhost:8080
debug: true
https-port: 8081
cert-file: /cert-file.pem
key-file: /key-file.key
`
)

const (
incorrectConfigPath = "/incorrect-config.yaml"
incorrectConfig = `http-port: xxx
mappings:
- http://demo: https://demo.com
`
)

const (
minimalConfigPath = "/minimal-config.yaml"
minimalConfig = `
http-port: 8080
mappings:
- http://demo: https://demo.com
`
)

func TestLoadConfiguration(t *testing.T) {
fs := testutils.PrepareFsForTests(t, "config_test_data")
fs := testutils.FsFromMap(t, map[string]string{
corruptedConfigPath: corruptedConfig,
fullConfigPath: fullConfig,
incorrectConfigPath: incorrectConfig,
minimalConfigPath: minimalConfig,
})
viperInstance := viper.New()
viperInstance.SetFs(fs)

Expand All @@ -35,7 +94,7 @@ func TestLoadConfiguration(t *testing.T) {
},
{
name: "minimal config is set",
args: []string{params.Config, "/minimal-config.yaml"},
args: []string{params.Config, minimalConfigPath},
expected: &config.UncorsConfig{
HTTPPort: 8080,
HTTPSPort: 443,
Expand All @@ -46,7 +105,7 @@ func TestLoadConfiguration(t *testing.T) {
},
{
name: "read all fields from config file config is set",
args: []string{params.Config, "/full-config.yaml"},
args: []string{params.Config, fullConfigPath},
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: []config.Mapping{
Expand Down Expand Up @@ -82,7 +141,7 @@ func TestLoadConfiguration(t *testing.T) {
{
name: "read all fields from config file config is set",
args: []string{
params.Config, "/full-config.yaml",
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,
Expand Down Expand Up @@ -188,13 +247,13 @@ func TestLoadConfiguration(t *testing.T) {
},
expected: []string{
"filed to read config file '/not-exist-config.yaml': open ",
"test_data/not-exist-config.yaml: no such file or directory",
"open /not-exist-config.yaml: file does not exist",
},
},
{
name: "config file is corrupted",
args: []string{
params.Config, "/corrupted-config.yaml",
params.Config, corruptedConfigPath,
},
expected: []string{
"filed to read config file '/corrupted-config.yaml': " +
Expand All @@ -214,7 +273,7 @@ func TestLoadConfiguration(t *testing.T) {
{
name: "incorrect type in config file",
args: []string{
params.Config, "/incorrect-config.yaml",
params.Config, incorrectConfigPath,
},
expected: []string{
"filed parsing config: 1 error(s) decoding:\n\n* cannot parse 'http-port' as int:" +
Expand Down Expand Up @@ -247,14 +306,44 @@ func TestUncorsConfigIsHTTPSEnabled(t *testing.T) {
expected: false,
},
{
name: "false by default",
config: &config.UncorsConfig{},
name: "true when https configured",
config: &config.UncorsConfig{
HTTPSPort: 443,
CertFile: "/cert.cer",
KeyFile: "/cert.key",
},
expected: true,
},
{
name: "false when https port is not configured",
config: &config.UncorsConfig{
CertFile: "/cert.cer",
KeyFile: "/cert.key",
},
expected: false,
},
{
name: "false when cert file is not configured",
config: &config.UncorsConfig{
HTTPSPort: 443,
KeyFile: "/cert.key",
},
expected: false,
},
{
name: "false when key file is not configured",
config: &config.UncorsConfig{
HTTPSPort: 443,
CertFile: "/cert.cer",
},
expected: false,
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.expected, testCase.config.IsHTTPSEnabled())
actual := testCase.config.IsHTTPSEnabled()

assert.Equal(t, testCase.expected, actual)
})
}
}
3 changes: 0 additions & 3 deletions internal/config/config_test_data/corrupted-config.yaml

This file was deleted.

23 changes: 0 additions & 23 deletions internal/config/config_test_data/full-config.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions internal/config/config_test_data/incorrect-config.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions internal/config/config_test_data/minimal-config.yaml

This file was deleted.

102 changes: 51 additions & 51 deletions internal/config/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,54 @@ func TestNormaliseMappings(t *testing.T) {
httpPort, httpsPort := 3000, 3001
testsCases := []struct {
name string
mappings []config.Mapping
expected []config.Mapping
mappings config.Mappings
expected config.Mappings
useHTTPS bool
}{
{
name: "correctly set http and https ports",
mappings: []config.Mapping{
{From: "localhost", To: "github.com"},
mappings: config.Mappings{
{From: testconstants.Localhost, To: testconstants.Github},
},
expected: []config.Mapping{
{From: "http://localhost:3000", To: "github.com"},
{From: "https://localhost:3001", To: "github.com"},
expected: config.Mappings{
{From: testconstants.HTTPLocalhostWithPort(httpPort), To: testconstants.Github},
{From: testconstants.HTTPSLocalhostWithPort(httpsPort), To: testconstants.Github},
},
useHTTPS: true,
},
{
name: "correctly set http port",
mappings: []config.Mapping{
{From: testconstants.HTTPLocalhost, To: "https://github.com"},
mappings: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
},
expected: []config.Mapping{
{From: "http://localhost:3000", To: "https://github.com"},
expected: config.Mappings{
{From: testconstants.HTTPLocalhostWithPort(httpPort), To: testconstants.HTTPSGithub},
},
useHTTPS: true,
},
{
name: "correctly set https port",
mappings: []config.Mapping{
{From: testconstants.HTTPSLocalhost, To: "https://github.com"},
mappings: config.Mappings{
{From: testconstants.HTTPSLocalhost, To: testconstants.HTTPSGithub},
},
expected: []config.Mapping{
{From: "https://localhost:3001", To: "https://github.com"},
expected: config.Mappings{
{From: testconstants.HTTPSLocalhostWithPort(httpsPort), To: testconstants.HTTPSGithub},
},
useHTTPS: true,
},
{
name: "correctly set mixed schemes",
mappings: []config.Mapping{
{From: "host1", To: "https://github.com"},
{From: "host2", To: "http://github.com"},
mappings: config.Mappings{
{From: testconstants.Host1, To: testconstants.HTTPSGithub},
{From: "host2", To: testconstants.HTTPGithub},
{From: "http://host3", To: "http://api.github.com"},
{From: "https://host4", To: "https://api.github.com"},
},
expected: []config.Mapping{
{From: "http://host1:3000", To: "https://github.com"},
{From: "https://host1:3001", To: "https://github.com"},
{From: "http://host2:3000", To: "http://github.com"},
{From: "https://host2:3001", To: "http://github.com"},
expected: config.Mappings{
{From: "http://host1:3000", To: testconstants.HTTPSGithub},
{From: "https://host1:3001", To: testconstants.HTTPSGithub},
{From: "http://host2:3000", To: testconstants.HTTPGithub},
{From: "https://host2:3001", To: testconstants.HTTPGithub},
{From: "http://host3:3000", To: "http://api.github.com"},
{From: "https://host4:3001", To: "https://api.github.com"},
},
Expand All @@ -87,54 +87,54 @@ func TestNormaliseMappings(t *testing.T) {
httpPort, httpsPort := 80, 443
testsCases := []struct {
name string
mappings []config.Mapping
expected []config.Mapping
mappings config.Mappings
expected config.Mappings
useHTTPS bool
}{
{
name: "correctly set http and https ports",
mappings: []config.Mapping{
{From: "localhost", To: "github.com"},
mappings: config.Mappings{
{From: testconstants.Localhost, To: testconstants.Github},
},
expected: []config.Mapping{
{From: testconstants.HTTPLocalhost, To: "github.com"},
{From: testconstants.HTTPSLocalhost, To: "github.com"},
expected: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.Github},
{From: testconstants.HTTPSLocalhost, To: testconstants.Github},
},
useHTTPS: true,
},
{
name: "correctly set http port",
mappings: []config.Mapping{
{From: testconstants.HTTPLocalhost, To: "https://github.com"},
mappings: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
},
expected: []config.Mapping{
{From: testconstants.HTTPLocalhost, To: "https://github.com"},
expected: config.Mappings{
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
},
useHTTPS: true,
},
{
name: "correctly set https port",
mappings: []config.Mapping{
{From: testconstants.HTTPSLocalhost, To: "https://github.com"},
mappings: config.Mappings{
{From: testconstants.HTTPSLocalhost, To: testconstants.HTTPSGithub},
},
expected: []config.Mapping{
{From: testconstants.HTTPSLocalhost, To: "https://github.com"},
expected: config.Mappings{
{From: testconstants.HTTPSLocalhost, To: testconstants.HTTPSGithub},
},
useHTTPS: true,
},
{
name: "correctly set mixed schemes",
mappings: []config.Mapping{
{From: "host1", To: "https://github.com"},
{From: "host2", To: "http://github.com"},
mappings: config.Mappings{
{From: testconstants.Host1, To: testconstants.HTTPSGithub},
{From: "host2", To: testconstants.HTTPGithub},
{From: "http://host3", To: "http://api.github.com"},
{From: "https://host4", To: "https://api.github.com"},
},
expected: []config.Mapping{
{From: "http://host1", To: "https://github.com"},
{From: "https://host1", To: "https://github.com"},
{From: "http://host2", To: "http://github.com"},
{From: "https://host2", To: "http://github.com"},
expected: config.Mappings{
{From: testconstants.HTTPHost1, To: testconstants.HTTPSGithub},
{From: testconstants.HTTPSHost1, To: testconstants.HTTPSGithub},
{From: "http://host2", To: testconstants.HTTPGithub},
{From: "https://host2", To: testconstants.HTTPGithub},
{From: "http://host3", To: "http://api.github.com"},
{From: "https://host4", To: "https://api.github.com"},
},
Expand All @@ -159,16 +159,16 @@ func TestNormaliseMappings(t *testing.T) {
t.Run("incorrect mappings", func(t *testing.T) {
testsCases := []struct {
name string
mappings []config.Mapping
mappings config.Mappings
httpPort int
httpsPort int
useHTTPS bool
expectedErr string
}{
{
name: "incorrect source url",
mappings: []config.Mapping{
{From: "loca^host", To: "github.com"},
mappings: config.Mappings{
{From: "loca^host", To: testconstants.Github},
},
httpPort: 3000,
httpsPort: 3001,
Expand All @@ -177,8 +177,8 @@ func TestNormaliseMappings(t *testing.T) {
},
{
name: "incorrect port in source url",
mappings: []config.Mapping{
{From: "localhost:", To: "github.com"},
mappings: config.Mappings{
{From: "localhost:", To: testconstants.Github},
},
httpPort: -1,
httpsPort: 3001,
Expand Down
Loading

0 comments on commit fe447f2

Please sign in to comment.