forked from go-kivik/couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_test.go
142 lines (121 loc) · 3.13 KB
/
mock_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package couchdb
import (
"context"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"testing"
"time"
"github.com/pkg/errors"
"github.com/go-kivik/couchdb/v4/chttp"
"github.com/go-kivik/kiviktest/v4/kt"
)
type customTransport func(*http.Request) (*http.Response, error)
var _ http.RoundTripper = customTransport(nil)
func (t customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t(req)
}
func newTestDB(response *http.Response, err error) *db {
return &db{
dbName: "testdb",
client: newTestClient(response, err),
}
}
func newCustomDB(fn func(*http.Request) (*http.Response, error)) *db {
return &db{
dbName: "testdb",
client: newCustomClient(fn),
}
}
func newTestClient(response *http.Response, err error) *client {
return newCustomClient(func(req *http.Request) (*http.Response, error) {
if e := consume(req.Body); e != nil {
return nil, e
}
if err != nil {
return nil, err
}
response := response
response.Request = req
return response, nil
})
}
func newCustomClient(fn func(*http.Request) (*http.Response, error)) *client {
chttpClient, _ := chttp.New("http://example.com/")
chttpClient.Client.Transport = customTransport(fn)
return &client{
Client: chttpClient,
}
}
func Body(str string) io.ReadCloser {
if !strings.HasSuffix(str, "\n") {
str = str + "\n"
}
return ioutil.NopCloser(strings.NewReader(str))
}
func parseTime(t *testing.T, str string) time.Time {
ts, err := time.Parse(time.RFC3339, str)
if err != nil {
t.Fatal(err)
}
return ts
}
// consume consumes and closes r or does nothing if it is nil.
func consume(r io.ReadCloser) error {
if r == nil {
return nil
}
defer r.Close() // nolint: errcheck
_, e := ioutil.ReadAll(r)
return e
}
type mockReadCloser struct {
ReadFunc func([]byte) (int, error)
CloseFunc func() error
}
var _ io.ReadCloser = &mockReadCloser{}
func (rc *mockReadCloser) Read(p []byte) (int, error) {
return rc.ReadFunc(p)
}
func (rc *mockReadCloser) Close() error {
return rc.CloseFunc()
}
func realDB(t *testing.T) *db {
db, err := realDBConnect(t)
if err != nil {
if _, ok := errors.Cause(err).(*url.Error); ok {
t.Skip("Cannot connect to CouchDB")
}
if strings.HasSuffix(err.Error(), "connect: connection refused") {
t.Skip("Cannot connect to CouchDB")
}
t.Fatal(err)
}
return db
}
func realDBConnect(t *testing.T) (*db, error) {
driver := &Couch{}
c, err := driver.NewClient(kt.DSN(t))
if err != nil {
return nil, err
}
dbname := kt.TestDBName(t)
err = c.CreateDB(context.Background(), dbname, nil)
return &db{
client: c.(*client),
dbName: dbname,
}, err
}