-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created configuration structure * Covered decoding by tests * Fixed configuration decoding issues * Created static directory mapping * Fixed cloning issue * Added tests for static serving * Added static dirs pringing * Changed handler structure * Added static middleware draft * Fixed index file serving * Added base tests for static middleware * Fixed mapping tests * Fixed middelware tesrs * Added tests fro normalise helper * Added tests for uncors handler * Added tests for index file * Added tests for mocks * Enabled skipped tests * Added fir for prefix cutting * Fixed error handling * Added tests for mocks handler registration * Fixed sonar issues * Updated readme
- Loading branch information
Evgeny Abramovich
committed
May 13, 2023
1 parent
201b14c
commit 4b7a562
Showing
43 changed files
with
1,860 additions
and
668 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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package configuration | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
type StaticDirMappings = []StaticDirMapping | ||
|
||
type StaticDirMapping struct { | ||
Path string `mapstructure:"path"` | ||
Dir string `mapstructure:"dir"` | ||
Index string `mapstructure:"index"` | ||
} | ||
|
||
func (s StaticDirMapping) Clone() StaticDirMapping { | ||
return StaticDirMapping{ | ||
Path: s.Path, | ||
Dir: s.Dir, | ||
Index: s.Index, | ||
} | ||
} | ||
|
||
var staticDirMappingsType = reflect.TypeOf(StaticDirMappings{}) | ||
|
||
func StaticDirMappingHookFunc() mapstructure.DecodeHookFunc { //nolint: ireturn | ||
return func(f reflect.Type, t reflect.Type, rawData any) (any, error) { | ||
if t != staticDirMappingsType || f.Kind() != reflect.Map { | ||
return rawData, nil | ||
} | ||
|
||
mappingsDefs, ok := rawData.(map[string]any) | ||
if !ok { | ||
return rawData, nil | ||
} | ||
|
||
var mappings StaticDirMappings | ||
for path, mappingDef := range mappingsDefs { | ||
if def, ok := mappingDef.(string); ok { | ||
mappings = append(mappings, StaticDirMapping{ | ||
Path: path, | ||
Dir: def, | ||
}) | ||
|
||
continue | ||
} | ||
|
||
mapping := StaticDirMapping{} | ||
err := decodeConfig(mappingDef, &mapping) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mapping.Path = path | ||
mappings = append(mappings, mapping) | ||
} | ||
|
||
return mappings, nil | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package configuration_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/evg4b/uncors/internal/configuration" | ||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
anotherStaticDir = "/another-static-dir" | ||
anotherPath = "/another-path" | ||
path = "/path" | ||
staticDir = "/static-dir" | ||
) | ||
|
||
func TestStaticDirMappingHookFunc(t *testing.T) { | ||
const configFile = "config.yaml" | ||
type testType struct { | ||
Statics configuration.StaticDirMappings `mapstructure:"statics"` | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
config string | ||
expected configuration.StaticDirMappings | ||
}{ | ||
{ | ||
name: "decode plan mapping", | ||
config: ` | ||
statics: | ||
/path: /static-dir | ||
/another-path: /another-static-dir | ||
`, | ||
expected: configuration.StaticDirMappings{ | ||
{Path: anotherPath, Dir: anotherStaticDir}, | ||
{Path: path, Dir: staticDir}, | ||
}, | ||
}, | ||
{ | ||
name: "decode object mappings", | ||
config: ` | ||
statics: | ||
/path: { dir: /static-dir } | ||
/another-path: { dir: /another-static-dir } | ||
`, | ||
expected: configuration.StaticDirMappings{ | ||
{Path: path, Dir: staticDir}, | ||
{Path: anotherPath, Dir: anotherStaticDir}, | ||
}, | ||
}, | ||
{ | ||
name: "decode object mappings with index", | ||
config: ` | ||
statics: | ||
/path: { dir: /static-dir, index: index.html } | ||
/another-path: { dir: /another-static-dir, index: default.html } | ||
`, | ||
expected: configuration.StaticDirMappings{ | ||
{Path: path, Dir: staticDir, Index: "index.html"}, | ||
{Path: anotherPath, Dir: anotherStaticDir, Index: "default.html"}, | ||
}, | ||
}, | ||
{ | ||
name: "decode mixed mappings with index", | ||
config: ` | ||
statics: | ||
/path: { dir: /static-dir, index: index.html } | ||
/another-path: /another-static-dir | ||
`, | ||
expected: configuration.StaticDirMappings{ | ||
{Path: path, Dir: staticDir, Index: "index.html"}, | ||
{Path: anotherPath, Dir: anotherStaticDir}, | ||
}, | ||
}, | ||
} | ||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
viperInstance := viper.GetViper() | ||
viperInstance.SetFs(testutils.FsFromMap(t, map[string]string{ | ||
configFile: testCase.config, | ||
})) | ||
viperInstance.SetConfigFile(configFile) | ||
err := viperInstance.ReadInConfig() | ||
testutils.CheckNoError(t, err) | ||
|
||
actual := testType{} | ||
|
||
err = viperInstance.Unmarshal(&actual, viper.DecodeHook( | ||
configuration.StaticDirMappingHookFunc(), | ||
)) | ||
testutils.CheckNoError(t, err) | ||
|
||
assert.ElementsMatch(t, actual.Statics, testCase.expected) | ||
}) | ||
} | ||
} | ||
|
||
func TestStaticDirMappingClone(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
expected configuration.StaticDirMapping | ||
}{ | ||
{ | ||
name: "empty structure", | ||
expected: configuration.StaticDirMapping{}, | ||
}, | ||
{ | ||
name: "structure with 1 field", | ||
expected: configuration.StaticDirMapping{ | ||
Dir: "dir", | ||
}, | ||
}, | ||
{ | ||
name: "structure with 2 field", | ||
expected: configuration.StaticDirMapping{ | ||
Dir: "dir", | ||
Path: "/some-path", | ||
}, | ||
}, | ||
{ | ||
name: "structure with all field", | ||
expected: configuration.StaticDirMapping{ | ||
Dir: "dir", | ||
Path: "/one-more-path", | ||
Index: "index.html", | ||
}, | ||
}, | ||
} | ||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
actual := testCase.expected.Clone() | ||
|
||
assert.NotSame(t, testCase.expected, actual) | ||
assert.Equal(t, testCase.expected, actual) | ||
}) | ||
} | ||
} |
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.