Skip to content

Commit

Permalink
Merge pull request #1049 from go-kivik/bug1048
Browse files Browse the repository at this point in the history
Fix nil map assignment panic
  • Loading branch information
flimzy authored Dec 4, 2024
2 parents 72210e5 + bdb0600 commit b836bc2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 45 deletions.
2 changes: 1 addition & 1 deletion couchdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func (c *client) AllDBs(ctx context.Context, opts driver.Options) ([]string, error) {
var query url.Values
query := url.Values{}
opts.Apply(&query)
var allDBs []string
err := c.DoJSON(ctx, http.MethodGet, "/_all_dbs", &chttp.Options{Query: query}, &allDBs)
Expand Down
84 changes: 43 additions & 41 deletions couchdb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,55 @@ import (
)

func TestAllDBs(t *testing.T) {
tests := []struct {
name string
type test struct {
client *client
options kivik.Option
expected []string
status int
err string
}{
{
name: "network error",
client: newTestClient(nil, errors.New("net error")),
status: http.StatusBadGateway,
err: `Get "?http://example.com/_all_dbs"?: net error`,
},
{
name: "2.0.0",
client: newTestClient(&http.Response{
StatusCode: 200,
Header: http.Header{
"Server": {"CouchDB/2.0.0 (Erlang OTP/17)"},
"Date": {"Fri, 27 Oct 2017 15:15:07 GMT"},
"Content-Type": {"application/json"},
"ETag": {`"33UVNAZU752CYNGBBTMWQFP7U"`},
"Transfer-Encoding": {"chunked"},
"X-Couch-Request-ID": {"ab5cd97c3e"},
"X-CouchDB-Body-Time": {"0"},
},
Body: Body(`["_global_changes","_replicator","_users"]`),
}, nil),
expected: []string{"_global_changes", "_replicator", "_users"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
opts := test.options
if opts == nil {
opts = mock.NilOption
}
result, err := test.client.AllDBs(context.Background(), opts)
if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" {
t.Error(d)
}
if d := testy.DiffInterface(test.expected, result); d != nil {
t.Error(d)
}
})
}
tests := testy.NewTable()
tests.Add("network error", test{
client: newTestClient(nil, errors.New("net error")),
status: http.StatusBadGateway,
err: `Get "?http://example.com/_all_dbs"?: net error`,
})
tests.Add("2.0.0", test{
client: newTestClient(&http.Response{
StatusCode: 200,
Header: http.Header{
"Server": {"CouchDB/2.0.0 (Erlang OTP/17)"},
"Date": {"Fri, 27 Oct 2017 15:15:07 GMT"},
"Content-Type": {"application/json"},
"ETag": {`"33UVNAZU752CYNGBBTMWQFP7U"`},
"Transfer-Encoding": {"chunked"},
"X-Couch-Request-ID": {"ab5cd97c3e"},
"X-CouchDB-Body-Time": {"0"},
},
Body: Body(`["_global_changes","_replicator","_users"]`),
}, nil),
expected: []string{"_global_changes", "_replicator", "_users"},
})
tests.Add("with param", test{
client: newTestClient(nil, errors.New("expected")),
options: kivik.Param("startkey", "bar"),
status: http.StatusBadGateway,
err: `Get "?http://example.com/_all_dbs\?startkey=bar"?: expected`,
})

tests.Run(t, func(t *testing.T, test test) {
opts := test.options
if opts == nil {
opts = mock.NilOption
}
result, err := test.client.AllDBs(context.Background(), opts)
if d := internal.StatusErrorDiffRE(test.err, test.status, err); d != "" {
t.Error(d)
}
if d := testy.DiffInterface(test.expected, result); d != nil {
t.Error(d)
}
})
}

func TestDBExists(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions couchdb/test/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ func testCreateSession(ctx *kt.Context, client *chttp.Client) {
}},
{Name: "GoodCredsJSON", Creds: true, Options: &chttp.Options{
ContentType: "application/json",
Body: kt.Body(fmt.Sprintf(`{"name":"%s","password":"%s"}`, name, password)),
Body: kt.Body(`{"name":"%s","password":"%s"}`, name, password),
}},
{Name: "GoodCredsForm", Creds: true, Options: &chttp.Options{
ContentType: "application/x-www-form-urlencoded",
Body: kt.Body(fmt.Sprintf(`name=%s&password=%s`, name, password)),
Body: kt.Body(`name=%s&password=%s`, name, password),
}},
{Name: "BadCredsJSON", Creds: true, Options: &chttp.Options{
ContentType: "application/json",
Body: kt.Body(fmt.Sprintf(`{"name":"%s","password":"%sxxx"}`, name, password)),
Body: kt.Body(`{"name":"%s","password":"%sxxx"}`, name, password),
}},
{Name: "BadCredsForm", Creds: true, Options: &chttp.Options{
ContentType: "application/x-www-form-urlencoded",
Expand Down

0 comments on commit b836bc2

Please sign in to comment.