File tree Expand file tree Collapse file tree 2 files changed +12
-3
lines changed Expand file tree Collapse file tree 2 files changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -115,3 +115,12 @@ func DoerTransport(cl interface {
115
115
}) Transport {
116
116
return RoundTripFunc (cl .Do )
117
117
}
118
+
119
+ // ErrorTransport always returns the specified error instead of connecting.
120
+ // It is intended for use in testing
121
+ // or to prevent accidental use of http.DefaultClient.
122
+ func ErrorTransport (err error ) Transport {
123
+ return RoundTripFunc (func (req * http.Request ) (* http.Response , error ) {
124
+ return nil , err
125
+ })
126
+ }
Original file line number Diff line number Diff line change 4
4
"bytes"
5
5
"context"
6
6
"crypto/md5"
7
+ "errors"
7
8
"fmt"
8
9
"io"
9
10
"net/http"
@@ -111,9 +112,8 @@ func ExampleLogTransport() {
111
112
fmt .Println ("Error!" , err )
112
113
}
113
114
// Works for bad responses too
114
- baseTrans = requests .RoundTripFunc (func (req * http.Request ) (* http.Response , error ) {
115
- return nil , fmt .Errorf ("can't connect" )
116
- })
115
+ baseTrans = requests .ErrorTransport (errors .New ("can't connect" ))
116
+
117
117
trans = requests .LogTransport (baseTrans , logger )
118
118
119
119
if err := requests .
You can’t perform that action at this time.
0 commit comments