1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
17 type stubTransport struct {
18 Responses map[string]string
19 Requests []http.Request
23 func (stub *stubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
25 stub.Requests = append(stub.Requests, *req)
28 resp := &http.Response{
36 str := stub.Responses[req.URL.Path]
38 resp.Status = "404 Not Found"
42 buf := bytes.NewBufferString(str)
43 resp.Body = ioutil.NopCloser(buf)
44 resp.ContentLength = int64(buf.Len())
48 type errorTransport struct{}
50 func (stub *errorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
51 return nil, fmt.Errorf("something awful happened")
54 func TestCurrentUser(t *testing.T) {
56 stub := &stubTransport{
57 Responses: map[string]string{
58 "/arvados/v1/users/current": `{"uuid":"zzzzz-abcde-012340123401234"}`,
65 APIHost: "zzzzz.arvadosapi.com",
68 u, err := c.CurrentUser()
72 if x := "zzzzz-abcde-012340123401234"; u.UUID != x {
73 t.Errorf("got uuid %q, expected %q", u.UUID, x)
75 if len(stub.Requests) < 1 {
76 t.Fatal("empty stub.Requests")
78 hdr := stub.Requests[len(stub.Requests)-1].Header
79 if hdr.Get("Authorization") != "OAuth2 xyzzy" {
80 t.Errorf("got headers %+q, expected Authorization header", hdr)
83 c.Client.Transport = &errorTransport{}
84 u, err = c.CurrentUser()
86 t.Errorf("got nil error, expected something awful")
90 func TestAnythingToValues(t *testing.T) {
91 type testCase struct {
93 // ok==nil means anythingToValues should return an
94 // error, otherwise it's a func that returns true if
96 ok func(out url.Values) bool
98 for _, tc := range []testCase{
100 in: map[string]interface{}{"foo": "bar"},
101 ok: func(out url.Values) bool {
102 return out.Get("foo") == "bar"
106 in: map[string]interface{}{"foo": 2147483647},
107 ok: func(out url.Values) bool {
108 return out.Get("foo") == "2147483647"
112 in: map[string]interface{}{"foo": 1.234},
113 ok: func(out url.Values) bool {
114 return out.Get("foo") == "1.234"
118 in: map[string]interface{}{"foo": "1.234"},
119 ok: func(out url.Values) bool {
120 return out.Get("foo") == "1.234"
124 in: map[string]interface{}{"foo": map[string]interface{}{"bar": 1.234}},
125 ok: func(out url.Values) bool {
126 return out.Get("foo") == `{"bar":1.234}`
130 in: url.Values{"foo": {"bar"}},
131 ok: func(out url.Values) bool {
132 return out.Get("foo") == "bar"
145 out, err := anythingToValues(tc.in)
149 t.Errorf("got %#v, expected error", out)
152 t.Errorf("got err %#v, expected nil", err)
154 t.Errorf("got %#v but tc.ok() says that is wrong", out)