1
21
22 package fosite
23
24 import (
25 "testing"
26
27 "github.com/ory/fosite/i18n"
28 "github.com/pkg/errors"
29 "github.com/stretchr/testify/assert"
30 "golang.org/x/text/language"
31 )
32
33 func TestRFC6749Error(t *testing.T) {
34 t.Run("case=wrap", func(t *testing.T) {
35 orig := errors.New("hi")
36 wrap := new(RFC6749Error)
37 wrap.Wrap(orig)
38
39 assert.EqualValues(t, orig.(stackTracer).StackTrace(), wrap.StackTrace())
40 })
41
42 t.Run("case=wrap_self", func(t *testing.T) {
43 wrap := new(RFC6749Error)
44 wrap.Wrap(wrap)
45
46 assert.Empty(t, wrap.StackTrace())
47 })
48 }
49
50 func TestErrorI18N(t *testing.T) {
51 catalog := i18n.NewDefaultMessageCatalog([]*i18n.DefaultLocaleBundle{
52 {
53 LangTag: "en",
54 Messages: []*i18n.DefaultMessage{
55 {
56 ID: "access_denied",
57 FormattedMessage: "The resource owner or authorization server denied the request.",
58 },
59 {
60 ID: "badRequestMethod",
61 FormattedMessage: "HTTP method is '%s', expected 'POST'.",
62 },
63 },
64 },
65 {
66 LangTag: "es",
67 Messages: []*i18n.DefaultMessage{
68 {
69 ID: "access_denied",
70 FormattedMessage: "El propietario del recurso o el servidor de autorización denegó la solicitud.",
71 },
72 {
73 ID: "HTTP method is '%s', expected 'POST'.",
74 FormattedMessage: "El método HTTP es '%s', esperado 'POST'.",
75 },
76 {
77 ID: "Unable to parse HTTP body, make sure to send a properly formatted form request body.",
78 FormattedMessage: "No se puede analizar el cuerpo HTTP, asegúrese de enviar un cuerpo de solicitud de formulario con el formato adecuado.",
79 },
80 {
81 ID: "badRequestMethod",
82 FormattedMessage: "El método HTTP es '%s', esperado 'POST'.",
83 },
84 },
85 },
86 })
87
88 t.Run("case=legacy", func(t *testing.T) {
89 err := ErrAccessDenied.WithLocalizer(catalog, language.Spanish).WithHintf("HTTP method is '%s', expected 'POST'.", "GET")
90 assert.EqualValues(t, "El propietario del recurso o el servidor de autorización denegó la solicitud. El método HTTP es 'GET', esperado 'POST'.", err.GetDescription())
91 })
92
93 t.Run("case=unsupported_locale_legacy", func(t *testing.T) {
94 err := ErrAccessDenied.WithLocalizer(catalog, language.Afrikaans).WithHintf("HTTP method is '%s', expected 'POST'.", "GET")
95 assert.EqualValues(t, "The resource owner or authorization server denied the request. HTTP method is 'GET', expected 'POST'.", err.GetDescription())
96 })
97
98 t.Run("case=simple", func(t *testing.T) {
99 err := ErrAccessDenied.WithLocalizer(catalog, language.Spanish).WithHintIDOrDefaultf("badRequestMethod", "HTTP method is '%s', expected 'POST'.", "GET")
100 assert.EqualValues(t, "El propietario del recurso o el servidor de autorización denegó la solicitud. El método HTTP es 'GET', esperado 'POST'.", err.GetDescription())
101 })
102
103 t.Run("case=unsupported_locale", func(t *testing.T) {
104 err := ErrAccessDenied.WithLocalizer(catalog, language.Afrikaans).WithHintIDOrDefaultf("badRequestMethod", "HTTP method is '%s', expected 'POST'.", "GET")
105 assert.EqualValues(t, "The resource owner or authorization server denied the request. HTTP method is 'GET', expected 'POST'.", err.GetDescription())
106 })
107 }
108
View as plain text