Last active
November 14, 2017 06:06
-
-
Save albertoleal/0b46fa7dd4e07e4f2f4f99e60ba203b2 to your computer and use it in GitHub Desktop.
Strava client mock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package strava_test | |
import ( | |
"net" | |
"net/http" | |
"net/url" | |
"time" | |
. "github.com/onsi/ginkgo" | |
. "github.com/onsi/gomega" | |
"github.com/onsi/gomega/ghttp" | |
"github.com/scottmuc/health/strava" | |
gostrava "github.com/strava/go.strava" | |
) | |
var _ = Describe("StravaService", func() { | |
var ( | |
fakeStravaServer *ghttp.Server | |
service strava.Service | |
) | |
BeforeEach(func() { | |
fakeStravaServer = ghttp.NewServer() | |
dial := func(network, addr string) (net.Conn, error) { | |
fakeStravaServerURL, err := url.Parse(fakeStravaServer.URL()) | |
Expect(err).ToNot(HaveOccurred()) | |
return net.Dial(network, fakeStravaServerURL.Host) | |
} | |
fakeStravaClient := &http.Client{ | |
Transport: &http.Transport{ | |
Dial: dial, | |
DialTLS: dial, | |
}, | |
} | |
service = strava.NewService("my-awesome-token", fakeStravaClient) | |
}) | |
Context("when Strava API returns a list of activities", func() { | |
var startDate time.Time | |
BeforeEach(func() { | |
startDate = time.Now() | |
activities := []*gostrava.ActivitySummary{ | |
{Id: 1, Name: "cycling", StartDate: startDate}, | |
{Id: 2, Name: "running", StartDate: startDate}, | |
} | |
fakeStravaServer.AppendHandlers( | |
ghttp.CombineHandlers( | |
ghttp.VerifyRequest("GET", "/api/v3/athlete/activities"), | |
ghttp.RespondWithJSONEncoded(http.StatusOK, activities), | |
), | |
) | |
}) | |
It("returns the first activity", func() { | |
activity, startDate, err := service.GetLatestActivity() | |
Expect(err).NotTo(HaveOccurred()) | |
Expect(activity).To(Equal("cycling")) | |
Expect(startDate).To(Equal(startDate)) | |
}) | |
}) | |
Context("when fails to talk to Strava API", func() { | |
BeforeEach(func() { | |
fakeStravaServer.AppendHandlers( | |
ghttp.CombineHandlers( | |
ghttp.VerifyRequest("GET", "/api/v3/athlete/activities"), | |
ghttp.RespondWith(http.StatusInternalServerError, "server error"), | |
), | |
) | |
}) | |
It("returns an error", func() { | |
_, _, err := service.GetLatestActivity() | |
Expect(err).To(MatchError(ContainSubstring("failed to talk to Strava API"))) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment