Skip to content

Commit

Permalink
Corrects an invalid test (#1502)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yugui committed Jul 7, 2020
1 parent b3909e3 commit 0022d96
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions runtime/marshaler_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runtime_test

import (
"errors"
"fmt"
"io"
"net/http"
"testing"
Expand All @@ -27,23 +28,27 @@ func TestMarshalerForRequest(t *testing.T) {
t.Errorf("out = %#v; want a runtime.HTTPBodyMarshaler", 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],
Expand All @@ -67,16 +72,17 @@ 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 {
t.Errorf("out = %#v; want %#v", got, want)
}
}

type dummyMarshaler struct{}
type dummyMarshaler int

func (dummyMarshaler) ContentType(_ interface{}) string { return "" }
func (dummyMarshaler) Marshal(interface{}) ([]byte, error) {
Expand All @@ -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 {
Expand Down

0 comments on commit 0022d96

Please sign in to comment.