github.com/go-kivik/kivik/v4@v4.3.2/couchdb/version_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 "context" 17 "errors" 18 "io" 19 "net/http" 20 "strings" 21 "testing" 22 23 "gitlab.com/flimzy/testy" 24 25 "github.com/go-kivik/kivik/v4/driver" 26 internal "github.com/go-kivik/kivik/v4/int/errors" 27 ) 28 29 func TestVersion2(t *testing.T) { 30 tests := []struct { 31 name string 32 client *client 33 expected *driver.Version 34 status int 35 err string 36 }{ 37 { 38 name: "network error", 39 client: newTestClient(nil, errors.New("net error")), 40 status: http.StatusBadGateway, 41 err: `Get "?http://example.com/"?: net error`, 42 }, 43 { 44 name: "invalid JSON response", 45 client: newTestClient(&http.Response{ 46 StatusCode: http.StatusOK, 47 Body: io.NopCloser(strings.NewReader(`{"couchdb":"Welcome","uuid":"a902efb0fac143c2b1f97160796a6347","version":"1.6.1","vendor":{"name":[]}}`)), 48 }, nil), 49 status: http.StatusBadGateway, 50 err: "json: cannot unmarshal array into Go ", 51 }, 52 { 53 name: "error response", 54 client: newTestClient(&http.Response{ 55 StatusCode: http.StatusInternalServerError, 56 Body: io.NopCloser(strings.NewReader("")), 57 }, nil), 58 status: http.StatusInternalServerError, 59 err: "Internal Server Error", 60 }, 61 { 62 name: "CouchDB 1.6.1", 63 client: newTestClient(&http.Response{ 64 StatusCode: http.StatusOK, 65 Body: io.NopCloser(strings.NewReader(`{"couchdb":"Welcome","uuid":"a902efb0fac143c2b1f97160796a6347","version":"1.6.1","vendor":{"version":"1.6.1","name":"The Apache Software Foundation"}}`)), 66 }, nil), 67 expected: &driver.Version{ 68 Version: "1.6.1", 69 Vendor: "The Apache Software Foundation", 70 RawResponse: []byte(`{"couchdb":"Welcome","uuid":"a902efb0fac143c2b1f97160796a6347","version":"1.6.1","vendor":{"version":"1.6.1","name":"The Apache Software Foundation"}}`), 71 }, 72 }, 73 { 74 name: "CouchDB 2.0.0", 75 client: newTestClient(&http.Response{ 76 StatusCode: http.StatusOK, 77 Body: io.NopCloser(strings.NewReader(`{"couchdb":"Welcome","version":"2.0.0","vendor":{"name":"The Apache Software Foundation"}}`)), 78 }, nil), 79 expected: &driver.Version{ 80 Version: "2.0.0", 81 Vendor: "The Apache Software Foundation", 82 RawResponse: []byte(`{"couchdb":"Welcome","version":"2.0.0","vendor":{"name":"The Apache Software Foundation"}}`), 83 }, 84 }, 85 { 86 name: "CouchDB 2.1.0", 87 client: newTestClient(&http.Response{ 88 StatusCode: http.StatusOK, 89 Body: io.NopCloser(strings.NewReader(`{"couchdb":"Welcome","version":"2.1.0","features":["scheduler"],"vendor":{"name":"The Apache Software Foundation"}}`)), 90 }, nil), 91 expected: &driver.Version{ 92 Version: "2.1.0", 93 Vendor: "The Apache Software Foundation", 94 Features: []string{"scheduler"}, 95 RawResponse: []byte(`{"couchdb":"Welcome","version":"2.1.0","features":["scheduler"],"vendor":{"name":"The Apache Software Foundation"}}`), 96 }, 97 }, 98 { 99 name: "Cloudant 2017-10-23", 100 client: newTestClient(&http.Response{ 101 StatusCode: http.StatusOK, 102 Body: io.NopCloser(strings.NewReader(`{"couchdb":"Welcome","version":"2.0.0","vendor":{"name":"IBM Cloudant","version":"6365","variant":"paas"},"features":["geo","scheduler"]}`)), 103 }, nil), 104 expected: &driver.Version{ 105 Version: "2.0.0", 106 Vendor: "IBM Cloudant", 107 Features: []string{"geo", "scheduler"}, 108 RawResponse: []byte(`{"couchdb":"Welcome","version":"2.0.0","vendor":{"name":"IBM Cloudant","version":"6365","variant":"paas"},"features":["geo","scheduler"]}`), 109 }, 110 }, 111 } 112 for _, test := range tests { 113 t.Run(test.name, func(t *testing.T) { 114 result, err := test.client.Version(context.Background()) 115 if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" { 116 t.Error(d) 117 } 118 if err != nil { 119 return 120 } 121 if d := testy.DiffInterface(test.expected, result); d != nil { 122 t.Error(d) 123 } 124 }) 125 } 126 }