|
| 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