-
Notifications
You must be signed in to change notification settings - Fork 36
/
x_test.go
63 lines (54 loc) · 1.75 KB
/
x_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
// This file contains stuff that is used across all the tests.
package couchdb_test
import (
"bytes"
. "net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/fjl/go-couchdb"
)
// testClient is a very special couchdb.Client that also implements
// the http.RoundTripper interface. The tests can register HTTP
// handlers on the testClient. Any requests made through the client are
// dispatched to a matching handler. This allows us to test what the
// HTTP client in the couchdb package does without actually using the network.
//
// If no handler matches the requests method/path combination, the test
// fails with a descriptive error.
type testClient struct {
*couchdb.Client
t *testing.T
handlers map[string]Handler
}
func (s *testClient) Handle(pat string, f func(ResponseWriter, *Request)) {
s.handlers[pat] = HandlerFunc(f)
}
func (s *testClient) ClearHandlers() {
s.handlers = make(map[string]Handler)
}
func (s *testClient) RoundTrip(req *Request) (*Response, error) {
handler, ok := s.handlers[req.Method+" "+req.URL.EscapedPath()]
if !ok {
s.t.Fatalf("unhandled request: %s %s", req.Method, req.URL.EscapedPath())
return nil, nil
}
recorder := httptest.NewRecorder()
recorder.Body = new(bytes.Buffer)
handler.ServeHTTP(recorder, req)
return recorder.Result(), nil
}
func newTestClient(t *testing.T) *testClient {
tc := &testClient{t: t, handlers: make(map[string]Handler)}
client, err := couchdb.NewClient("http://testClient:5984/", tc)
if err != nil {
t.Fatalf("couchdb.NewClient returned error: %v", err)
}
tc.Client = client
return tc
}
func check(t *testing.T, field string, expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
t.Errorf("%s mismatch:\nwant %#v\ngot %#v", field, expected, actual)
}
}