...
1
2
3
4
5
6
7
8
9
10
11
12
13 package chttp
14
15 import (
16 "io"
17 "net/http"
18 "net/url"
19 "strings"
20 )
21
22 type customTransport func(*http.Request) (*http.Response, error)
23
24 var _ http.RoundTripper = customTransport(nil)
25
26 func (c customTransport) RoundTrip(req *http.Request) (*http.Response, error) {
27 return c(req)
28 }
29
30 func newCustomClient(dsn string, fn func(*http.Request) (*http.Response, error)) *Client {
31 c := &Client{
32 Client: &http.Client{
33 Transport: customTransport(fn),
34 },
35 }
36 var err error
37 if dsn == "" {
38 dsn = "http://example.com/"
39 }
40 c.dsn, err = url.Parse(dsn)
41 if err != nil {
42 panic(err)
43 }
44 c.basePath = strings.TrimSuffix(c.dsn.Path, "/")
45 return c
46 }
47
48 func newTestClient(resp *http.Response, err error) *Client {
49 return newCustomClient("", func(_ *http.Request) (*http.Response, error) {
50 return resp, err
51 })
52 }
53
54 func Body(str string) io.ReadCloser {
55 return io.NopCloser(strings.NewReader(str))
56 }
57
View as plain text