diff --git a/internal/config/helpers_test.go b/internal/config/helpers_test.go index 2e86c48d..15ad6315 100644 --- a/internal/config/helpers_test.go +++ b/internal/config/helpers_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/evg4b/uncors/internal/config" + "github.com/evg4b/uncors/testing/mocks" "github.com/spf13/viper" "github.com/stretchr/testify/assert" ) @@ -17,9 +18,9 @@ func TestReadURLMapping(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, map[string]string{ - "host1": "target-host1", - "host2": "target-host2", - "host3": "target-host3", + mocks.SourceHost1: mocks.TargetHost1, + mocks.SourceHost2: mocks.TargetHost2, + mocks.SourceHost3: mocks.TargetHost3, }, actual) }) @@ -32,26 +33,26 @@ func TestReadURLMapping(t *testing.T) { }{ { name: "from is not set", - from: []string{"host1"}, + from: []string{mocks.SourceHost1}, to: []string{}, expectedErr: "`to` values are not set for every `from`", }, { name: "to is not set", from: []string{}, - to: []string{"target-host1"}, + to: []string{mocks.TargetHost1}, expectedErr: "`from` values are not set for every `to`", }, { name: "count of from values greath then count of to", - from: []string{"host1", "host2"}, - to: []string{"target-host1"}, + from: []string{mocks.SourceHost1, mocks.SourceHost2}, + to: []string{mocks.TargetHost1}, expectedErr: "`to` values are not set for every `from`", }, { name: "count of to values greath then count of from", - from: []string{"host1"}, - to: []string{"target-host1", "target-host2"}, + from: []string{mocks.SourceHost1}, + to: []string{mocks.TargetHost1, mocks.TargetHost2}, expectedErr: "`from` values are not set for every `to`", }, } diff --git a/internal/infrastructure/cors_test.go b/internal/infrastructure/cors_test.go index 6d3533b4..e2443045 100644 --- a/internal/infrastructure/cors_test.go +++ b/internal/infrastructure/cors_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/evg4b/uncors/internal/infrastructure" + "github.com/evg4b/uncors/testing/mocks" "github.com/evg4b/uncors/testing/testutils" "github.com/go-http-utils/headers" "github.com/stretchr/testify/assert" @@ -23,9 +24,7 @@ func TestWriteCorsHeaders(t *testing.T) { expected: map[string][]string{ headers.AccessControlAllowOrigin: {"*"}, headers.AccessControlAllowCredentials: {"true"}, - headers.AccessControlAllowMethods: { - "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS", - }, + headers.AccessControlAllowMethods: {mocks.AllMethods}, }, }, { @@ -42,27 +41,23 @@ func TestWriteCorsHeaders(t *testing.T) { "X-Hey-Header": {"123"}, headers.AccessControlAllowOrigin: {"*"}, headers.AccessControlAllowCredentials: {"true"}, - headers.AccessControlAllowMethods: { - "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS", - }, + headers.AccessControlAllowMethods: {mocks.AllMethods}, }, }, { name: "should override same headers", recorderFactory: func() *httptest.ResponseRecorder { writer := httptest.NewRecorder() - writer.Header().Set("Test-Header", "true") + writer.Header().Set("Custom-Header", "true") writer.Header().Set(headers.AccessControlAllowOrigin, "localhost:3000") return writer }, expected: map[string][]string{ - "Test-Header": {"true"}, + "Custom-Header": {"true"}, headers.AccessControlAllowOrigin: {"*"}, headers.AccessControlAllowCredentials: {"true"}, - headers.AccessControlAllowMethods: { - "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS", - }, + headers.AccessControlAllowMethods: {mocks.AllMethods}, }, }, } diff --git a/internal/mock/handler_test.go b/internal/mock/handler_test.go index e493f414..67e0f155 100644 --- a/internal/mock/handler_test.go +++ b/internal/mock/handler_test.go @@ -14,6 +14,8 @@ import ( const textPlain = "text/plain; charset=utf-8" +const testContent = "test content" + func TestHandler(t *testing.T) { logger := mocks.NewNoopLogger(t) @@ -76,30 +78,26 @@ func TestHandler(t *testing.T) { name: "should put default CORS headers", response: mock.Response{ Code: 200, - RawContent: "test content", + RawContent: testContent, }, expected: map[string][]string{ headers.AccessControlAllowOrigin: {"*"}, headers.AccessControlAllowCredentials: {"true"}, headers.ContentType: {textPlain}, - headers.AccessControlAllowMethods: { - "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS", - }, + headers.AccessControlAllowMethods: {mocks.AllMethods}, }, }, { name: "should set response code", response: mock.Response{ Code: 200, - RawContent: "test content", + RawContent: testContent, }, expected: map[string][]string{ headers.AccessControlAllowOrigin: {"*"}, headers.AccessControlAllowCredentials: {"true"}, headers.ContentType: {textPlain}, - headers.AccessControlAllowMethods: { - "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS", - }, + headers.AccessControlAllowMethods: {mocks.AllMethods}, }, }, { @@ -109,16 +107,14 @@ func TestHandler(t *testing.T) { Headers: map[string]string{ "X-Key": "X-Key-Value", }, - RawContent: "test content", + RawContent: testContent, }, expected: map[string][]string{ headers.AccessControlAllowOrigin: {"*"}, headers.AccessControlAllowCredentials: {"true"}, headers.ContentType: {textPlain}, "X-Key": {"X-Key-Value"}, - headers.AccessControlAllowMethods: { - "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS", - }, + headers.AccessControlAllowMethods: {mocks.AllMethods}, }, }, { @@ -130,15 +126,13 @@ func TestHandler(t *testing.T) { headers.AccessControlAllowCredentials: "false", headers.ContentType: "none", }, - RawContent: "test content", + RawContent: testContent, }, expected: map[string][]string{ headers.AccessControlAllowOrigin: {"localhost"}, headers.AccessControlAllowCredentials: {"false"}, headers.ContentType: {"none"}, - headers.AccessControlAllowMethods: { - "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS", - }, + headers.AccessControlAllowMethods: {mocks.AllMethods}, }, }, } diff --git a/internal/mock/routes_test.go b/internal/mock/routes_test.go index b0003238..2a80bdeb 100644 --- a/internal/mock/routes_test.go +++ b/internal/mock/routes_test.go @@ -18,6 +18,8 @@ var mock2Body = `{"mock": "mock number 2"}` var mock3Body = `{"mock": "mock number 3"}` var mock4Body = `{"mock": "mock number 4"}` +var notFoundBody = "404 page not found\n" + func TestMakeMockedRoutes(t *testing.T) { logger := mocks.NewNoopLogger(t) @@ -153,7 +155,7 @@ func TestMakeMockedRoutes(t *testing.T) { { name: "direct path with ending slash", url: "https://localhost/api/user/", - expected: "404 page not found\n", + expected: notFoundBody, statusCode: http.StatusNotFound, }, { @@ -165,13 +167,13 @@ func TestMakeMockedRoutes(t *testing.T) { { name: "direct path with incorrect parameter", url: "https://localhost/api/user/unknow", - expected: "404 page not found\n", + expected: notFoundBody, statusCode: http.StatusNotFound, }, { name: "path with subpath to single matching param", url: "https://localhost/api/some-path/with-some-subpath/demo", - expected: "404 page not found\n", + expected: notFoundBody, statusCode: http.StatusNotFound, }, { diff --git a/internal/proxy/proxy_test.go b/internal/proxy/proxy_test.go index 5a36df87..349db103 100644 --- a/internal/proxy/proxy_test.go +++ b/internal/proxy/proxy_test.go @@ -170,7 +170,7 @@ func TestProxyHandler(t *testing.T) { assert.Equal(t, "true", header.Get(headers.AccessControlAllowCredentials)) assert.Equal( t, - "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS", + mocks.AllMethods, header.Get(headers.AccessControlAllowMethods), ) }) diff --git a/testing/mocks/constants.go b/testing/mocks/constants.go new file mode 100644 index 00000000..548b1bcc --- /dev/null +++ b/testing/mocks/constants.go @@ -0,0 +1,11 @@ +package mocks + +var SourceHost1 = "host1" +var SourceHost2 = "host2" +var SourceHost3 = "host3" + +var TargetHost1 = "target-host1" +var TargetHost2 = "target-host2" +var TargetHost3 = "target-host3" + +const AllMethods = "GET, PUT, POST, HEAD, TRACE, DELETE, PATCH, COPY, HEAD, LINK, OPTIONS"