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

Function Test_apiRun_cache

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

Source from the content-addressed store, hash-verified

1424}
1425
1426func Test_apiRun_cache(t *testing.T) {
1427 // Given we have a test server that spies on the number of requests it receives
1428 requestCount := 0
1429 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1430 requestCount++
1431 w.WriteHeader(http.StatusNoContent)
1432 }))
1433 t.Cleanup(s.Close)
1434
1435 ios, _, stdout, stderr := iostreams.Test()
1436 options := ApiOptions{
1437 IO: ios,
1438 Config: func() (gh.Config, error) {
1439 return &ghmock.ConfigMock{
1440 AuthenticationFunc: func() gh.AuthConfig {
1441 cfg := &config.AuthConfig{}
1442 // Required because the http client tries to get the active token and otherwise
1443 // this goes down to go-gh config and panics. Pretty bad solution, it would
1444 // be better if this were black box.
1445 cfg.SetActiveToken("token", "stub")
1446 return cfg
1447 },
1448 // Cached responses are stored in a tempdir that gets automatically cleaned up
1449 CacheDirFunc: func() string {
1450 return t.TempDir()
1451 },
1452 }, nil
1453 },
1454 // You might think that we want to set Host: s.URL here, but you'd be wrong.
1455 // The host field is later used to evaluate an API URL e.g. https://api.host.com/graphql
1456 // The RequestPath field is used exactly as is, for the request if it includes a host.
1457 RequestPath: s.URL,
1458 CacheTTL: time.Minute,
1459 }
1460
1461 // When we run the API behaviour twice
1462 require.NoError(t, apiRun(&options))
1463 require.NoError(t, apiRun(&options))
1464
1465 // We only get one request to the http server because it uses the cached response
1466 assert.Equal(t, 1, requestCount)
1467 assert.Equal(t, "", stdout.String(), "stdout")
1468 assert.Equal(t, "", stderr.String(), "stderr")
1469}
1470
1471func Test_apiRun_invokingAgent(t *testing.T) {
1472 var receivedUA string

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected