From 593570d1a9eba200da1812a3d851706b3618f169 Mon Sep 17 00:00:00 2001 From: Yuki Yugui Sonoda Date: Mon, 6 Jul 2020 22:59:59 +0900 Subject: [PATCH] Corrects an invalid test Fixes #1501. The expectations in the test was implemented wrong. But the wrong test input has been hiding the wrong expectation, as explained in #1501. --- runtime/marshaler_registry_test.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/runtime/marshaler_registry_test.go b/runtime/marshaler_registry_test.go index 194de6fee11..c36e6d2a8d6 100644 --- a/runtime/marshaler_registry_test.go +++ b/runtime/marshaler_registry_test.go @@ -2,6 +2,7 @@ package runtime_test import ( "errors" + "fmt" "io" "net/http" "testing" @@ -27,23 +28,27 @@ func TestMarshalerForRequest(t *testing.T) { t.Errorf("out = %#v; want a runtime.JSONPb", in) } - var marshalers [3]dummyMarshaler + marshalers := []dummyMarshaler{0, 1, 2} specs := []struct { opt runtime.ServeMuxOption wantIn runtime.Marshaler wantOut runtime.Marshaler }{ + // The option with wildcard overwrites the default configuration { opt: runtime.WithMarshalerOption(runtime.MIMEWildcard, &marshalers[0]), wantIn: &marshalers[0], wantOut: &marshalers[0], }, + // You can specify a marshaler for a specific MIME type. + // The output marshaler follows the input one unless specified. { opt: runtime.WithMarshalerOption("application/x-in", &marshalers[1]), wantIn: &marshalers[1], - wantOut: &marshalers[0], + wantOut: &marshalers[1], }, + // You can also separately specify an output marshaler { opt: runtime.WithMarshalerOption("application/x-out", &marshalers[2]), wantIn: &marshalers[1], @@ -67,8 +72,9 @@ func TestMarshalerForRequest(t *testing.T) { } r.Header.Set("Content-Type", "application/x-another") + r.Header.Set("Accept", "application/x-another") in, out = runtime.MarshalerForRequest(mux, r) - if got, want := in, &marshalers[1]; got != want { + if got, want := in, &marshalers[0]; got != want { t.Errorf("in = %#v; want %#v", got, want) } if got, want := out, &marshalers[0]; got != want { @@ -76,7 +82,7 @@ func TestMarshalerForRequest(t *testing.T) { } } -type dummyMarshaler struct{} +type dummyMarshaler int func (dummyMarshaler) ContentType() string { return "" } func (dummyMarshaler) Marshal(interface{}) ([]byte, error) { @@ -94,6 +100,10 @@ func (dummyMarshaler) NewEncoder(w io.Writer) runtime.Encoder { return dummyEncoder{} } +func (m dummyMarshaler) GoString() string { + return fmt.Sprintf("dummyMarshaler(%d)", m) +} + type dummyDecoder struct{} func (dummyDecoder) Decode(interface{}) error {