1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
package httpcli
import (
"net/http"
"testing"
"github.com/mesos/mesos-go/api/v1/lib/client"
"github.com/mesos/mesos-go/api/v1/lib/encoding"
)
func TestPrepareForResponse(t *testing.T) {
codec := encoding.Codec{
Type: encoding.MediaType("foo"),
}
for ti, tc := range []struct {
rc client.ResponseClass
wantsHeaders []string
wantsErr bool
}{
{client.ResponseClass(-1), nil, true},
{client.ResponseClassNoData, []string{"Accept", "foo"}, false},
{client.ResponseClassSingleton, []string{"Accept", "foo"}, false},
{client.ResponseClassAuto, []string{"Accept", "foo"}, false},
{client.ResponseClassStreaming, []string{"Accept", "application/recordio", "Message-Accept", "foo"}, false},
} {
opts, err := prepareForResponse(tc.rc, codec)
if (err != nil) != tc.wantsErr {
if tc.wantsErr {
t.Fatalf("test case %d failed: expected error", ti)
} else {
t.Fatalf("test case %d failed: unexpected error %v", ti, err)
}
} else {
req := http.Request{Header: http.Header(make(map[string][]string))}
opts.Apply(&req)
if wants := len(tc.wantsHeaders) / 2; wants != len(req.Header) {
t.Fatalf("test case %d failed: header count mismatch, expected %d instead of %d", ti, wants, len(req.Header))
}
for i := 0; i < len(tc.wantsHeaders); i++ {
name := tc.wantsHeaders[i]
v := req.Header.Get(name)
i++
if wants := tc.wantsHeaders[i]; v != wants {
t.Fatalf("test case %d failed: unexpected value %q for header %q, wanted %q instead", ti, v, name, wants)
}
}
}
}
}
func TestNewSourceFactory(t *testing.T) {
for ti, tc := range []struct {
rc client.ResponseClass
wantsNil bool
wantsPanic bool
}{
{client.ResponseClass(-1), false, true},
{client.ResponseClassNoData, true, false},
{client.ResponseClassAuto, false, false},
{client.ResponseClassSingleton, false, false},
{client.ResponseClassStreaming, false, false},
} {
f := func() encoding.SourceFactoryFunc {
defer func() {
if x := recover(); (x != nil) != tc.wantsPanic {
panic(x)
}
}()
return newSourceFactory(tc.rc)
}()
if tc.wantsPanic {
continue
}
if (f == nil) != tc.wantsNil {
if tc.wantsNil {
t.Fatalf("test case %d failed: expected nil factory func instead of %v", ti, f)
} else {
t.Fatalf("test case %d failed: expected non-nil factory func", ti)
}
}
}
}
|