MCPcopy
hub / github.com/cli/cli / Test_apiRun_cache

Function Test_apiRun_cache

pkg/cmd/api/api_test.go:1346–1389  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1344}
1345
1346func Test_apiRun_cache(t *testing.T) {
1347 // Given we have a test server that spies on the number of requests it receives
1348 requestCount := 0
1349 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1350 requestCount++
1351 w.WriteHeader(http.StatusNoContent)
1352 }))
1353 t.Cleanup(s.Close)
1354
1355 ios, _, stdout, stderr := iostreams.Test()
1356 options := ApiOptions{
1357 IO: ios,
1358 Config: func() (gh.Config, error) {
1359 return &ghmock.ConfigMock{
1360 AuthenticationFunc: func() gh.AuthConfig {
1361 cfg := &config.AuthConfig{}
1362 // Required because the http client tries to get the active token and otherwise
1363 // this goes down to to go-gh config and panics. Pretty bad solution, it would
1364 // be better if this were black box.
1365 cfg.SetActiveToken("token", "stub")
1366 return cfg
1367 },
1368 // Cached responses are stored in a tempdir that gets automatically cleaned up
1369 CacheDirFunc: func() string {
1370 return t.TempDir()
1371 },
1372 }, nil
1373 },
1374 // You might think that we want to set Host: s.URL here, but you'd be wrong.
1375 // The host field is later used to evaluate an API URL e.g. https://api.host.com/graphql
1376 // The RequestPath field is used exactly as is, for the request if it includes a host.
1377 RequestPath: s.URL,
1378 CacheTTL: time.Minute,
1379 }
1380
1381 // When we run the API behaviour twice
1382 require.NoError(t, apiRun(&options))
1383 require.NoError(t, apiRun(&options))
1384
1385 // We only get one request to the http server because it uses the cached response
1386 assert.Equal(t, 1, requestCount)
1387 assert.Equal(t, "", stdout.String(), "stdout")
1388 assert.Equal(t, "", stderr.String(), "stderr")
1389}
1390
1391func Test_apiRun_invokingAgent(t *testing.T) {
1392 var receivedUA string

Callers

nothing calls this directly

Calls 5

SetActiveTokenMethod · 0.95
TestFunction · 0.92
apiRunFunction · 0.85
EqualMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected