Skip to content

Commit

Permalink
Cleanup code: WIP 4
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Jun 3, 2023
1 parent fe447f2 commit b76b08d
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 52 deletions.
39 changes: 19 additions & 20 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ mappings:
proxy: localhost:8080
debug: true
https-port: 8081
cert-file: /cert-file.pem
key-file: /key-file.key
cert-file: /etc/certificates/cert-file.pem
key-file: /etc/certificates/key-file.key
`
)

Expand All @@ -68,14 +68,13 @@ mappings:
)

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

t.Run("correctly parse config", func(t *testing.T) {
tests := []struct {
Expand All @@ -89,7 +88,7 @@ func TestLoadConfiguration(t *testing.T) {
expected: &config.UncorsConfig{
HTTPPort: 80,
HTTPSPort: 443,
Mappings: []config.Mapping{},
Mappings: config.Mappings{},
},
},
{
Expand All @@ -98,7 +97,7 @@ func TestLoadConfiguration(t *testing.T) {
expected: &config.UncorsConfig{
HTTPPort: 8080,
HTTPSPort: 443,
Mappings: []config.Mapping{
Mappings: config.Mappings{
{From: "http://demo", To: "https://demo.com"},
},
},
Expand All @@ -108,7 +107,7 @@ func TestLoadConfiguration(t *testing.T) {
args: []string{params.Config, fullConfigPath},
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: []config.Mapping{
Mappings: config.Mappings{
{From: "http://demo1", To: "https://demo1.com"},
{From: "http://other-demo2", To: "https://demo2.io", Mocks: []config.Mock{
{
Expand All @@ -134,8 +133,8 @@ func TestLoadConfiguration(t *testing.T) {
Proxy: "localhost:8080",
Debug: true,
HTTPSPort: 8081,
CertFile: "/cert-file.pem",
KeyFile: "/key-file.key",
CertFile: testconstants.CertFilePath,
KeyFile: testconstants.KeyFilePath,
},
},
{
Expand All @@ -148,7 +147,7 @@ func TestLoadConfiguration(t *testing.T) {
},
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: []config.Mapping{
Mappings: config.Mappings{
{From: "http://demo1", To: "https://demo1.com"},
{
From: "http://other-demo2",
Expand Down Expand Up @@ -181,8 +180,8 @@ func TestLoadConfiguration(t *testing.T) {
Proxy: "localhost:8080",
Debug: true,
HTTPSPort: 8081,
CertFile: "/cert-file.pem",
KeyFile: "/key-file.key",
CertFile: testconstants.CertFilePath,
KeyFile: testconstants.KeyFilePath,
},
},
}
Expand Down Expand Up @@ -309,32 +308,32 @@ func TestUncorsConfigIsHTTPSEnabled(t *testing.T) {
name: "true when https configured",
config: &config.UncorsConfig{
HTTPSPort: 443,
CertFile: "/cert.cer",
KeyFile: "/cert.key",
CertFile: testconstants.CertFilePath,
KeyFile: testconstants.KeyFilePath,
},
expected: true,
},
{
name: "false when https port is not configured",
config: &config.UncorsConfig{
CertFile: "/cert.cer",
KeyFile: "/cert.key",
CertFile: testconstants.CertFilePath,
KeyFile: testconstants.KeyFilePath,
},
expected: false,
},
{
name: "false when cert file is not configured",
config: &config.UncorsConfig{
HTTPSPort: 443,
KeyFile: "/cert.key",
KeyFile: testconstants.KeyFilePath,
},
expected: false,
},
{
name: "false when key file is not configured",
config: &config.UncorsConfig{
HTTPSPort: 443,
CertFile: "/cert.cer",
CertFile: testconstants.CertFilePath,
},
expected: false,
},
Expand Down
12 changes: 8 additions & 4 deletions internal/config/mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ func TestMappings(t *testing.T) {
mappings config.Mappings
expected []string
}{
{
name: "no mapping and no mocks",
expected: []string{"\n"},
},
{
name: "http mapping only",
mappings: config.Mappings{
Expand Down Expand Up @@ -97,4 +93,12 @@ func TestMappings(t *testing.T) {
}
})
}

t.Run("empty", func(t *testing.T) {
var mappings config.Mappings

actual := mappings.String()

assert.Equal(t, "\n", actual)
})
}
40 changes: 26 additions & 14 deletions internal/config/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestResponseClone(t *testing.T) {
object := config.Response{
response := config.Response{
Code: http.StatusOK,
Headers: map[string]string{
headers.ContentType: "plain/text",
Expand All @@ -22,23 +22,23 @@ func TestResponseClone(t *testing.T) {
Delay: time.Hour,
}

actual := object.Clone()
clonedResponse := response.Clone()

t.Run("not same", func(t *testing.T) {
assert.NotSame(t, &object, &actual)
assert.NotSame(t, &response, &clonedResponse)
})

t.Run("equals values", func(t *testing.T) {
assert.EqualValues(t, object, actual)
assert.EqualValues(t, response, clonedResponse)
})

t.Run("not same Headers map", func(t *testing.T) {
assert.NotSame(t, &object.Headers, &actual.Headers)
assert.NotSame(t, &response.Headers, &clonedResponse.Headers)
})
}

func TestMockClone(t *testing.T) {
object := config.Mock{
mock := config.Mock{
Path: "/constants",
Method: http.MethodGet,
Queries: map[string]string{
Expand All @@ -55,25 +55,37 @@ func TestMockClone(t *testing.T) {
},
}

actual := object.Clone()
clonedMock := mock.Clone()

t.Run("not same", func(t *testing.T) {
assert.NotSame(t, &object, &actual)
assert.NotSame(t, &mock, &clonedMock)
})

t.Run("equals values", func(t *testing.T) {
assert.EqualValues(t, object, actual)
assert.EqualValues(t, mock, clonedMock)
})

t.Run("not same Headers map", func(t *testing.T) {
assert.NotSame(t, &object.Headers, &actual.Headers)
t.Run("not same headers map", func(t *testing.T) {
assert.NotSame(t, &mock.Headers, &clonedMock.Headers)
})

t.Run("equals headers map", func(t *testing.T) {
assert.EqualValues(t, mock.Headers, clonedMock.Headers)
})

t.Run("not same Queries map", func(t *testing.T) {
assert.NotSame(t, &object.Headers, &actual.Headers)
t.Run("not same queries map", func(t *testing.T) {
assert.NotSame(t, &mock.Queries, &clonedMock.Queries)
})

t.Run("equals queries map values", func(t *testing.T) {
assert.EqualValues(t, mock.Queries, clonedMock.Queries)
})

t.Run("not same Response", func(t *testing.T) {
assert.NotSame(t, &object.Response, &actual.Response)
assert.NotSame(t, &mock.Response, &clonedMock.Response)
})

t.Run("equals Response values", func(t *testing.T) {
assert.EqualValues(t, mock.Response, clonedMock.Response)
})
}
2 changes: 1 addition & 1 deletion internal/config/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestValidate(t *testing.T) {
{
name: "invalid http-port",
config: &config.UncorsConfig{
Mappings: []config.Mapping{},
Mappings: config.Mappings{},
},
expected: "Key: 'UncorsConfig.HTTPPort' Error:Field validation for 'HTTPPort' failed on the 'required' tag",
},
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/proxy/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestProxyMiddleware(t *testing.T) {
replacerFactory, err := urlreplacer.NewURLReplacerFactory([]config.Mapping{
replacerFactory, err := urlreplacer.NewURLReplacerFactory(config.Mappings{
{From: "http://premium.local.com", To: "https://premium.api.com"},
})
testutils.CheckNoError(t, err)
Expand Down
12 changes: 6 additions & 6 deletions internal/handler/uncors_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestUncorsRequestHandler(t *testing.T) {
"/mock.json": mockJSON,
})

mappings := []config.Mapping{
mappings := config.Mappings{
{
From: testconstants.HTTPLocalhost,
To: testconstants.HTTPSLocalhost,
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestMockMiddleware(t *testing.T) {
handler.WithHTTPClient(mocks.NewHTTPClientMock(t)),
handler.WithURLReplacerFactory(mocks.NewURLReplacerFactoryMock(t)),
handler.WithLogger(logger),
handler.WithMappings([]config.Mapping{
handler.WithMappings(config.Mappings{
{
From: "*",
To: "*",
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestMockMiddleware(t *testing.T) {
t.Run("where method is set", func(t *testing.T) {
expectedCode := 299
expectedBody := "forwarded"
mappings := []config.Mapping{
mappings := config.Mappings{
{From: "*", To: "*", Mocks: []config.Mock{{
Path: "/api",
Method: http.MethodPut,
Expand Down Expand Up @@ -415,7 +415,7 @@ func TestMockMiddleware(t *testing.T) {
t.Run("path handling", func(t *testing.T) {
expectedCode := 299
expectedBody := "forwarded"
mappings := []config.Mapping{
mappings := config.Mappings{
{From: "*", To: "*", Mocks: []config.Mock{
{
Path: userPath,
Expand Down Expand Up @@ -526,7 +526,7 @@ func TestMockMiddleware(t *testing.T) {
handler.WithHTTPClient(mocks.NewHTTPClientMock(t)),
handler.WithURLReplacerFactory(mocks.NewURLReplacerFactoryMock(t)),
handler.WithLogger(logger),
handler.WithMappings([]config.Mapping{
handler.WithMappings(config.Mappings{
{From: "*", To: "*", Mocks: []config.Mock{
{
Path: userPath,
Expand Down Expand Up @@ -622,7 +622,7 @@ func TestMockMiddleware(t *testing.T) {
handler.WithHTTPClient(mocks.NewHTTPClientMock(t)),
handler.WithURLReplacerFactory(mocks.NewURLReplacerFactoryMock(t)),
handler.WithLogger(logger),
handler.WithMappings([]config.Mapping{
handler.WithMappings(config.Mappings{
{From: "*", To: "*", Mocks: []config.Mock{
{
Path: userPath,
Expand Down
12 changes: 6 additions & 6 deletions internal/urlreplacer/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ func TestNewUrlReplacerFactory(t *testing.T) {
t.Run("should return error when", func(t *testing.T) {
tests := []struct {
name string
mapping []config.Mapping
mapping config.Mappings
}{
{
name: "mappings is empty",
mapping: make([]config.Mapping, 0),
mapping: make(config.Mappings, 0),
},
{
name: "source url is incorrect",
mapping: []config.Mapping{
mapping: config.Mappings{
{From: string(rune(0x7f)), To: testconstants.HTTPSGithub},
},
},
{
name: "target url is incorrect ",
mapping: []config.Mapping{
mapping: config.Mappings{
{From: testconstants.Localhost, To: string(rune(0x7f))},
},
},
Expand All @@ -45,7 +45,7 @@ func TestNewUrlReplacerFactory(t *testing.T) {
})

t.Run("should return replacers", func(t *testing.T) {
actual, err := urlreplacer.NewURLReplacerFactory([]config.Mapping{
actual, err := urlreplacer.NewURLReplacerFactory(config.Mappings{
{From: testconstants.Localhost, To: testconstants.HTTPSGithub},
})

Expand All @@ -55,7 +55,7 @@ func TestNewUrlReplacerFactory(t *testing.T) {
}

func TestFactoryMake(t *testing.T) {
factory, err := urlreplacer.NewURLReplacerFactory([]config.Mapping{
factory, err := urlreplacer.NewURLReplacerFactory(config.Mappings{
{From: "http://server1.com", To: "https://mappedserver1.com"},
{From: "https://server2.com", To: "https://mappedserver2.com"},
})
Expand Down
6 changes: 6 additions & 0 deletions testing/testconstants/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package testconstants

const (
CertFilePath = "/etc/certificates/cert-file.pem"
KeyFilePath = "/etc/certificates/key-file.key"
)

0 comments on commit b76b08d

Please sign in to comment.