github.com/grpc-ecosystem/grpc-gateway/v2@v2.19.1/runtime/marshal_httpbodyproto_test.go (about)

     1  package runtime_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
     8  	"google.golang.org/genproto/googleapis/api/httpbody"
     9  	"google.golang.org/protobuf/encoding/protojson"
    10  )
    11  
    12  func TestHTTPBodyContentType(t *testing.T) {
    13  	m := runtime.HTTPBodyMarshaler{
    14  		&runtime.JSONPb{
    15  			MarshalOptions: protojson.MarshalOptions{
    16  				UseProtoNames: true,
    17  			},
    18  		},
    19  	}
    20  	expected := "CustomContentType"
    21  	message := &httpbody.HttpBody{
    22  		ContentType: expected,
    23  	}
    24  	res := m.ContentType(nil)
    25  	if res != "application/json" {
    26  		t.Errorf("content type not equal (%q, %q)", res, expected)
    27  	}
    28  	res = m.ContentType(message)
    29  	if res != expected {
    30  		t.Errorf("content type not equal (%q, %q)", res, expected)
    31  	}
    32  }
    33  
    34  func TestHTTPBodyMarshal(t *testing.T) {
    35  	m := runtime.HTTPBodyMarshaler{
    36  		&runtime.JSONPb{
    37  			MarshalOptions: protojson.MarshalOptions{
    38  				UseProtoNames: true,
    39  			},
    40  		},
    41  	}
    42  	expected := []byte("Some test")
    43  	message := &httpbody.HttpBody{
    44  		Data: expected,
    45  	}
    46  	res, err := m.Marshal(message)
    47  	if err != nil {
    48  		t.Errorf("m.Marshal(%#v) failed with %v; want success", message, err)
    49  	}
    50  	if !bytes.Equal(res, expected) {
    51  		t.Errorf("Marshalled data not equal (%q, %q)", res, expected)
    52  
    53  	}
    54  }