13 type stubTransport struct {
14 Responses map[string]string
15 Requests []http.Request
19 func (stub *stubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
21 stub.Requests = append(stub.Requests, *req)
24 resp := &http.Response{
32 str := stub.Responses[req.URL.Path]
34 resp.Status = "404 Not Found"
38 buf := bytes.NewBufferString(str)
39 resp.Body = ioutil.NopCloser(buf)
40 resp.ContentLength = int64(buf.Len())
44 type errorTransport struct{}
46 func (stub *errorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
47 return nil, fmt.Errorf("something awful happened")
50 func TestCurrentUser(t *testing.T) {
52 stub := &stubTransport{
53 Responses: map[string]string{
54 "/arvados/v1/users/current": `{"uuid":"zzzzz-abcde-012340123401234"}`,
61 APIHost: "zzzzz.arvadosapi.com",
64 u, err := c.CurrentUser()
68 if x := "zzzzz-abcde-012340123401234"; u.UUID != x {
69 t.Errorf("got uuid %q, expected %q", u.UUID, x)
71 if len(stub.Requests) < 1 {
72 t.Fatal("empty stub.Requests")
74 hdr := stub.Requests[len(stub.Requests)-1].Header
75 if hdr.Get("Authorization") != "OAuth2 xyzzy" {
76 t.Errorf("got headers %+q, expected Authorization header", hdr)
79 c.Client.Transport = &errorTransport{}
80 u, err = c.CurrentUser()
82 t.Errorf("got nil error, expected something awful")
86 func TestAnythingToValues(t *testing.T) {
87 type testCase struct {
89 // ok==nil means anythingToValues should return an
90 // error, otherwise it's a func that returns true if
92 ok func(out url.Values) bool
94 for _, tc := range []testCase{
96 in: map[string]interface{}{"foo": "bar"},
97 ok: func(out url.Values) bool {
98 return out.Get("foo") == "bar"
102 in: map[string]interface{}{"foo": 2147483647},
103 ok: func(out url.Values) bool {
104 return out.Get("foo") == "2147483647"
108 in: map[string]interface{}{"foo": 1.234},
109 ok: func(out url.Values) bool {
110 return out.Get("foo") == "1.234"
114 in: map[string]interface{}{"foo": "1.234"},
115 ok: func(out url.Values) bool {
116 return out.Get("foo") == "1.234"
120 in: map[string]interface{}{"foo": map[string]interface{}{"bar": 1.234}},
121 ok: func(out url.Values) bool {
122 return out.Get("foo") == `{"bar":1.234}`
126 in: url.Values{"foo": {"bar"}},
127 ok: func(out url.Values) bool {
128 return out.Get("foo") == "bar"
141 out, err := anythingToValues(tc.in)
145 t.Errorf("got %#v, expected error", out)
148 t.Errorf("got err %#v, expected nil", err)
150 t.Errorf("got %#v but tc.ok() says that is wrong", out)