2021-09-29 15:38:50 +00:00
|
|
|
package config_test
|
|
|
|
|
|
|
|
import (
|
2024-07-03 14:12:13 +00:00
|
|
|
"net/http"
|
2021-09-29 15:38:50 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-23 17:49:45 +00:00
|
|
|
|
|
|
|
"gh.tarampamp.am/error-pages/internal/config"
|
2024-07-03 14:12:13 +00:00
|
|
|
"gh.tarampamp.am/error-pages/internal/template"
|
2021-09-29 15:38:50 +00:00
|
|
|
)
|
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
func TestNew(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
t.Run("default config", func(t *testing.T) {
|
|
|
|
var cfg = config.New()
|
2022-01-27 12:29:49 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
assert.NotEmpty(t, cfg.Formats.XML)
|
|
|
|
assert.NotEmpty(t, cfg.Formats.JSON)
|
|
|
|
assert.NotEmpty(t, cfg.Formats.PlainText)
|
|
|
|
assert.True(t, len(cfg.Codes) >= 19)
|
|
|
|
assert.True(t, len(cfg.Templates) >= 1)
|
|
|
|
assert.NotEmpty(t, cfg.TemplateName)
|
|
|
|
assert.True(t, cfg.Templates.Has(cfg.TemplateName))
|
|
|
|
assert.Equal(t, uint16(http.StatusNotFound), cfg.DefaultCodeToRender)
|
2024-07-05 14:59:07 +00:00
|
|
|
assert.False(t, cfg.DisableMinification)
|
2024-07-03 14:12:13 +00:00
|
|
|
})
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
t.Run("changing cfg1 should not affect cfg2", func(t *testing.T) {
|
|
|
|
var cfg1, cfg2 = config.New(), config.New()
|
2022-01-27 12:29:49 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
cfg1.Codes["400"] = config.CodeDescription{Message: "foo", Description: "bar"}
|
2022-01-27 12:29:49 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
assert.NotEqual(t, cfg1.Codes["400"], cfg2.Codes["400"])
|
2022-01-27 12:29:49 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
cfg1.ProxyHeaders = append(cfg1.ProxyHeaders, "foo")
|
2022-01-27 12:29:49 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
assert.NotEqual(t, cfg1.ProxyHeaders, cfg2.ProxyHeaders)
|
|
|
|
})
|
2022-01-27 12:29:49 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
t.Run("render default format templates", func(t *testing.T) {
|
|
|
|
var cfg = config.New()
|
2022-01-27 12:29:49 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
for _, content := range []string{cfg.Formats.JSON, cfg.Formats.XML, cfg.Formats.PlainText} {
|
|
|
|
var result, err = template.Render(content, template.Props{
|
|
|
|
ShowRequestDetails: true,
|
|
|
|
Code: 404,
|
|
|
|
Message: "Not Found",
|
|
|
|
})
|
2022-01-27 12:29:49 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
assert.NotEmpty(t, result)
|
|
|
|
assert.NoError(t, err)
|
2021-09-29 15:38:50 +00:00
|
|
|
|
2024-07-03 14:12:13 +00:00
|
|
|
t.Log(result)
|
|
|
|
}
|
|
|
|
})
|
2021-09-29 15:38:50 +00:00
|
|
|
}
|