Browse Source

Review comments

pull/129/head
Kegan Dougal 8 years ago
parent
commit
683d36b5c5
  1. 5
      src/github.com/matrix-org/go-neb/services/guggy/guggy_test.go
  2. 5
      src/github.com/matrix-org/go-neb/services/rssbot/rssbot_test.go
  3. 5
      src/github.com/matrix-org/go-neb/services/travisci/travisci_test.go
  4. 13
      src/github.com/matrix-org/go-neb/testutils/testutils.go

5
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}

5
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

5
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}

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