github.com/abemedia/go-don@v0.2.2-0.20240329015135-be88e32bb73b/encoding/protobuf/protobuf_test.go (about) 1 package protobuf_test 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/abemedia/go-don" 8 "github.com/abemedia/go-don/encoding" 9 "github.com/abemedia/go-don/encoding/protobuf/testdata" 10 "github.com/abemedia/go-don/internal/test" 11 "github.com/abemedia/go-don/pkg/httptest" 12 ) 13 14 //go:generate protoc testdata/test.proto --go_out=. --go_opt=paths=source_relative 15 16 var opt = test.EncodingOptions[*testdata.Item]{ 17 Mime: "application/protobuf", 18 Raw: "\n\x03bar", 19 Parsed: &testdata.Item{Foo: "bar"}, 20 } 21 22 func TestProtobuf(t *testing.T) { 23 test.Encoding(t, opt) 24 } 25 26 func TestProtobufError(t *testing.T) { 27 ctx := httptest.NewRequest("", "", "", nil) 28 v := "test" 29 30 t.Run("Decode", func(t *testing.T) { 31 dec := encoding.GetDecoder("application/protobuf") 32 err := dec(ctx, &v) 33 if !errors.Is(err, don.ErrUnsupportedMediaType) { 34 t.Fatal("should fail") 35 } 36 }) 37 38 t.Run("Encode", func(t *testing.T) { 39 enc := encoding.GetEncoder("application/protobuf") 40 err := enc(ctx, &v) 41 if !errors.Is(err, don.ErrNotAcceptable) { 42 t.Fatal("should fail") 43 } 44 }) 45 } 46 47 func BenchmarkProtobuf(b *testing.B) { 48 test.BenchmarkEncoding(b, opt) 49 }