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 781e976 commit 374daac
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 75 deletions.
56 changes: 30 additions & 26 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const (
fullConfig = `
http-port: 8080
mappings:
- http://demo1: https://demo1.com
- from: http://other-demo2
to: https://demo2.io
- http://localhost: https://github.com
- from: http://localhost2
to: https://stackoverflow.com
mocks:
- path: /demo
method: POST
Expand All @@ -54,7 +54,7 @@ const (
incorrectConfigPath = "/incorrect-config.yaml"
incorrectConfig = `http-port: xxx
mappings:
- http://demo: https://demo.com
- http://localhost: https://github.com
`
)

Expand All @@ -63,7 +63,7 @@ const (
minimalConfig = `
http-port: 8080
mappings:
- http://demo: https://demo.com
- http://localhost: https://github.com
`
)

Expand Down Expand Up @@ -98,7 +98,7 @@ func TestLoadConfiguration(t *testing.T) {
HTTPPort: 8080,
HTTPSPort: 443,
Mappings: config.Mappings{
{From: "http://demo", To: "https://demo.com"},
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
},
},
},
Expand All @@ -108,27 +108,31 @@ func TestLoadConfiguration(t *testing.T) {
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: config.Mappings{
{From: "http://demo1", To: "https://demo1.com"},
{From: "http://other-demo2", To: "https://demo2.io", Mocks: []config.Mock{
{
Path: "/demo",
Method: "POST",
Queries: map[string]string{
"foo": "bar",
},
Headers: map[string]string{
acceptEncoding: "deflate",
},
Response: config.Response{
Code: 201,
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
{
From: testconstants.HTTPLocalhost2,
To: testconstants.HTTPSStackoverflow,
Mocks: config.Mocks{
{
Path: "/demo",
Method: "POST",
Queries: map[string]string{
"foo": "bar",
},
Headers: map[string]string{
acceptEncoding: "deflate",
},
Raw: "demo",
File: "/demo.txt",
Response: config.Response{
Code: 201,
Headers: map[string]string{
acceptEncoding: "deflate",
},
Raw: "demo",
File: "/demo.txt",
},
},
},
}},
},
},
Proxy: "localhost:8080",
Debug: true,
Expand All @@ -148,11 +152,11 @@ func TestLoadConfiguration(t *testing.T) {
expected: &config.UncorsConfig{
HTTPPort: 8080,
Mappings: config.Mappings{
{From: "http://demo1", To: "https://demo1.com"},
{From: testconstants.HTTPLocalhost, To: testconstants.HTTPSGithub},
{
From: "http://other-demo2",
To: "https://demo2.io",
Mocks: []config.Mock{
From: testconstants.HTTPLocalhost2,
To: testconstants.HTTPSStackoverflow,
Mocks: config.Mocks{
{
Path: "/demo",
Method: "POST",
Expand Down
52 changes: 28 additions & 24 deletions internal/config/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
"github.com/stretchr/testify/assert"
)

const (
httpPort = 3000
httpsPort = 3001
)

func TestNormaliseMappings(t *testing.T) {
t.Run("custom port handling", func(t *testing.T) {
httpPort, httpsPort := 3000, 3001
testsCases := []struct {
name string
mappings config.Mappings
Expand Down Expand Up @@ -52,18 +56,18 @@ func TestNormaliseMappings(t *testing.T) {
{
name: "correctly set mixed schemes",
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"},
{From: testconstants.Localhost1, To: testconstants.HTTPSGithub},
{From: testconstants.Localhost2, To: testconstants.HTTPGithub},
{From: testconstants.HTTPLocalhost3, To: testconstants.HTTPAPIGithub},
{From: testconstants.HTTPSLocalhost4, To: testconstants.HTTPSAPIGithub},
},
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"},
{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},
},
useHTTPS: true,
},
Expand Down Expand Up @@ -125,18 +129,18 @@ func TestNormaliseMappings(t *testing.T) {
{
name: "correctly set mixed schemes",
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"},
{From: testconstants.Localhost1, To: testconstants.HTTPSGithub},
{From: testconstants.Localhost2, To: testconstants.HTTPGithub},
{From: testconstants.HTTPLocalhost3, To: testconstants.HTTPAPIGithub},
{From: testconstants.HTTPSLocalhost4, To: testconstants.HTTPSAPIGithub},
},
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"},
{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},
},
useHTTPS: true,
},
Expand Down Expand Up @@ -170,8 +174,8 @@ func TestNormaliseMappings(t *testing.T) {
mappings: config.Mappings{
{From: "loca^host", To: testconstants.Github},
},
httpPort: 3000,
httpsPort: 3001,
httpPort: httpPort,
httpsPort: httpsPort,
useHTTPS: true,
expectedErr: "failed to parse source url: parse \"//loca^host\": invalid character \"^\" in host name",
},
Expand All @@ -181,7 +185,7 @@ func TestNormaliseMappings(t *testing.T) {
{From: "localhost:", To: testconstants.Github},
},
httpPort: -1,
httpsPort: 3001,
httpsPort: httpsPort,
useHTTPS: true,
expectedErr: "failed to parse source url: port \"//localhost:\": empty port",
},
Expand Down
8 changes: 4 additions & 4 deletions internal/config/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "http://localhost:4200",
From: testconstants.HTTPLocalhostWithPort(4200),
To: testconstants.HTTPSGithub,
},
},
{
name: "full object mapping",
config: "{ from: http://localhost:3000, to: https://google.com }",
config: "{ from: http://localhost:3000, to: https://api.github.com }",
expected: config.Mapping{
From: "http://localhost:3000",
To: "https://google.com",
From: testconstants.HTTPLocalhostWithPort(3000),
To: testconstants.HTTPSAPIGithub,
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestMappings(t *testing.T) {
{
From: testconstants.HTTPLocalhost,
To: testconstants.HTTPSGithub,
Mocks: []config.Mock{
Mocks: config.Mocks{
{
Path: "/endpoint-1",
Method: http.MethodPost,
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/mocked_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/gorilla/mux"
)

func (m *RequestHandler) makeMockedRoutes(router *mux.Router, mocks []config.Mock) {
var defaultMocks []config.Mock
func (m *RequestHandler) makeMockedRoutes(router *mux.Router, mocks config.Mocks) {
var defaultMocks config.Mocks

for _, mockDef := range mocks {
if len(mockDef.Queries) > 0 || len(mockDef.Headers) > 0 || len(mockDef.Method) > 0 {
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 @@ -61,7 +61,7 @@ func TestUncorsRequestHandler(t *testing.T) {
{Dir: "/assets", Path: "/pnp/", Index: "index.php"},
{Dir: "/images", Path: "/img/"},
},
Mocks: []config.Mock{
Mocks: config.Mocks{
{
Path: "/api/mocks/1",
Response: config.Response{
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestMockMiddleware(t *testing.T) {
{
From: "*",
To: "*",
Mocks: []config.Mock{
Mocks: config.Mocks{
{
Path: "/api",
Response: config.Response{
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestMockMiddleware(t *testing.T) {
expectedCode := 299
expectedBody := "forwarded"
mappings := config.Mappings{
{From: "*", To: "*", Mocks: []config.Mock{{
{From: "*", To: "*", Mocks: config.Mocks{{
Path: "/api",
Method: http.MethodPut,
Response: config.Response{
Expand Down Expand Up @@ -416,7 +416,7 @@ func TestMockMiddleware(t *testing.T) {
expectedCode := 299
expectedBody := "forwarded"
mappings := config.Mappings{
{From: "*", To: "*", Mocks: []config.Mock{
{From: "*", To: "*", Mocks: config.Mocks{
{
Path: userPath,
Response: config.Response{
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestMockMiddleware(t *testing.T) {
handler.WithURLReplacerFactory(mocks.NewURLReplacerFactoryMock(t)),
handler.WithLogger(logger),
handler.WithMappings(config.Mappings{
{From: "*", To: "*", Mocks: []config.Mock{
{From: "*", To: "*", Mocks: config.Mocks{
{
Path: userPath,
Response: config.Response{
Expand Down Expand Up @@ -623,7 +623,7 @@ func TestMockMiddleware(t *testing.T) {
handler.WithURLReplacerFactory(mocks.NewURLReplacerFactoryMock(t)),
handler.WithLogger(logger),
handler.WithMappings(config.Mappings{
{From: "*", To: "*", Mocks: []config.Mock{
{From: "*", To: "*", Mocks: config.Mocks{
{
Path: userPath,
Response: config.Response{
Expand Down
52 changes: 40 additions & 12 deletions testing/testconstants/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,51 @@ import (
)

var (
Localhost = "localhost"
HTTPLocalhost = "http://localhost"
HTTPSLocalhost = "https://localhost"
Localhost = "localhost"
HTTPLocalhost = "http://localhost"
HTTPLocalhostWithPort = portFunction(HTTPLocalhost)
HTTPSLocalhost = "https://localhost"
HTTPSLocalhostWithPort = portFunction(HTTPSLocalhost)

Github = "github.com"
HTTPGithub = "http://github.com"
HTTPSGithub = "https://github.com"

Host1 = "host1"
HTTPHost1 = "http://host1"
HTTPSHost1 = "https://host1"
)
Stackoverflow = "stackoverflow.com"
HTTPStackoverflow = "http://stackoverflow.com"
HTTPSStackoverflow = "https://stackoverflow.com"

func HTTPLocalhostWithPort(port int) string {
return HTTPLocalhost + ":" + strconv.Itoa(port)
}
APIGithub = "api.github.com"
HTTPAPIGithub = "http://api.github.com"
HTTPSAPIGithub = "https://api.github.com"

Localhost1 = "localhost1"
HTTPLocalhost1 = "http://localhost1"
HTTPLocalhost1WithPort = portFunction(HTTPLocalhost1)
HTTPSLocalhost1 = "https://localhost1"
HTTPSLocalhost1WithPort = portFunction(HTTPSLocalhost1)

Localhost2 = "localhost2"
HTTPLocalhost2 = "http://localhost2"
HTTPLocalhost2WithPort = portFunction(HTTPLocalhost2)
HTTPSLocalhost2 = "https://localhost2"
HTTPSLocalhost2WithPort = portFunction(HTTPSLocalhost2)

Localhost3 = "localhost3"
HTTPLocalhost3 = "http://localhost3"
HTTPLocalhost3WithPort = portFunction(HTTPLocalhost3)
HTTPSLocalhost3 = "https://localhost3"
HTTPSLocalhost3WithPort = portFunction(HTTPSLocalhost3)

Localhost4 = "localhost4"
HTTPLocalhost4 = "http://localhost4"
HTTPLocalhost4WithPort = portFunction(HTTPLocalhost4)
HTTPSLocalhost4 = "https://localhost4"
HTTPSLocalhost4WithPort = portFunction(HTTPSLocalhost4)
)

func HTTPSLocalhostWithPort(port int) string {
return HTTPSLocalhost + ":" + strconv.Itoa(port)
func portFunction(host string) func(port int) string {
return func(port int) string {
return host + ":" + strconv.Itoa(port)
}
}

0 comments on commit 374daac

Please sign in to comment.