Debian Codegraph @

api/v1/lib/httpcli/http_test.go

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