From fdf6025abae159a3c644d1ee49d9c856ea78698a Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Tue, 6 Feb 2024 10:10:50 +0100 Subject: [PATCH] tests: urlencoded transform error --- deserialization_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/deserialization_test.go b/deserialization_test.go index fbc4bcb9..99b8256f 100644 --- a/deserialization_test.go +++ b/deserialization_test.go @@ -88,6 +88,17 @@ func (t *BodyTestWithInTransformer) InTransform(context.Context) error { var _ InTransformer = &BodyTestWithInTransformer{} +type BodyTestWithInTransformerError struct { + A string + B int +} + +func (t *BodyTestWithInTransformerError) InTransform(context.Context) error { + return errors.New("error happened!") +} + +var _ InTransformer = &BodyTestWithInTransformerError{} + func TestInTransform(t *testing.T) { t.Run("ReadJSON", func(t *testing.T) { input := strings.NewReader(`{"A":"a", "B":1}`) @@ -151,4 +162,14 @@ func TestReadURLEncoded(t *testing.T) { require.Error(t, err) require.Equal(t, BodyTest{"a", 0, true}, res) }) + + t.Run("read urlencoded with transform error", func(t *testing.T) { + input := strings.NewReader(`A=a&B=9`) + r := httptest.NewRequest("POST", "/", input) + r.Header.Add("Content-Type", "application/x-www-form-urlencoded") + res, err := ReadURLEncoded[BodyTestWithInTransformerError](r) + require.Error(t, err) + require.ErrorAs(t, err, &BadRequestError{}, "Expected a BadRequestError") + require.Equal(t, BodyTestWithInTransformerError{"a", 9}, res) + }) }