...

Source file src/github.com/ory/fosite/errors_test.go

Documentation: github.com/ory/fosite

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    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