-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
405 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,177 @@ | ||
package validators_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/evg4b/uncors/internal/config" | ||
"github.com/evg4b/uncors/internal/config/validators" | ||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/gobuffalo/validate" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMappingValidator_IsValid(t *testing.T) { | ||
const field = "mapping" | ||
|
||
t.Run("should not register errors for", func(t *testing.T) { | ||
fs := testutils.FsFromMap(t, map[string]string{ | ||
"/tmp/demo.json": "{}", | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
value config.Mapping | ||
}{ | ||
{ | ||
name: "full filled mapping", | ||
value: config.Mapping{ | ||
From: "localhost", | ||
To: "github.com", | ||
Statics: []config.StaticDirectory{ | ||
{Path: "/", Dir: "/tmp"}, | ||
{Path: "/", Dir: "/tmp"}, | ||
}, | ||
Mocks: []config.Mock{ | ||
{ | ||
Path: "/api/info", | ||
Method: http.MethodGet, | ||
Response: config.Response{ | ||
Code: 200, | ||
Raw: "test", | ||
}, | ||
}, | ||
{ | ||
Path: "/api/info/demo", | ||
Method: http.MethodGet, | ||
Response: config.Response{ | ||
Code: 300, | ||
File: "/tmp/demo.json", | ||
}, | ||
}, | ||
}, | ||
Cache: config.CacheGlobs{ | ||
"/api/info", | ||
"/**", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "mapping without mocks and statics and caches", | ||
value: config.Mapping{ | ||
From: "localhost", | ||
To: "github.com", | ||
Statics: []config.StaticDirectory{}, | ||
Mocks: []config.Mock{}, | ||
Cache: config.CacheGlobs{}, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
errors := validate.Validate(&validators.MappingValidator{ | ||
Field: field, | ||
Value: test.value, | ||
Fs: fs, | ||
}) | ||
|
||
assert.False(t, errors.HasAny()) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("should register errors for", func(t *testing.T) { | ||
fs := testutils.FsFromMap(t, map[string]string{ | ||
"/tmp/demo.json": "{}", | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
value config.Mapping | ||
error string | ||
}{ | ||
{ | ||
name: "mapping without from", | ||
value: config.Mapping{ | ||
From: "", | ||
To: "github.com", | ||
Statics: []config.StaticDirectory{}, | ||
Mocks: []config.Mock{}, | ||
Cache: config.CacheGlobs{}, | ||
}, | ||
error: "mapping.from must not be empty", | ||
}, | ||
{ | ||
name: "mapping without to", | ||
value: config.Mapping{ | ||
From: "localhost", | ||
To: "", | ||
Statics: []config.StaticDirectory{}, | ||
Mocks: []config.Mock{}, | ||
Cache: config.CacheGlobs{}, | ||
}, | ||
error: "mapping.to must not be empty", | ||
}, | ||
{ | ||
name: "mapping with invalid statics", | ||
value: config.Mapping{ | ||
From: "localhost", | ||
To: "github.com", | ||
Statics: []config.StaticDirectory{ | ||
{Path: "/", Dir: "/tmp"}, | ||
{Path: "/", Dir: ""}, | ||
}, | ||
Mocks: []config.Mock{}, | ||
Cache: config.CacheGlobs{}, | ||
}, | ||
error: "mapping.statics[1].directory must not be empty", | ||
}, | ||
{ | ||
name: "mapping with invalid mocks", | ||
value: config.Mapping{ | ||
From: "localhost", | ||
To: "github.com", | ||
Statics: []config.StaticDirectory{}, | ||
Mocks: []config.Mock{ | ||
{ | ||
Path: "/api/info", | ||
Method: "invalid", | ||
Response: config.Response{ | ||
Code: 200, | ||
Raw: "test", | ||
}, | ||
}, | ||
}, | ||
Cache: config.CacheGlobs{}, | ||
}, | ||
error: "mapping.mocks[0].method must be one of GET, HEAD, POST, PUT, PATCH, DELETE, CONNECT, OPTIONS, TRACE", | ||
}, | ||
{ | ||
name: "mapping with invalid mocks", | ||
value: config.Mapping{ | ||
From: "localhost", | ||
To: "github.com", | ||
Statics: []config.StaticDirectory{}, | ||
Mocks: []config.Mock{}, | ||
Cache: config.CacheGlobs{ | ||
"/api/info[", | ||
}, | ||
}, | ||
error: "mapping.cache[0] is not a valid glob pattern", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
errors := validate.Validate(&validators.MappingValidator{ | ||
Field: field, | ||
Value: test.value, | ||
Fs: fs, | ||
}) | ||
|
||
require.EqualError(t, errors, test.error) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.