You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
775 B

8 years ago
8 years ago
  1. package testutils
  2. import (
  3. "net/http"
  4. )
  5. // MockTransport implements RoundTripper
  6. type MockTransport struct {
  7. // RT is the RoundTrip function. Replace this function with your test function.
  8. // For example:
  9. // t := MockTransport{}
  10. // t.RT = func(req *http.Request) (*http.Response, error) {
  11. // // assert req args, return res or error
  12. // }
  13. RT func(*http.Request) (*http.Response, error)
  14. }
  15. // RoundTrip is a RoundTripper
  16. func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  17. return t.RT(req)
  18. }
  19. // NewRoundTripper returns a new RoundTripper which will call the provided function.
  20. func NewRoundTripper(roundTrip func(*http.Request) (*http.Response, error)) http.RoundTripper {
  21. rt := MockTransport{}
  22. rt.RT = roundTrip
  23. return rt
  24. }