|
| 1 | +package decoder_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/abemedia/go-don/decoder" |
| 7 | + "github.com/abemedia/httprouter" |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "github.com/valyala/fasthttp" |
| 10 | +) |
| 11 | + |
| 12 | +func TestAdapters(t *testing.T) { |
| 13 | + t.Run("Args", func(t *testing.T) { |
| 14 | + in := &fasthttp.Args{} |
| 15 | + in.Add("string", "string") |
| 16 | + testAdapter(t, (*decoder.Args)(in)) |
| 17 | + }) |
| 18 | + |
| 19 | + t.Run("Header", func(t *testing.T) { |
| 20 | + in := &fasthttp.RequestHeader{} |
| 21 | + in.Add("string", "string") |
| 22 | + testAdapter(t, (*decoder.Header)(in)) |
| 23 | + }) |
| 24 | + |
| 25 | + t.Run("Params", func(t *testing.T) { |
| 26 | + in := httprouter.Params{{Key: "string", Value: "string"}} |
| 27 | + testAdapter(t, decoder.Params(in)) |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +func testAdapter(t *testing.T, in decoder.Getter) { |
| 32 | + t.Helper() |
| 33 | + |
| 34 | + type item struct { |
| 35 | + String string `field:"string"` |
| 36 | + Strings []string `field:"string"` |
| 37 | + } |
| 38 | + |
| 39 | + want := &item{ |
| 40 | + String: "string", |
| 41 | + Strings: []string{"string"}, |
| 42 | + } |
| 43 | + |
| 44 | + dec, err := decoder.NewCached(item{}, "field") |
| 45 | + if err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + got := &item{} |
| 50 | + if err = dec.Decode(in, got); err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + if diff := cmp.Diff(want, got); diff != "" { |
| 55 | + t.Errorf(diff) |
| 56 | + } |
| 57 | +} |
0 commit comments