github.com/go-kivik/kivik/v4@v4.3.2/couchdb/couchdb_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package couchdb 14 15 import ( 16 "net/http" 17 "testing" 18 "time" 19 20 "gitlab.com/flimzy/testy" 21 22 "github.com/go-kivik/kivik/v4/driver" 23 internal "github.com/go-kivik/kivik/v4/int/errors" 24 "github.com/go-kivik/kivik/v4/int/mock" 25 ) 26 27 func TestNewClient(t *testing.T) { 28 type ncTest struct { 29 name string 30 dsn string 31 options driver.Options 32 expectedUA []string 33 status int 34 err string 35 } 36 tests := []ncTest{ 37 { 38 name: "invalid url", 39 dsn: "foo.com/%xxx", 40 status: http.StatusBadRequest, 41 err: `parse "?http://foo.com/%xxx"?: invalid URL escape "%xx"`, 42 }, 43 { 44 name: "success", 45 dsn: "http://foo.com/", 46 expectedUA: nil, 47 }, 48 { 49 name: "User Agent", 50 dsn: "http://foo.com/", 51 options: OptionUserAgent("test/foo"), 52 expectedUA: []string{ 53 "test/foo", 54 }, 55 }, 56 } 57 58 for _, test := range tests { 59 t.Run(test.name, func(t *testing.T) { 60 driver := &couch{} 61 opts := test.options 62 if opts == nil { 63 opts = mock.NilOption 64 } 65 result, err := driver.NewClient(test.dsn, opts) 66 if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" { 67 t.Error(d) 68 } 69 if err != nil { 70 return 71 } 72 client, ok := result.(*client) 73 if !ok { 74 t.Errorf("Unexpected type returned: %t", result) 75 } 76 if d := testy.DiffInterface(test.expectedUA, client.Client.UserAgents); d != nil { 77 t.Error(d) 78 } 79 }) 80 } 81 t.Run("custom HTTP client", func(t *testing.T) { 82 opts := OptionHTTPClient(&http.Client{Timeout: time.Millisecond}) 83 driver := &couch{} 84 c, err := driver.NewClient("http://example.com/", opts) 85 if err != nil { 86 t.Fatal(err) 87 } 88 if c.(*client).Client.Timeout != time.Millisecond { 89 t.Error("Unexpected *http.Client returned") 90 } 91 }) 92 } 93 94 func TestDB(t *testing.T) { 95 tests := []struct { 96 name string 97 client *client 98 dbName string 99 expected *db 100 status int 101 err string 102 }{ 103 { 104 name: "no dbname", 105 status: http.StatusBadRequest, 106 err: "kivik: dbName required", 107 }, 108 { 109 name: "no full commit", 110 dbName: "foo", 111 expected: &db{ 112 dbName: "foo", 113 }, 114 }, 115 } 116 for _, test := range tests { 117 t.Run(test.name, func(t *testing.T) { 118 result, err := test.client.DB(test.dbName, mock.NilOption) 119 if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" { 120 t.Error(d) 121 } 122 if err != nil { 123 return 124 } 125 if _, ok := result.(*db); !ok { 126 t.Errorf("Unexpected result type: %T", result) 127 } 128 }) 129 } 130 }