Skip to content

Commit

Permalink
Merge pull request #1041 from dominicbarnes/couchdb-db-exists-url-pat…
Browse files Browse the repository at this point in the history
…h-encode

fix(couchdb): use url.PathEscape in DBExists for consistency with other DB* methods
  • Loading branch information
flimzy authored Aug 21, 2024
2 parents f10e42a + 8c9b10a commit 9e790c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion couchdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 30 additions & 0 deletions couchdb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ package couchdb
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9e790c4

Please sign in to comment.