TestDownloadAsset pins the octet-stream media type, without which the API returns asset metadata as JSON rather than the binary itself.
(t *testing.T)
| 278 | // TestDownloadAsset pins the octet-stream media type, without which the API returns asset |
| 279 | // metadata as JSON rather than the binary itself. |
| 280 | func TestDownloadAsset(t *testing.T) { |
| 281 | t.Run("sends the octet-stream media type and writes the body", func(t *testing.T) { |
| 282 | reg := &httpmock.Registry{} |
| 283 | defer reg.Verify(t) |
| 284 | |
| 285 | reg.Register( |
| 286 | func(req *http.Request) bool { |
| 287 | return req.URL.String() == "https://example.com/release/cool" && |
| 288 | req.Header.Get("Accept") == "application/octet-stream" |
| 289 | }, |
| 290 | httpmock.StatusStringResponse(http.StatusOK, "BINARY"), |
| 291 | ) |
| 292 | |
| 293 | destPath := filepath.Join(t.TempDir(), "gh-cool") |
| 294 | err := downloadAsset(&http.Client{Transport: reg}, "github.com", safeurl.NewImmutableSafeURL("https://example.com/release/cool"), destPath) |
| 295 | |
| 296 | require.NoError(t, err) |
| 297 | contents, err := os.ReadFile(destPath) |
| 298 | require.NoError(t, err) |
| 299 | assert.Equal(t, "BINARY", string(contents)) |
| 300 | }) |
| 301 | |
| 302 | t.Run("errors are reported", func(t *testing.T) { |
| 303 | reg := &httpmock.Registry{} |
| 304 | defer reg.Verify(t) |
| 305 | |
| 306 | reg.Register( |
| 307 | httpmock.REST(http.MethodGet, "release/cool"), |
| 308 | httpmock.StatusStringResponse(http.StatusNotFound, `{"message":"Not Found"}`), |
| 309 | ) |
| 310 | |
| 311 | destPath := filepath.Join(t.TempDir(), "gh-cool") |
| 312 | err := downloadAsset(&http.Client{Transport: reg}, "github.com", safeurl.NewImmutableSafeURL("https://example.com/release/cool"), destPath) |
| 313 | |
| 314 | requireExtensionHTTPError(t, err, http.StatusNotFound) |
| 315 | assert.NoFileExists(t, destPath) |
| 316 | }) |
| 317 | } |
nothing calls this directly
no test coverage detected