Skip to content

Commit

Permalink
Merge pull request #3 from jbowes/github-tests
Browse files Browse the repository at this point in the history
Add more github tests
  • Loading branch information
jbowes committed Aug 11, 2021
2 parents 3709eb8 + 1b4fbe1 commit 12c7836
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
11 changes: 8 additions & 3 deletions impl/github.go
Expand Up @@ -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.
Expand All @@ -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()
Expand Down
75 changes: 75 additions & 0 deletions 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")
}
}

0 comments on commit 12c7836

Please sign in to comment.