From 374daacc8914637819d0c5fb0957f614b5cc33b5 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 3 Jun 2023 16:49:25 -0300 Subject: [PATCH] Cleanup code: WIP 4 --- internal/config/config_test.go | 56 +++++++++++++------------ internal/config/helpers_test.go | 52 ++++++++++++----------- internal/config/mapping_test.go | 8 ++-- internal/config/mappings_test.go | 2 +- internal/handler/mocked_routes.go | 4 +- internal/handler/uncors_handler_test.go | 12 +++--- testing/testconstants/hosts.go | 52 +++++++++++++++++------ 7 files changed, 111 insertions(+), 75 deletions(-) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 984812a9..ec2c3cbf 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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 @@ -54,7 +54,7 @@ const ( incorrectConfigPath = "/incorrect-config.yaml" incorrectConfig = `http-port: xxx mappings: - - http://demo: https://demo.com + - http://localhost: https://github.com ` ) @@ -63,7 +63,7 @@ const ( minimalConfig = ` http-port: 8080 mappings: - - http://demo: https://demo.com + - http://localhost: https://github.com ` ) @@ -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}, }, }, }, @@ -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, @@ -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", diff --git a/internal/config/helpers_test.go b/internal/config/helpers_test.go index 9818aab4..75c087f5 100644 --- a/internal/config/helpers_test.go +++ b/internal/config/helpers_test.go @@ -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 @@ -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, }, @@ -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, }, @@ -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", }, @@ -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", }, diff --git a/internal/config/mapping_test.go b/internal/config/mapping_test.go index ee1bebc5..d897654e 100644 --- a/internal/config/mapping_test.go +++ b/internal/config/mapping_test.go @@ -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, }, }, } diff --git a/internal/config/mappings_test.go b/internal/config/mappings_test.go index f7f727fe..af77203b 100644 --- a/internal/config/mappings_test.go +++ b/internal/config/mappings_test.go @@ -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, diff --git a/internal/handler/mocked_routes.go b/internal/handler/mocked_routes.go index 74ff0c9b..4e5c4b39 100644 --- a/internal/handler/mocked_routes.go +++ b/internal/handler/mocked_routes.go @@ -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 { diff --git a/internal/handler/uncors_handler_test.go b/internal/handler/uncors_handler_test.go index 84a3ce05..96a9da70 100644 --- a/internal/handler/uncors_handler_test.go +++ b/internal/handler/uncors_handler_test.go @@ -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{ @@ -299,7 +299,7 @@ func TestMockMiddleware(t *testing.T) { { From: "*", To: "*", - Mocks: []config.Mock{ + Mocks: config.Mocks{ { Path: "/api", Response: config.Response{ @@ -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{ @@ -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{ @@ -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{ @@ -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{ diff --git a/testing/testconstants/hosts.go b/testing/testconstants/hosts.go index 93ba3bcb..acb1fea7 100644 --- a/testing/testconstants/hosts.go +++ b/testing/testconstants/hosts.go @@ -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) + } }