MCPcopy Create free account
hub / github.com/cli/cli / TestDownloadAsset

Function TestDownloadAsset

pkg/cmd/extension/http_test.go:280–317  ·  view source on GitHub ↗

TestDownloadAsset pins the octet-stream media type, without which the API returns asset metadata as JSON rather than the binary itself.

(t *testing.T)

Source from the content-addressed store, hash-verified

278// TestDownloadAsset pins the octet-stream media type, without which the API returns asset
279// metadata as JSON rather than the binary itself.
280func 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}

Callers

nothing calls this directly

Calls 12

VerifyMethod · 0.95
RegisterMethod · 0.95
StatusStringResponseFunction · 0.92
NewImmutableSafeURLFunction · 0.92
RESTFunction · 0.92
JoinMethod · 0.80
EqualMethod · 0.80
downloadAssetFunction · 0.70
RunMethod · 0.65
StringMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected