diff --git a/couchdb/client.go b/couchdb/client.go index 31ab810c..61866cbf 100644 --- a/couchdb/client.go +++ b/couchdb/client.go @@ -40,7 +40,7 @@ func (c *client) DBExists(ctx context.Context, dbName string, _ driver.Options) if dbName == "" { return false, missingArg("dbName") } - _, err := c.DoError(ctx, http.MethodHead, dbName, nil) + _, err := c.DoError(ctx, http.MethodHead, url.PathEscape(dbName), nil) if kivik.HTTPStatus(err) == http.StatusNotFound { return false, nil } diff --git a/couchdb/client_test.go b/couchdb/client_test.go index e2637d03..d45de541 100644 --- a/couchdb/client_test.go +++ b/couchdb/client_test.go @@ -15,8 +15,10 @@ package couchdb import ( "context" "errors" + "fmt" "io" "net/http" + "net/url" "testing" "github.com/google/go-cmp/cmp" @@ -131,6 +133,34 @@ func TestDBExists(t *testing.T) { }, nil), exists: true, }, + { + name: "slashes", + dbName: "foo/bar", + client: newCustomClient(func(req *http.Request) (*http.Response, error) { + if err := consume(req.Body); err != nil { + return nil, err + } + expected := "/" + url.PathEscape("foo/bar") + actual := req.URL.RawPath + if actual != expected { + return nil, fmt.Errorf("expected path %s, got %s", expected, actual) + } + response := &http.Response{ + StatusCode: 200, + Header: http.Header{ + "Server": {"CouchDB/1.6.1 (Erlang OTP/17)"}, + "Date": {"Fri, 27 Oct 2017 15:09:19 GMT"}, + "Content-Type": {"text/plain; charset=utf-8"}, + "Content-Length": {"229"}, + "Cache-Control": {"must-revalidate"}, + }, + Body: Body(""), + } + response.Request = req + return response, nil + }), + exists: true, + }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) {