1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
18 type stubTransport struct {
19 Responses map[string]string
20 Requests []http.Request
24 func (stub *stubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
26 stub.Requests = append(stub.Requests, *req)
29 resp := &http.Response{
37 str := stub.Responses[req.URL.Path]
39 resp.Status = "404 Not Found"
43 buf := bytes.NewBufferString(str)
44 resp.Body = ioutil.NopCloser(buf)
45 resp.ContentLength = int64(buf.Len())
49 type errorTransport struct{}
51 func (stub *errorTransport) RoundTrip(req *http.Request) (*http.Response, error) {
52 return nil, fmt.Errorf("something awful happened")
55 type timeoutTransport struct {
59 func (stub *timeoutTransport) RoundTrip(req *http.Request) (*http.Response, error) {
60 return &http.Response{
67 Body: ioutil.NopCloser(iotest.TimeoutReader(bytes.NewReader(stub.response))),
71 func TestCurrentUser(t *testing.T) {
73 stub := &stubTransport{
74 Responses: map[string]string{
75 "/arvados/v1/users/current": `{"uuid":"zzzzz-abcde-012340123401234"}`,
82 APIHost: "zzzzz.arvadosapi.com",
85 u, err := c.CurrentUser()
89 if x := "zzzzz-abcde-012340123401234"; u.UUID != x {
90 t.Errorf("got uuid %q, expected %q", u.UUID, x)
92 if len(stub.Requests) < 1 {
93 t.Fatal("empty stub.Requests")
95 hdr := stub.Requests[len(stub.Requests)-1].Header
96 if hdr.Get("Authorization") != "OAuth2 xyzzy" {
97 t.Errorf("got headers %+q, expected Authorization header", hdr)
100 c.Client.Transport = &errorTransport{}
101 u, err = c.CurrentUser()
103 t.Errorf("got nil error, expected something awful")
107 func TestAnythingToValues(t *testing.T) {
108 type testCase struct {
110 // ok==nil means anythingToValues should return an
111 // error, otherwise it's a func that returns true if
113 ok func(out url.Values) bool
115 for _, tc := range []testCase{
117 in: map[string]interface{}{"foo": "bar"},
118 ok: func(out url.Values) bool {
119 return out.Get("foo") == "bar"
123 in: map[string]interface{}{"foo": 2147483647},
124 ok: func(out url.Values) bool {
125 return out.Get("foo") == "2147483647"
129 in: map[string]interface{}{"foo": 1.234},
130 ok: func(out url.Values) bool {
131 return out.Get("foo") == "1.234"
135 in: map[string]interface{}{"foo": "1.234"},
136 ok: func(out url.Values) bool {
137 return out.Get("foo") == "1.234"
141 in: map[string]interface{}{"foo": map[string]interface{}{"bar": 1.234}},
142 ok: func(out url.Values) bool {
143 return out.Get("foo") == `{"bar":1.234}`
147 in: url.Values{"foo": {"bar"}},
148 ok: func(out url.Values) bool {
149 return out.Get("foo") == "bar"
162 out, err := anythingToValues(tc.in)
166 t.Errorf("got %#v, expected error", out)
169 t.Errorf("got err %#v, expected nil", err)
171 t.Errorf("got %#v but tc.ok() says that is wrong", out)