Skip to content

Commit

Permalink
Merge pull request #267 from go-kivik/duplicateParams-v3
Browse files Browse the repository at this point in the history
Avoid duplicate rev= parameters for Delete() method
  • Loading branch information
flimzy authored Dec 2, 2020
2 parents 487ff2f + 422cfbc commit 2b1cc4b
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 202 deletions.
2 changes: 1 addition & 1 deletion chttp/chttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const typeJSON = "application/json"
// The default UserAgent values
const (
UserAgent = "Kivik chttp"
Version = "3.2.1"
Version = "3.2.5"
)

// Client represents a client connection. It embeds an *http.Client
Expand Down
150 changes: 72 additions & 78 deletions chttp/chttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,94 +28,88 @@ var defaultUA = func() string {
}()

func TestNew(t *testing.T) {
type newTest struct {
name string
type tt struct {
dsn string
expected *Client
status int
curlStatus int
err string
}
tests := []newTest{
{
name: "invalid url",
dsn: "http://foo.com/%xx",
status: http.StatusBadRequest,
curlStatus: ExitStatusURLMalformed,
err: `parse "?http://foo.com/%xx"?: invalid URL escape "%xx"`,
},
{
name: "no url",
dsn: "",
status: http.StatusBadRequest,
curlStatus: ExitFailedToInitialize,
err: "no URL specified",
},
{
name: "no auth",
dsn: "http://foo.com/",
expected: &Client{
Client: &http.Client{},
rawDSN: "http://foo.com/",
dsn: &url.URL{
Scheme: "http",
Host: "foo.com",
Path: "/",
},

tests := testy.NewTable()
tests.Add("invalid url", tt{
dsn: "http://foo.com/%xx",
status: http.StatusBadRequest,
curlStatus: ExitStatusURLMalformed,
err: `parse "?http://foo.com/%xx"?: invalid URL escape "%xx"`,
})
tests.Add("no url", tt{
dsn: "",
status: http.StatusBadRequest,
curlStatus: ExitFailedToInitialize,
err: "no URL specified",
})
tests.Add("no auth", tt{
dsn: "http://foo.com/",
expected: &Client{
Client: &http.Client{},
rawDSN: "http://foo.com/",
dsn: &url.URL{
Scheme: "http",
Host: "foo.com",
Path: "/",
},
},
func() newTest {
h := func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"userCtx":{"name":"user"}}`) // nolint: errcheck
}
s := httptest.NewServer(http.HandlerFunc(h))
authDSN, _ := url.Parse(s.URL)
dsn, _ := url.Parse(s.URL + "/")
authDSN.User = url.UserPassword("user", "password")
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
c := &Client{
Client: &http.Client{Jar: jar},
rawDSN: authDSN.String(),
dsn: dsn,
}
auth := &CookieAuth{
Username: "user",
Password: "password",
client: c,
transport: http.DefaultTransport,
}
c.auth = auth
c.Client.Transport = auth
return newTest{
name: "auth success",
dsn: authDSN.String(),
expected: c,
}
}(),
{
name: "default url scheme",
dsn: "foo.com",
expected: &Client{
Client: &http.Client{},
rawDSN: "foo.com",
dsn: &url.URL{
Scheme: "http",
Host: "foo.com",
Path: "/",
},
})
tests.Add("auth success", func(t *testing.T) interface{} {
h := func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, `{"userCtx":{"name":"user"}}`) // nolint: errcheck
}
s := httptest.NewServer(http.HandlerFunc(h))
authDSN, _ := url.Parse(s.URL)
dsn, _ := url.Parse(s.URL + "/")
authDSN.User = url.UserPassword("user", "password")
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
c := &Client{
Client: &http.Client{Jar: jar},
rawDSN: authDSN.String(),
dsn: dsn,
}
auth := &CookieAuth{
Username: "user",
Password: "password",
client: c,
transport: http.DefaultTransport,
}
c.auth = auth
c.Client.Transport = auth

return tt{
dsn: authDSN.String(),
expected: c,
}
})
tests.Add("default url scheme", tt{
dsn: "foo.com",
expected: &Client{
Client: &http.Client{},
rawDSN: "foo.com",
dsn: &url.URL{
Scheme: "http",
Host: "foo.com",
Path: "/",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := New(test.dsn)
curlStatusErrorRE(t, test.err, test.status, test.curlStatus, err)
if d := testy.DiffInterface(test.expected, result); d != nil {
t.Error(d)
}
})
}
})

tests.Run(t, func(t *testing.T, tt tt) {
result, err := New(tt.dsn)
curlStatusErrorRE(t, tt.err, tt.status, tt.curlStatus, err)
if d := testy.DiffInterface(tt.expected, result); d != nil {
t.Error(d)
}
})
}

func TestParseDSN(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package couchdb

// Version is the current version of this package.
const Version = "3.2.1"
const Version = "3.2.5"

const (
// OptionFullCommit is the option key used to set the `X-Couch-Full-Commit`
Expand Down
4 changes: 3 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,9 @@ func (d *db) Delete(ctx context.Context, docID, rev string, options map[string]i
if err != nil {
return "", err
}
query.Add("rev", rev)
if query.Get("rev") == "" {
query.Set("rev", rev)
}
opts := &chttp.Options{
FullCommit: fullCommit,
Query: query,
Expand Down
Loading

0 comments on commit 2b1cc4b

Please sign in to comment.