From 1b4fbe1317ed2227d9fdf63623a6b02ada12b8e1 Mon Sep 17 00:00:00 2001 From: James Bowes Date: Wed, 11 Aug 2021 06:55:40 -0300 Subject: [PATCH] Add more github tests --- impl/github.go | 11 +++++-- impl/github_test.go | 75 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 impl/github_test.go diff --git a/impl/github.go b/impl/github.go index d651cf1..747b4b8 100644 --- a/impl/github.go +++ b/impl/github.go @@ -14,7 +14,8 @@ import ( // GitHubReleaser is the default Releaser used in whatsnew. type GitHubReleaser struct { - URL string // a complete URL to the releases API. + URL string // a complete URL to the releases API. + Client *http.Client // if not set, http.DefaultClient is used. } // Get a list of releases. @@ -35,9 +36,13 @@ func (g *GitHubReleaser) Get(ctx context.Context, etag string) ([]Release, strin req = req.WithContext(ctx) - resp, err := http.DefaultClient.Do(req) - if err != nil { + c := g.Client + if c == nil { + c = http.DefaultClient + } + resp, err := c.Do(req) + if err != nil { return nil, "", err } defer resp.Body.Close() diff --git a/impl/github_test.go b/impl/github_test.go new file mode 100644 index 0000000..0875f97 --- /dev/null +++ b/impl/github_test.go @@ -0,0 +1,75 @@ +// Copyright (c) 2021 James Bowes. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package impl_test + +import ( + "context" + "errors" + "net/http" + "testing" + + "github.com/jbowes/whatsnew/impl" +) + +func TestGihubReleaser(t *testing.T) { + ctx := context.Background() + ghr := &impl.GitHubReleaser{ + URL: "http://github.com/repos/you/your-app/releases", + Client: &http.Client{ + Transport: http.NewFileTransport( + http.Dir("../testdata/example"), + ), + }, + } + rels, etag, err := ghr.Get(ctx, `"some-etag"`) + if err != nil { + t.Errorf("got unexpected error: %s", err) + } + + if len(rels) != 1 { + t.Errorf("wrong number of releases. expected: %d got: %d", 1, len(rels)) + } + if rels[0].TagName != "0.30.0" { + t.Errorf("wrong tag name. expected: %s got: %s", "0.30.0", rels[0].TagName) + } + + if etag != "" { + t.Errorf("wrong etag. expected: %s got: %s", "", etag) + } +} + +func TestGihubReleaser_errorOn404(t *testing.T) { + ctx := context.Background() + ghr := &impl.GitHubReleaser{ + URL: "http://github.com/repos/you/your-app/badurl", + Client: &http.Client{ + Transport: http.NewFileTransport( + http.Dir("../testdata/example"), + ), + }, + } + _, _, err := ghr.Get(ctx, `"some-etag"`) + if err == nil { + t.Error("expected error but got none") + } +} + +type errTripper struct{} + +func (errTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, errors.New("oops") } + +func TestGihubReleaser_errorOnRequest(t *testing.T) { + ctx := context.Background() + ghr := &impl.GitHubReleaser{ + URL: "http://github.com/repos/you/your-app/badurl", + Client: &http.Client{ + Transport: errTripper{}, + }, + } + _, _, err := ghr.Get(ctx, `"some-etag"`) + if err == nil { + t.Error("expected error but got none") + } +}