Skip to content

Commit

Permalink
Fixed sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Nov 19, 2022
1 parent 6044382 commit 8a156c1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 40 deletions.
19 changes: 10 additions & 9 deletions internal/config/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
})

Expand All @@ -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`",
},
}
Expand Down
17 changes: 6 additions & 11 deletions internal/infrastructure/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
}
Expand Down
26 changes: 10 additions & 16 deletions internal/mock/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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},
},
},
{
Expand All @@ -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},
},
},
{
Expand All @@ -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},
},
},
}
Expand Down
8 changes: 5 additions & 3 deletions internal/mock/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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,
},
{
Expand All @@ -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,
},
{
Expand Down
2 changes: 1 addition & 1 deletion internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
})
Expand Down
11 changes: 11 additions & 0 deletions testing/mocks/constants.go
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 8a156c1

Please sign in to comment.