Browse Source

Add a dedicated testutils package

In the spirit of "if you have to do something 3 times then factor it
out", make a testutils package to put all the `RoundTrip` boilerplate.

I don't overly like having test packages, especially mixed in with
code, but I don't see a nicer way of doing this without ending up
with a sprawling mess of copypasta'd test boilerplate which will
be an absolute nightmare to maintain.

I think this is the lesser of two evils.
pull/129/head
Kegan Dougal 8 years ago
parent
commit
8ec37a4cf9
  1. 17
      src/github.com/matrix-org/go-neb/services/guggy/guggy_test.go
  2. 17
      src/github.com/matrix-org/go-neb/services/rssbot/rssbot_test.go
  3. 17
      src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go
  4. 15
      src/github.com/matrix-org/go-neb/testutils/testutils.go

17
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,

17
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 = `
</channel>
</rss>`
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

17
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())
}

15
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)
}
Loading…
Cancel
Save