Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherrypick #1502 #1509

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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