Merge branch 'master' into 13822-nm-delayed-daemon
[arvados.git] / sdk / go / arvados / client_test.go
index 2db50bfd6c9adb427ba53a030a54a49c5431dbdf..df938008d49756b850ca6e5ce5abee8a0510e2a3 100644 (file)
@@ -1,3 +1,7 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: Apache-2.0
+
 package arvados
 
 import (
@@ -5,8 +9,10 @@ import (
        "fmt"
        "io/ioutil"
        "net/http"
+       "net/url"
        "sync"
        "testing"
+       "testing/iotest"
 )
 
 type stubTransport struct {
@@ -46,6 +52,22 @@ func (stub *errorTransport) RoundTrip(req *http.Request) (*http.Response, error)
        return nil, fmt.Errorf("something awful happened")
 }
 
+type timeoutTransport struct {
+       response []byte
+}
+
+func (stub *timeoutTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+       return &http.Response{
+               Status:     "200 OK",
+               StatusCode: 200,
+               Proto:      "HTTP/1.1",
+               ProtoMajor: 1,
+               ProtoMinor: 1,
+               Request:    req,
+               Body:       ioutil.NopCloser(iotest.TimeoutReader(bytes.NewReader(stub.response))),
+       }, nil
+}
+
 func TestCurrentUser(t *testing.T) {
        t.Parallel()
        stub := &stubTransport{
@@ -81,3 +103,72 @@ func TestCurrentUser(t *testing.T) {
                t.Errorf("got nil error, expected something awful")
        }
 }
+
+func TestAnythingToValues(t *testing.T) {
+       type testCase struct {
+               in interface{}
+               // ok==nil means anythingToValues should return an
+               // error, otherwise it's a func that returns true if
+               // out is correct
+               ok func(out url.Values) bool
+       }
+       for _, tc := range []testCase{
+               {
+                       in: map[string]interface{}{"foo": "bar"},
+                       ok: func(out url.Values) bool {
+                               return out.Get("foo") == "bar"
+                       },
+               },
+               {
+                       in: map[string]interface{}{"foo": 2147483647},
+                       ok: func(out url.Values) bool {
+                               return out.Get("foo") == "2147483647"
+                       },
+               },
+               {
+                       in: map[string]interface{}{"foo": 1.234},
+                       ok: func(out url.Values) bool {
+                               return out.Get("foo") == "1.234"
+                       },
+               },
+               {
+                       in: map[string]interface{}{"foo": "1.234"},
+                       ok: func(out url.Values) bool {
+                               return out.Get("foo") == "1.234"
+                       },
+               },
+               {
+                       in: map[string]interface{}{"foo": map[string]interface{}{"bar": 1.234}},
+                       ok: func(out url.Values) bool {
+                               return out.Get("foo") == `{"bar":1.234}`
+                       },
+               },
+               {
+                       in: url.Values{"foo": {"bar"}},
+                       ok: func(out url.Values) bool {
+                               return out.Get("foo") == "bar"
+                       },
+               },
+               {
+                       in: 1234,
+                       ok: nil,
+               },
+               {
+                       in: []string{"foo"},
+                       ok: nil,
+               },
+       } {
+               t.Logf("%#v", tc.in)
+               out, err := anythingToValues(tc.in)
+               switch {
+               case tc.ok == nil:
+                       if err == nil {
+                               t.Errorf("got %#v, expected error", out)
+                       }
+               case err != nil:
+                       t.Errorf("got err %#v, expected nil", err)
+               case !tc.ok(out):
+                       t.Errorf("got %#v but tc.ok() says that is wrong", out)
+               }
+       }
+}