1package dash 2 3import ( 4 "io" 5 "net/http" 6 "time" 7 8 "github.com/apex/log" 9 "github.com/ooni/probe-engine/model" 10) 11 12type FakeDeps struct { 13 httpTransport http.RoundTripper 14 jsonMarshalErr error 15 jsonMarshalResult []byte 16 newHTTPRequestErr error 17 newHTTPRequestResult *http.Request 18 readAllErr error 19 readAllResult []byte 20} 21 22func (d FakeDeps) HTTPClient() *http.Client { 23 return &http.Client{Transport: d.httpTransport} 24} 25 26func (d FakeDeps) JSONMarshal(v interface{}) ([]byte, error) { 27 return d.jsonMarshalResult, d.jsonMarshalErr 28} 29 30func (d FakeDeps) Logger() model.Logger { 31 return log.Log 32} 33 34func (d FakeDeps) NewHTTPRequest( 35 method string, url string, body io.Reader) (*http.Request, error) { 36 return d.newHTTPRequestResult, d.newHTTPRequestErr 37} 38 39func (d FakeDeps) ReadAll(r io.Reader) ([]byte, error) { 40 return d.readAllResult, d.readAllErr 41} 42 43func (d FakeDeps) Scheme() string { 44 return "https" 45} 46 47func (d FakeDeps) UserAgent() string { 48 return "miniooni/0.1.0-dev" 49} 50 51type FakeHTTPTransport struct { 52 err error 53 resp *http.Response 54} 55 56func (txp FakeHTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) { 57 time.Sleep(10 * time.Microsecond) 58 return txp.resp, txp.err 59} 60 61type FakeHTTPTransportStack struct { 62 all []FakeHTTPTransport 63} 64 65func (txp *FakeHTTPTransportStack) RoundTrip(req *http.Request) (*http.Response, error) { 66 frame := txp.all[0] 67 txp.all = txp.all[1:] 68 return frame.RoundTrip(req) 69} 70