Skip to content

Commit 12c7836

Browse files
authored
Merge pull request #3 from jbowes/github-tests
Add more github tests
2 parents 3709eb8 + 1b4fbe1 commit 12c7836

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

impl/github.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414

1515
// GitHubReleaser is the default Releaser used in whatsnew.
1616
type GitHubReleaser struct {
17-
URL string // a complete URL to the releases API.
17+
URL string // a complete URL to the releases API.
18+
Client *http.Client // if not set, http.DefaultClient is used.
1819
}
1920

2021
// Get a list of releases.
@@ -35,9 +36,13 @@ func (g *GitHubReleaser) Get(ctx context.Context, etag string) ([]Release, strin
3536

3637
req = req.WithContext(ctx)
3738

38-
resp, err := http.DefaultClient.Do(req)
39-
if err != nil {
39+
c := g.Client
40+
if c == nil {
41+
c = http.DefaultClient
42+
}
4043

44+
resp, err := c.Do(req)
45+
if err != nil {
4146
return nil, "", err
4247
}
4348
defer resp.Body.Close()

impl/github_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2021 James Bowes. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package impl_test
6+
7+
import (
8+
"context"
9+
"errors"
10+
"net/http"
11+
"testing"
12+
13+
"github.com/jbowes/whatsnew/impl"
14+
)
15+
16+
func TestGihubReleaser(t *testing.T) {
17+
ctx := context.Background()
18+
ghr := &impl.GitHubReleaser{
19+
URL: "http://github.com/repos/you/your-app/releases",
20+
Client: &http.Client{
21+
Transport: http.NewFileTransport(
22+
http.Dir("../testdata/example"),
23+
),
24+
},
25+
}
26+
rels, etag, err := ghr.Get(ctx, `"some-etag"`)
27+
if err != nil {
28+
t.Errorf("got unexpected error: %s", err)
29+
}
30+
31+
if len(rels) != 1 {
32+
t.Errorf("wrong number of releases. expected: %d got: %d", 1, len(rels))
33+
}
34+
if rels[0].TagName != "0.30.0" {
35+
t.Errorf("wrong tag name. expected: %s got: %s", "0.30.0", rels[0].TagName)
36+
}
37+
38+
if etag != "" {
39+
t.Errorf("wrong etag. expected: %s got: %s", "", etag)
40+
}
41+
}
42+
43+
func TestGihubReleaser_errorOn404(t *testing.T) {
44+
ctx := context.Background()
45+
ghr := &impl.GitHubReleaser{
46+
URL: "http://github.com/repos/you/your-app/badurl",
47+
Client: &http.Client{
48+
Transport: http.NewFileTransport(
49+
http.Dir("../testdata/example"),
50+
),
51+
},
52+
}
53+
_, _, err := ghr.Get(ctx, `"some-etag"`)
54+
if err == nil {
55+
t.Error("expected error but got none")
56+
}
57+
}
58+
59+
type errTripper struct{}
60+
61+
func (errTripper) RoundTrip(*http.Request) (*http.Response, error) { return nil, errors.New("oops") }
62+
63+
func TestGihubReleaser_errorOnRequest(t *testing.T) {
64+
ctx := context.Background()
65+
ghr := &impl.GitHubReleaser{
66+
URL: "http://github.com/repos/you/your-app/badurl",
67+
Client: &http.Client{
68+
Transport: errTripper{},
69+
},
70+
}
71+
_, _, err := ghr.Get(ctx, `"some-etag"`)
72+
if err == nil {
73+
t.Error("expected error but got none")
74+
}
75+
}

0 commit comments

Comments
 (0)