diff --git a/src/github.com/matrix-org/go-neb/services/guggy/guggy_test.go b/src/github.com/matrix-org/go-neb/services/guggy/guggy_test.go index b7d5a05..f79bf74 100644 --- a/src/github.com/matrix-org/go-neb/services/guggy/guggy_test.go +++ b/src/github.com/matrix-org/go-neb/services/guggy/guggy_test.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" + "github.com/matrix-org/go-neb/testutils" "github.com/matrix-org/go-neb/types" "io/ioutil" "net/http" @@ -14,14 +15,6 @@ import ( "testing" ) -type MockTransport struct { - roundTrip func(*http.Request) (*http.Response, error) -} - -func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { - return t.roundTrip(req) -} - // TODO: It would be nice to tabularise this test so we can try failing different combinations of responses to make // sure all cases are handled, rather than just the general case as is here. func TestCommand(t *testing.T) { @@ -30,8 +23,8 @@ func TestCommand(t *testing.T) { guggyImageURL := "https://guggy.com/gifs/23ryf872fg" // Mock the response from Guggy - guggyTrans := struct{ MockTransport }{} - guggyTrans.roundTrip = func(req *http.Request) (*http.Response, error) { + guggyTrans := struct{ testutils.MockTransport }{} + guggyTrans.RT = func(req *http.Request) (*http.Response, error) { guggyURL := "https://text2gif.guggy.com/guggify" if req.URL.String() != guggyURL { t.Fatalf("Bad URL: got %s want %s", req.URL.String(), guggyURL) @@ -79,8 +72,8 @@ func TestCommand(t *testing.T) { guggy := srv.(*Service) // Mock the response from Matrix - matrixTrans := struct{ MockTransport }{} - matrixTrans.roundTrip = func(req *http.Request) (*http.Response, error) { + matrixTrans := struct{ testutils.MockTransport }{} + matrixTrans.RT = func(req *http.Request) (*http.Response, error) { if req.URL.String() == guggyImageURL { // getting the guggy image return &http.Response{ StatusCode: 200, diff --git a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot_test.go b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot_test.go index cc6b260..040b2ea 100644 --- a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot_test.go +++ b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot_test.go @@ -14,6 +14,7 @@ import ( "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" + "github.com/matrix-org/go-neb/testutils" "github.com/matrix-org/go-neb/types" ) @@ -36,20 +37,12 @@ const rssFeedXML = ` ` -type MockTransport struct { - roundTrip func(*http.Request) (*http.Response, error) -} - -func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { - return t.roundTrip(req) -} - func TestHTMLEntities(t *testing.T) { database.SetServiceDB(&database.NopStorage{}) feedURL := "https://thehappymaskshop.hyrule" // Replace the cachingClient with a mock so we can intercept RSS requests - rssTrans := struct{ MockTransport }{} - rssTrans.roundTrip = func(req *http.Request) (*http.Response, error) { + rssTrans := struct{ testutils.MockTransport }{} + rssTrans.RT = func(req *http.Request) (*http.Response, error) { if req.URL.String() != feedURL { return nil, errors.New("Unknown test URL") } @@ -79,8 +72,8 @@ func TestHTMLEntities(t *testing.T) { // Create the Matrix client which will send the notification wg := sync.WaitGroup{} wg.Add(1) - matrixTrans := struct{ MockTransport }{} - matrixTrans.roundTrip = func(req *http.Request) (*http.Response, error) { + matrixTrans := struct{ testutils.MockTransport }{} + matrixTrans.RT = func(req *http.Request) (*http.Response, error) { if strings.HasPrefix(req.URL.Path, "/_matrix/client/r0/rooms/!linksroom:hyrule/send/m.room.message") { // Check content body to make sure it is decoded var msg matrix.HTMLMessage diff --git a/src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go b/src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go index 5a8e2e5..77b14e7 100644 --- a/src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go +++ b/src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go @@ -13,6 +13,7 @@ import ( "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/matrix" + "github.com/matrix-org/go-neb/testutils" "github.com/matrix-org/go-neb/types" ) @@ -91,14 +92,6 @@ var travisTests = []struct { }, } -type MockTransport struct { - roundTrip func(*http.Request) (*http.Response, error) -} - -func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { - return t.roundTrip(req) -} - func TestTravisCI(t *testing.T) { database.SetServiceDB(&database.NopStorage{}) @@ -106,8 +99,8 @@ func TestTravisCI(t *testing.T) { urlToKey := make(map[string]string) urlToKey["https://api.travis-ci.org/config"] = travisOrgPEMPublicKey urlToKey["https://api.travis-ci.com/config"] = travisComPEMPublicKey - travisTransport := struct{ MockTransport }{} - travisTransport.roundTrip = func(req *http.Request) (*http.Response, error) { + travisTransport := struct{ testutils.MockTransport }{} + travisTransport.RT = func(req *http.Request) (*http.Response, error) { if key := urlToKey[req.URL.String()]; key != "" { escKey, _ := json.Marshal(key) return &http.Response{ @@ -124,8 +117,8 @@ func TestTravisCI(t *testing.T) { // Intercept message sending to Matrix and mock responses msgs := []matrix.TextMessage{} - matrixTrans := struct{ MockTransport }{} - matrixTrans.roundTrip = func(req *http.Request) (*http.Response, error) { + matrixTrans := struct{ testutils.MockTransport }{} + matrixTrans.RT = func(req *http.Request) (*http.Response, error) { if !strings.Contains(req.URL.String(), "/send/m.room.message") { return nil, fmt.Errorf("Unhandled URL: %s", req.URL.String()) } diff --git a/src/github.com/matrix-org/go-neb/testutils/testutils.go b/src/github.com/matrix-org/go-neb/testutils/testutils.go new file mode 100644 index 0000000..ba8508e --- /dev/null +++ b/src/github.com/matrix-org/go-neb/testutils/testutils.go @@ -0,0 +1,15 @@ +package testutils + +import ( + "net/http" +) + +// MockTransport implements RoundTripper +type MockTransport struct { + RT func(*http.Request) (*http.Response, error) +} + +// RoundTrip is a RoundTripper +func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { + return t.RT(req) +}