Skip to content

Commit

Permalink
tests: urlencoded transform error
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Feb 6, 2024
1 parent dfcadf3 commit fdf6025
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions deserialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down Expand Up @@ -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)
})
}

0 comments on commit fdf6025

Please sign in to comment.