...

Source file src/github.com/grpc-ecosystem/grpc-gateway/runtime/marshaler_registry_test.go

Documentation: github.com/grpc-ecosystem/grpc-gateway/runtime

     1  package runtime_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"testing"
     9  
    10  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
    11  )
    12  
    13  func TestMarshalerForRequest(t *testing.T) {
    14  	r, err := http.NewRequest("GET", "http://example.com", nil)
    15  	if err != nil {
    16  		t.Fatalf(`http.NewRequest("GET", "http://example.com", nil) failed with %v; want success`, err)
    17  	}
    18  
    19  	mux := runtime.NewServeMux()
    20  
    21  	r.Header.Set("Accept", "application/x-out")
    22  	r.Header.Set("Content-Type", "application/x-in")
    23  	in, out := runtime.MarshalerForRequest(mux, r)
    24  	if _, ok := in.(*runtime.JSONPb); !ok {
    25  		t.Errorf("in = %#v; want a runtime.JSONPb", in)
    26  	}
    27  	if _, ok := out.(*runtime.JSONPb); !ok {
    28  		t.Errorf("out = %#v; want a runtime.JSONPb", in)
    29  	}
    30  
    31  	marshalers := []dummyMarshaler{0, 1, 2}
    32  	specs := []struct {
    33  		opt runtime.ServeMuxOption
    34  
    35  		wantIn  runtime.Marshaler
    36  		wantOut runtime.Marshaler
    37  	}{
    38  		// The option with wildcard overwrites the default configuration
    39  		{
    40  			opt:     runtime.WithMarshalerOption(runtime.MIMEWildcard, &marshalers[0]),
    41  			wantIn:  &marshalers[0],
    42  			wantOut: &marshalers[0],
    43  		},
    44  		// You can specify a marshaler for a specific MIME type.
    45  		// The output marshaler follows the input one unless specified.
    46  		{
    47  			opt:     runtime.WithMarshalerOption("application/x-in", &marshalers[1]),
    48  			wantIn:  &marshalers[1],
    49  			wantOut: &marshalers[1],
    50  		},
    51  		// You can also separately specify an output marshaler
    52  		{
    53  			opt:     runtime.WithMarshalerOption("application/x-out", &marshalers[2]),
    54  			wantIn:  &marshalers[1],
    55  			wantOut: &marshalers[2],
    56  		},
    57  	}
    58  	for i, spec := range specs {
    59  		var opts []runtime.ServeMuxOption
    60  		for _, s := range specs[:i+1] {
    61  			opts = append(opts, s.opt)
    62  		}
    63  		mux = runtime.NewServeMux(opts...)
    64  
    65  		in, out = runtime.MarshalerForRequest(mux, r)
    66  		if got, want := in, spec.wantIn; got != want {
    67  			t.Errorf("in = %#v; want %#v", got, want)
    68  		}
    69  		if got, want := out, spec.wantOut; got != want {
    70  			t.Errorf("out = %#v; want %#v", got, want)
    71  		}
    72  	}
    73  
    74  	r.Header.Set("Content-Type", "application/x-in; charset=UTF-8")
    75  	in, out = runtime.MarshalerForRequest(mux, r)
    76  	if got, want := in, &marshalers[1]; got != want {
    77  		t.Errorf("in = %#v; want %#v", got, want)
    78  	}
    79  	if got, want := out, &marshalers[2]; got != want {
    80  		t.Errorf("out = %#v; want %#v", got, want)
    81  	}
    82  
    83  	r.Header.Set("Content-Type", "application/x-another")
    84  	r.Header.Set("Accept", "application/x-another")
    85  	in, out = runtime.MarshalerForRequest(mux, r)
    86  	if got, want := in, &marshalers[0]; got != want {
    87  		t.Errorf("in = %#v; want %#v", got, want)
    88  	}
    89  	if got, want := out, &marshalers[0]; got != want {
    90  		t.Errorf("out = %#v; want %#v", got, want)
    91  	}
    92  }
    93  
    94  type dummyMarshaler int
    95  
    96  func (dummyMarshaler) ContentType() string { return "" }
    97  func (dummyMarshaler) Marshal(interface{}) ([]byte, error) {
    98  	return nil, errors.New("not implemented")
    99  }
   100  
   101  func (dummyMarshaler) Unmarshal([]byte, interface{}) error {
   102  	return errors.New("not implemented")
   103  }
   104  
   105  func (dummyMarshaler) NewDecoder(r io.Reader) runtime.Decoder {
   106  	return dummyDecoder{}
   107  }
   108  func (dummyMarshaler) NewEncoder(w io.Writer) runtime.Encoder {
   109  	return dummyEncoder{}
   110  }
   111  
   112  func (m dummyMarshaler) GoString() string {
   113  	return fmt.Sprintf("dummyMarshaler(%d)", m)
   114  }
   115  
   116  type dummyDecoder struct{}
   117  
   118  func (dummyDecoder) Decode(interface{}) error {
   119  	return errors.New("not implemented")
   120  }
   121  
   122  type dummyEncoder struct{}
   123  
   124  func (dummyEncoder) Encode(interface{}) error {
   125  	return errors.New("not implemented")
   126  }
   127  

View as plain text