|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fetch |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + "net/http/httptest" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + testGitHubDn = "https://localhost:12345" |
| 27 | + tarballPathTrailer = "/archive/5d5b1bf126485b0e2c972bac41b376438601e266.tar.gz" |
| 28 | +) |
| 29 | + |
| 30 | +func TestRepoFromTarballLink(t *testing.T) { |
| 31 | + got, err := RepoFromTarballLink(testGitHubDn, testGitHubDn+"/org-name/repo-name"+tarballPathTrailer) |
| 32 | + if err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + want := &Repo{ |
| 36 | + Org: "org-name", |
| 37 | + Repo: "repo-name", |
| 38 | + } |
| 39 | + if diff := cmp.Diff(want, got); diff != "" { |
| 40 | + t.Errorf("mismatch (-want +got):\n%s", diff) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestRepoFromTarballLinkErrors(t *testing.T) { |
| 45 | + for _, test := range []struct { |
| 46 | + tarballLink string |
| 47 | + }{ |
| 48 | + {tarballLink: "too-short"}, |
| 49 | + } { |
| 50 | + if got, err := RepoFromTarballLink(testGitHubDn, test.tarballLink); err == nil { |
| 51 | + t.Errorf("expected an error, got=%v", got) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestSha256(t *testing.T) { |
| 57 | + const ( |
| 58 | + tarballPath = "/googleapis/googleapis/archive/5d5b1bf126485b0e2c972bac41b376438601e266.tar.gz" |
| 59 | + latestShaContents = "The quick brown fox jumps over the lazy dog" |
| 60 | + latestShaContentsHash = "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592" |
| 61 | + ) |
| 62 | + |
| 63 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 64 | + if r.URL.Path != tarballPath { |
| 65 | + t.Fatalf("unexpected request path %q", r.URL.Path) |
| 66 | + } |
| 67 | + w.WriteHeader(http.StatusOK) |
| 68 | + w.Write([]byte(latestShaContents)) |
| 69 | + })) |
| 70 | + defer server.Close() |
| 71 | + |
| 72 | + got, err := Sha256(server.URL + tarballPath) |
| 73 | + if err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + if got != latestShaContentsHash { |
| 77 | + t.Errorf("Sha256() = %q, want %q", got, latestShaContentsHash) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestSha256Error(t *testing.T) { |
| 82 | + for _, test := range []struct { |
| 83 | + name string |
| 84 | + url string |
| 85 | + }{ |
| 86 | + { |
| 87 | + name: "http status error", |
| 88 | + url: func() string { |
| 89 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 90 | + w.WriteHeader(http.StatusBadRequest) |
| 91 | + w.Write([]byte("ERROR - bad request")) |
| 92 | + })) |
| 93 | + t.Cleanup(server.Close) |
| 94 | + return server.URL + "/test" |
| 95 | + }(), |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "invalid url", |
| 99 | + url: "http://invalid-url-that-does-not-exist-12345.local", |
| 100 | + }, |
| 101 | + } { |
| 102 | + t.Run(test.name, func(t *testing.T) { |
| 103 | + if _, err := Sha256(test.url); err == nil { |
| 104 | + t.Error("expected an error from Sha256()") |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestLatestSha(t *testing.T) { |
| 111 | + const ( |
| 112 | + getLatestShaPath = "/repos/googleapis/googleapis/commits/master" |
| 113 | + latestSha = "5d5b1bf126485b0e2c972bac41b376438601e266" |
| 114 | + ) |
| 115 | + |
| 116 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 117 | + if r.URL.Path != getLatestShaPath { |
| 118 | + t.Fatalf("unexpected request path %q", r.URL.Path) |
| 119 | + } |
| 120 | + got := r.Header.Get("Accept") |
| 121 | + want := "application/vnd.github.VERSION.sha" |
| 122 | + if got != want { |
| 123 | + t.Fatalf("mismatched Accept header for %q, got=%q, want=%s", r.URL.Path, got, want) |
| 124 | + } |
| 125 | + w.WriteHeader(http.StatusOK) |
| 126 | + w.Write([]byte(latestSha)) |
| 127 | + })) |
| 128 | + defer server.Close() |
| 129 | + |
| 130 | + got, err := LatestSha(server.URL + getLatestShaPath) |
| 131 | + if err != nil { |
| 132 | + t.Fatal(err) |
| 133 | + } |
| 134 | + if got != latestSha { |
| 135 | + t.Errorf("LatestSha() = %q, want %q", got, latestSha) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestLatestShaError(t *testing.T) { |
| 140 | + for _, test := range []struct { |
| 141 | + name string |
| 142 | + url string |
| 143 | + }{ |
| 144 | + { |
| 145 | + name: "http status error", |
| 146 | + url: func() string { |
| 147 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 148 | + w.WriteHeader(http.StatusBadRequest) |
| 149 | + w.Write([]byte("ERROR - bad request")) |
| 150 | + })) |
| 151 | + t.Cleanup(server.Close) |
| 152 | + return server.URL + "/test" |
| 153 | + }(), |
| 154 | + }, |
| 155 | + { |
| 156 | + name: "invalid url", |
| 157 | + url: "http://invalid-url-that-does-not-exist-12345.local", |
| 158 | + }, |
| 159 | + } { |
| 160 | + t.Run(test.name, func(t *testing.T) { |
| 161 | + if _, err := LatestSha(test.url); err == nil { |
| 162 | + t.Error("expected an error from LatestSha()") |
| 163 | + } |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func TestTarballLink(t *testing.T) { |
| 169 | + for _, test := range []struct { |
| 170 | + githubDownload string |
| 171 | + repo *Repo |
| 172 | + sha string |
| 173 | + want string |
| 174 | + }{ |
| 175 | + { |
| 176 | + githubDownload: "https://github.com", |
| 177 | + repo: &Repo{Org: "googleapis", Repo: "googleapis"}, |
| 178 | + sha: "abc123", |
| 179 | + want: "https://github.com/googleapis/googleapis/archive/abc123.tar.gz", |
| 180 | + }, |
| 181 | + { |
| 182 | + githubDownload: "https://test.example.com", |
| 183 | + repo: &Repo{Org: "my-org", Repo: "my-repo"}, |
| 184 | + sha: "def456", |
| 185 | + want: "https://test.example.com/my-org/my-repo/archive/def456.tar.gz", |
| 186 | + }, |
| 187 | + } { |
| 188 | + got := TarballLink(test.githubDownload, test.repo, test.sha) |
| 189 | + if got != test.want { |
| 190 | + t.Errorf("TarballLink() = %q, want %q", got, test.want) |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments