2021-09-29 15:38:50 +00:00
|
|
|
package config_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-23 17:49:45 +00:00
|
|
|
|
|
|
|
"gh.tarampamp.am/error-pages/internal/config"
|
2021-09-29 15:38:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFromYaml(t *testing.T) {
|
2022-01-27 12:29:49 +00:00
|
|
|
var cases = map[string]struct { //nolint:maligned
|
2021-09-29 15:38:50 +00:00
|
|
|
giveYaml []byte
|
|
|
|
giveEnv map[string]string
|
|
|
|
wantErr bool
|
|
|
|
checkResultFn func(*testing.T, *config.Config)
|
|
|
|
}{
|
2022-01-27 12:29:49 +00:00
|
|
|
"with all possible values": {
|
2021-09-29 15:38:50 +00:00
|
|
|
giveEnv: map[string]string{
|
2022-01-27 12:29:49 +00:00
|
|
|
"__FOO_TPL_PATH": "./testdata/foo-tpl.html",
|
|
|
|
"__FOO_TPL_NAME": "Foo Template",
|
2021-09-29 15:38:50 +00:00
|
|
|
},
|
|
|
|
giveYaml: []byte(`
|
|
|
|
templates:
|
2022-01-27 12:29:49 +00:00
|
|
|
- path: ${__FOO_TPL_PATH}
|
|
|
|
name: ${__FOO_TPL_NAME:-default_value} # name is optional
|
|
|
|
- path: ./testdata/bar-tpl.html
|
|
|
|
- name: Baz
|
2021-09-29 15:38:50 +00:00
|
|
|
content: |
|
2022-01-27 12:29:49 +00:00
|
|
|
Some content {{ code }}
|
2021-09-29 15:38:50 +00:00
|
|
|
New line
|
|
|
|
|
2022-01-27 12:29:49 +00:00
|
|
|
formats:
|
|
|
|
json:
|
|
|
|
content: |
|
|
|
|
{"code": "{{code}}"}
|
|
|
|
Avada_Kedavra:
|
|
|
|
content: "{{ message }}"
|
|
|
|
|
2021-09-29 15:38:50 +00:00
|
|
|
pages:
|
|
|
|
400:
|
|
|
|
message: Bad Request
|
|
|
|
description: The server did not understand the request
|
|
|
|
|
|
|
|
401:
|
|
|
|
message: Unauthorized
|
|
|
|
description: The requested page needs a username and a password
|
|
|
|
`),
|
|
|
|
wantErr: false,
|
|
|
|
checkResultFn: func(t *testing.T, cfg *config.Config) {
|
|
|
|
assert.Len(t, cfg.Templates, 3)
|
2022-01-27 12:29:49 +00:00
|
|
|
|
|
|
|
tpl, found := cfg.Template("Foo Template")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "Foo Template", tpl.Name())
|
|
|
|
assert.Equal(t, "<html><body>foo {{ code }}</body></html>\n", string(tpl.Content()))
|
|
|
|
|
|
|
|
tpl, found = cfg.Template("bar-tpl")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "bar-tpl", tpl.Name())
|
|
|
|
assert.Equal(t, "<html><body>bar {{ code }}</body></html>\n", string(tpl.Content()))
|
|
|
|
|
|
|
|
tpl, found = cfg.Template("Baz")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "Baz", tpl.Name())
|
|
|
|
assert.Equal(t, "Some content {{ code }}\nNew line\n", string(tpl.Content()))
|
|
|
|
|
|
|
|
tpl, found = cfg.Template("NonExists")
|
|
|
|
assert.False(t, found)
|
|
|
|
assert.Equal(t, "", tpl.Name())
|
|
|
|
assert.Equal(t, "", string(tpl.Content()))
|
|
|
|
|
|
|
|
assert.Len(t, cfg.Formats, 2)
|
|
|
|
|
|
|
|
format, found := cfg.Formats["json"]
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, `{"code": "{{code}}"}`, string(format.Content()))
|
|
|
|
|
|
|
|
format, found = cfg.Formats["Avada_Kedavra"]
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "{{ message }}", string(format.Content()))
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
assert.Len(t, cfg.Pages, 2)
|
2022-01-27 12:29:49 +00:00
|
|
|
|
|
|
|
errPage, found := cfg.Pages["400"]
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "400", errPage.Code())
|
|
|
|
assert.Equal(t, "Bad Request", errPage.Message())
|
|
|
|
assert.Equal(t, "The server did not understand the request", errPage.Description())
|
|
|
|
|
|
|
|
errPage, found = cfg.Pages["401"]
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "401", errPage.Code())
|
|
|
|
assert.Equal(t, "Unauthorized", errPage.Message())
|
|
|
|
assert.Equal(t, "The requested page needs a username and a password", errPage.Description())
|
|
|
|
|
|
|
|
errPage, found = cfg.Pages["666"]
|
|
|
|
assert.False(t, found)
|
|
|
|
assert.Equal(t, "", errPage.Message())
|
|
|
|
assert.Equal(t, "", errPage.Code())
|
|
|
|
assert.Equal(t, "", errPage.Description())
|
2021-09-29 15:38:50 +00:00
|
|
|
},
|
|
|
|
},
|
2022-01-27 12:29:49 +00:00
|
|
|
"broken yaml": {
|
2021-09-29 15:38:50 +00:00
|
|
|
giveYaml: []byte(`foo bar`),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-27 12:29:49 +00:00
|
|
|
for name, tt := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2021-09-29 15:38:50 +00:00
|
|
|
if tt.giveEnv != nil {
|
|
|
|
for key, value := range tt.giveEnv {
|
|
|
|
assert.NoError(t, os.Setenv(key, value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conf, err := config.FromYaml(tt.giveYaml)
|
|
|
|
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, err)
|
|
|
|
tt.checkResultFn(t, conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.giveEnv != nil {
|
|
|
|
for key := range tt.giveEnv {
|
|
|
|
assert.NoError(t, os.Unsetenv(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromYamlFile(t *testing.T) {
|
2022-01-27 12:29:49 +00:00
|
|
|
var cases = map[string]struct { //nolint:maligned
|
2021-09-29 15:38:50 +00:00
|
|
|
giveYamlFilePath string
|
|
|
|
wantErr bool
|
|
|
|
checkResultFn func(*testing.T, *config.Config)
|
|
|
|
}{
|
2022-01-27 12:29:49 +00:00
|
|
|
"with all possible values": {
|
2021-09-29 15:38:50 +00:00
|
|
|
giveYamlFilePath: "./testdata/simple.yml",
|
|
|
|
wantErr: false,
|
|
|
|
checkResultFn: func(t *testing.T, cfg *config.Config) {
|
|
|
|
assert.Len(t, cfg.Templates, 2)
|
2022-01-27 12:29:49 +00:00
|
|
|
|
|
|
|
tpl, found := cfg.Template("ghost")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "ghost", tpl.Name())
|
|
|
|
assert.Equal(t, "<html><body>foo {{ code }}</body></html>\n", string(tpl.Content()))
|
|
|
|
|
|
|
|
tpl, found = cfg.Template("bar-tpl")
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "bar-tpl", tpl.Name())
|
|
|
|
assert.Equal(t, "<html><body>bar {{ code }}</body></html>\n", string(tpl.Content()))
|
2021-09-29 15:38:50 +00:00
|
|
|
|
|
|
|
assert.Len(t, cfg.Pages, 2)
|
2022-01-27 12:29:49 +00:00
|
|
|
|
|
|
|
errPage, found := cfg.Pages["400"]
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "400", errPage.Code())
|
|
|
|
assert.Equal(t, "Bad Request", errPage.Message())
|
|
|
|
assert.Equal(t, "The server did not understand the request", errPage.Description())
|
|
|
|
|
|
|
|
errPage, found = cfg.Pages["401"]
|
|
|
|
assert.True(t, found)
|
|
|
|
assert.Equal(t, "401", errPage.Code())
|
|
|
|
assert.Equal(t, "Unauthorized", errPage.Message())
|
|
|
|
assert.Equal(t, "The requested page needs a username and a password", errPage.Description())
|
2021-09-29 15:38:50 +00:00
|
|
|
},
|
|
|
|
},
|
2022-01-27 12:29:49 +00:00
|
|
|
"broken yaml": {
|
2021-09-29 15:38:50 +00:00
|
|
|
giveYamlFilePath: "./testdata/broken.yml",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-01-27 12:29:49 +00:00
|
|
|
"wrong file path": {
|
2021-09-29 15:38:50 +00:00
|
|
|
giveYamlFilePath: "foo bar",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-27 12:29:49 +00:00
|
|
|
for name, tt := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2021-09-29 15:38:50 +00:00
|
|
|
conf, err := config.FromYamlFile(tt.giveYamlFilePath)
|
|
|
|
|
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, err)
|
|
|
|
tt.checkResultFn(t, conf)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|