From 683d36b5c505658f442c1cf713ece1ae1413c5f3 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 24 Nov 2016 16:10:12 +0000 Subject: [PATCH] Review comments --- .../matrix-org/go-neb/services/guggy/guggy_test.go | 5 ++--- .../go-neb/services/rssbot/rssbot_test.go | 5 ++--- .../go-neb/services/travisci/travisci_test.go | 5 ++--- .../matrix-org/go-neb/testutils/testutils.go | 13 +++++++++++++ 4 files changed, 19 insertions(+), 9 deletions(-) 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 f79bf74..480b8b8 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 @@ -23,8 +23,7 @@ func TestCommand(t *testing.T) { guggyImageURL := "https://guggy.com/gifs/23ryf872fg" // Mock the response from Guggy - guggyTrans := struct{ testutils.MockTransport }{} - guggyTrans.RT = func(req *http.Request) (*http.Response, error) { + guggyTrans := testutils.NewRoundTripper(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) @@ -58,7 +57,7 @@ func TestCommand(t *testing.T) { StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBuffer(b)), }, nil - } + }) // clobber the guggy service http client instance httpClient = &http.Client{Transport: guggyTrans} 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 040b2ea..4912d8c 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 @@ -41,8 +41,7 @@ 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{ testutils.MockTransport }{} - rssTrans.RT = func(req *http.Request) (*http.Response, error) { + rssTrans := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { if req.URL.String() != feedURL { return nil, errors.New("Unknown test URL") } @@ -50,7 +49,7 @@ func TestHTMLEntities(t *testing.T) { StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(rssFeedXML)), }, nil - } + }) cachingClient = &http.Client{Transport: rssTrans} // Create the RSS service 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 77b14e7..3c1d695 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 @@ -99,8 +99,7 @@ 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{ testutils.MockTransport }{} - travisTransport.RT = func(req *http.Request) (*http.Response, error) { + travisTransport := testutils.NewRoundTripper(func(req *http.Request) (*http.Response, error) { if key := urlToKey[req.URL.String()]; key != "" { escKey, _ := json.Marshal(key) return &http.Response{ @@ -111,7 +110,7 @@ func TestTravisCI(t *testing.T) { }, nil } return nil, fmt.Errorf("Unhandled URL %s", req.URL.String()) - } + }) // clobber the http client that the service uses to talk to Travis httpClient = &http.Client{Transport: travisTransport} diff --git a/src/github.com/matrix-org/go-neb/testutils/testutils.go b/src/github.com/matrix-org/go-neb/testutils/testutils.go index ba8508e..ee678fa 100644 --- a/src/github.com/matrix-org/go-neb/testutils/testutils.go +++ b/src/github.com/matrix-org/go-neb/testutils/testutils.go @@ -6,6 +6,12 @@ import ( // MockTransport implements RoundTripper type MockTransport struct { + // RT is the RoundTrip function. Replace this function with your test function. + // For example: + // t := MockTransport{} + // t.RT = func(req *http.Request) (*http.Response, error) { + // // assert req args, return res or error + // } RT func(*http.Request) (*http.Response, error) } @@ -13,3 +19,10 @@ type MockTransport struct { func (t MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { return t.RT(req) } + +// NewRoundTripper returns a new RoundTripper which will call the provided function. +func NewRoundTripper(roundTrip func(*http.Request) (*http.Response, error)) http.RoundTripper { + rt := MockTransport{} + rt.RT = roundTrip + return rt +}