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

Function TestFetchCommitSHA

pkg/cmd/extension/http_test.go:240–276  ·  view source on GitHub ↗

TestFetchCommitSHA covers the two things that kept this site on the raw client: the custom Accept media type that makes the API answer with a bare SHA, and the 422 sentinel.

(t *testing.T)

Source from the content-addressed store, hash-verified

238// TestFetchCommitSHA covers the two things that kept this site on the raw client: the custom
239// Accept media type that makes the API answer with a bare SHA, and the 422 sentinel.
240func TestFetchCommitSHA(t *testing.T) {
241 repo := ghrepo.New("OWNER", "REPO")
242
243 t.Run("sends the sha media type and returns the bare body", func(t *testing.T) {
244 reg := &httpmock.Registry{}
245 defer reg.Verify(t)
246
247 reg.Register(
248 func(req *http.Request) bool {
249 return req.URL.Path == "/repos/OWNER/REPO/commits/main" &&
250 req.Header.Get("Accept") == "application/vnd.github.v3.sha"
251 },
252 httpmock.StatusStringResponse(http.StatusOK, "0123456789abcdef"),
253 )
254
255 sha, err := fetchCommitSHA(&http.Client{Transport: reg}, repo, "main")
256
257 require.NoError(t, err)
258 assert.Equal(t, "0123456789abcdef", sha)
259 })
260
261 t.Run("unprocessable entity means the commit was not found", func(t *testing.T) {
262 client := extensionHTTPClient(t, "repos/OWNER/REPO/commits/nope", http.StatusUnprocessableEntity, `{"message":"No commit found"}`)
263
264 _, err := fetchCommitSHA(client, repo, "nope")
265
266 require.ErrorIs(t, err, commitNotFoundErr)
267 })
268
269 t.Run("other errors are reported", func(t *testing.T) {
270 client := extensionHTTPClient(t, "repos/OWNER/REPO/commits/main", http.StatusInternalServerError, `{"message":"Internal Server Error"}`)
271
272 _, err := fetchCommitSHA(client, repo, "main")
273
274 requireExtensionHTTPError(t, err, http.StatusInternalServerError)
275 })
276}
277
278// TestDownloadAsset pins the octet-stream media type, without which the API returns asset
279// metadata as JSON rather than the binary itself.

Callers

nothing calls this directly

Calls 10

VerifyMethod · 0.95
RegisterMethod · 0.95
NewFunction · 0.92
StatusStringResponseFunction · 0.92
fetchCommitSHAFunction · 0.85
extensionHTTPClientFunction · 0.85
EqualMethod · 0.80
RunMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected