func oauthClient(source Source) (*github.Client, error) { ts := oauth2.StaticTokenSource(&oauth2.Token{ AccessToken: source.AccessToken, }) oauthClient := oauth2.NewClient(oauth2.NoContext, ts) apiHost := "api.github.com" if source.GitHubAPIURL != "" { uri, err := url.Parse(source.GitHubAPIURL) if err != nil { return nil, err } apiHost = uri.Host } uploadHost := "uploads.github.com" if source.GitHubUploadsURL != "" { uri, err := url.Parse(source.GitHubUploadsURL) if err != nil { return nil, err } uploadHost = uri.Host } // The google/go-github library uses the same http.Client to perform // requests to both github.com and the S3 download API (for downloading // release assets). We don't want it to user the same OAuth transport for // both. transport := statham.NewTransport(http.DefaultTransport, statham.Mapping{ apiHost: oauthClient.Transport, uploadHost: oauthClient.Transport, }) httpClient := &http.Client{ Transport: transport, } return github.NewClient(httpClient), nil }
var _ = Describe("A per-host transport mapping", func() { var ( defaultTransport *fakeRoundTripper orgTransport *fakeRoundTripper comTransport *fakeRoundTripper transport http.RoundTripper ) BeforeEach(func() { defaultTransport = &fakeRoundTripper{} orgTransport = &fakeRoundTripper{} comTransport = &fakeRoundTripper{} transport = statham.NewTransport(defaultTransport, statham.Mapping{ "example.org": orgTransport, "example.com": comTransport, }) }) Context("when the host doesn't match any domain in the mapping", func() { It("uses the default transport", func() { req, _ := http.NewRequest("GET", "http://example.net", nil) transport.RoundTrip(req) Expect(defaultTransport.Calls).To(Equal(1)) Expect(orgTransport.Calls).To(Equal(0)) Expect(comTransport.Calls).To(Equal(0)) }) })