Skip to content

Commit

Permalink
couchdb: fix TestSecurity on Go 1.7
Browse files Browse the repository at this point in the history
In Go 1.7, where ResponseRecorder.Result was introduced, ContentLength
isn't handled properly by Result. Just remove the extra check for it in
the client code, and rely on io.EOF instead. This more robust anyway.
  • Loading branch information
fjl committed Nov 8, 2020
1 parent 47d268d commit 75016f3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
6 changes: 2 additions & 4 deletions couchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,8 @@ func (db *DB) Security() (*Security, error) {
if err != nil {
return nil, err
}
if resp.ContentLength == 0 {
// empty reply means defaults
return secobj, nil
}
// The extra check for io.EOF is there because empty responses are OK.
// CouchDB returns an empty response if no security object has been set.
if err = readBody(resp, secobj); err != nil && err != io.EOF {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion couchdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
. "net/http"
"net/url"
"regexp"
"strconv"
"testing"

"github.com/fjl/go-couchdb"
Expand Down Expand Up @@ -142,6 +143,7 @@ var securityObject = &couchdb.Security{
func TestSecurity(t *testing.T) {
c := newTestClient(t)
c.Handle("GET /db/_security", func(resp ResponseWriter, req *Request) {
resp.Header().Set("content-length", strconv.Itoa(len(securityObjectJSON)))
io.WriteString(resp, securityObjectJSON)
})

Expand All @@ -155,7 +157,8 @@ func TestSecurity(t *testing.T) {
func TestEmptySecurity(t *testing.T) {
c := newTestClient(t)
c.Handle("GET /db/_security", func(resp ResponseWriter, req *Request) {
// CouchDB returns an empty reply if no security object has been set
// CouchDB returns an empty response if no security object has been set.
resp.Header().Set("content-length", "0")
resp.WriteHeader(200)
})

Expand Down
2 changes: 1 addition & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func responseRev(resp *http.Response, err error) (string, error) {
}

func readBody(resp *http.Response, v interface{}) error {
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
resp.Body.Close()
return err
}
Expand Down

0 comments on commit 75016f3

Please sign in to comment.